zoukankan      html  css  js  c++  java
  • 看C++时记得一些笔记

    Windows Forms是利用.NET Framework程序库的代码全自动生成的GUI(Graphical User Interface)编程方式

    CLR中执行的c++称为托管的c++,否则为非托管c++或本地c++.

    初视化变量 int value(0);

    Wchar_t为宽字符类型,变量存储2字节字符代码,值域0—65535 wchar_t letter = L’z’; or wchar_t letter(L’z’);

    enum Week{Mon = 1, Tues = 1, Wed, Thurs, Fri, Sat, Sun} thisWeek; thisWeek = thurs;(4个字节)

    格式化输出cout:

             #include<iomanip>  cout<<setw(n) n位宽 cout<<hex cout<<dec;

             cout.width(n)

             cout.fill(‘*’)

             cout.precision()

             cout.write(const char_type* s, streamsize n);

             cout.setf(ios_base::showpoint)

                       1、fmtflags setf( fmtflags );

                       ios_base::boolalpha\ ios_base::showbase\ ios_base::showpoint\ ios_base::uppercase ios_base::showpos

                       2、fmtflags setf( fmtflags, fmtflags );

    第二参数

    第一参数

    ios_base::basefield

    ios_base::dec

    ios_base::oct

    ios_base::hex

    ios_base::floatfield

    ios_base::fixed

    ios_base::scientific

    Ios_base::adjustfield

    ios_base::left

    ios_base::right

    ios_base::internal

    cout << dec << oct << hex << fixed << scientific << left <<right << internal << flush;

    标准输入

    cin.ignore(255, ‘\n’);

    cin.read(gross. 144); ch = cin.peek(); cin.gcount(); cin.pushback(ch);

    强制类型转换:

             static_cast<int>(n)静态检查

             dynamic_cast<double>(n)动态检查

             const_cast 删除表达式中的const属性

             reinterpret_cast 无条件强制转换

    const char *p; char *const p; const char *const p;

    int *p = new int(0); delete p; int *p = new int[20]; delete [] p;

    #include<cstdarg>

    int sum(int cout,…)

    {

             va_list p;

             va_start(p,count);  //p = *count;

             int sum = 0;

             for(int i = 0; i < cout; i++)

                       sum += va_arg(p, int); //va_arg(p, int)返回一个int值,并将p指向下一位

             va_end(p);

             return sum;

    }

    Try

    {

             ……

             Throw 表达式;

    }

    catch(type name)// catch (…) //catch any type exception.

    {

    }

    #include<new> catch(bad_alloc &ex){ cout << ex.what();}

    template<class T>

    T max(T x[], int len)

    {}

    内联函数不能递归

    具体化:

    template <> void Swap<>( int &, int &)

    template <> void Swap ( int &, int &)

    实例化:

             template void Swap<int>( int &, int &)

    struct data{

             char name[20];

             mutable int id;

    };

    const data mike = { “VipXD”, 0 };

    data.id = 2;  //allowed

    #include<new>

    int main()

    {

             char str[100];

             int *p = new (str) int [10];

             delete [] p;

    return 0;

    }

    名称空间(开放的)可以是全局,也可以位于名称空间内,但不能在代码块里。

    namespace MyFavariteThing{};

    namespace Mft = MyFavariteThing;

    #include<iostream>

    #ifndef GString_H_

    #define GString_H_

    class GString{

             private:

                       enum{ len = 30 }; // const int len = 30 not allowed. Static const int len = 30 also allowed.

                       ………..

             public:

                       GString();// explicit GString(); 关闭隐式转换

                       GString(const GString &);

                       virtual ~GString();

                       GString operator + (const GString &) const;

                       friend GString operator + (double m, const GString &);

                       friend ostream & operator << (ostream &os, const GString &);

                       operator int () const;{ return int (money);}

    };

    #endif

    class GString2:virtual public GString, public virtual GString1

    {

             public:

                       GString2():GString(), GString0(), GString1();

    }

    template <class T1, class T2 = int>

    class GString

    {}

    template <template T> void counts();

    template <template T> void report(T &);

    template <template <typename T> class Thing, class U, class V>//模板作为参数

    class Crab

    {

             private:

                       Thing<U> s1;

                       Thing<V> s2;

             public:

                       friend class GString;

                       friend void show( Crab<vector, int, double> &);//非模板类友元

                       friend void counts<Thing, U, V>();   //约束模板类友元

                       friend void report<>(Crab<thing, U, V> &);  //约束模板类友元

                       template<class A, class B>

                       friend void write(A &, B &);//非约束模板友元

    }

    嵌套类:

    1、  嵌套类为别一个类的私有部分,则只有后者知道它。

    2、  嵌套类为别一个类的保护部分,对于后者是可见的,对于外部世界是不可见的。

    3、  嵌套类为别一个类的公有部分,后者,后者的派生类及外部世界都可以用它,但外部世界要用类限定符。

    string a = “1.txt”;

    ostream fout;

    fout.open(a.c_str());

    #include <memory>

    auto_ptr<string> ps (new string (str));

    //delete ps; not needed. Auto_ptr类的析构函数用的是delete p;而不是delete [];

    STL提供了一组表示容器,迭代器,函数对象和算法的模板。

    迭代器能够用来遍历容器的对象,与能够遍历数组的指针类似,是广义指针。

    vector<double>::iterator p;

    vector<double> scores;

    P = scores.begin();//push_back() erase(…,…), insert(…,…,…), size(), end().

    #include <algorithm>  // for_each(…,…,函数指针), random_shuffle(…,…), sort().

    输入迭代器:该类的算法不会修改容器中的值,是单向迭代器,可递增,不可倒退;

    输出迭代器:单通行,只写算法可用;

    正向迭代器:多次通行算法可行,只使用++操作符遍历容器,总是按相同顺序遍历一系列值

    双向迭代器:双向迭代器具有正向迭代器的所有特性,同时支持两种递减操作符。

    随机访问迭代器:可用+= -= 等符号访问容器。具有迭代器的所有功能。

    copy(casts, casts + 10, dice.begin()); // int casts[] = {9,42,4,65,2}; vector <int> dice[10]; 足够大。

    #include <iterator>

    ostream_iterator<int, char> out_iter(cout, “ “);

    copy(istream_iterator<int, char> (cin), istream_iterator<int, char>(), dice.begin());

    reverse_iterator 执行递增将导致递减,rend(),rbegin()返回值就是这一类迭代器。

    vector<double>:: Reverse_iterator pp;

    back_insert_iterator、front_insert_iterator

    back_insert_iterator<vector<int> > back_itr(dice);

    insert_iterator

    insert_iterator<vector<int> > insert_iter(dice, dice.begin());

    container: deque, list, queue, priority_queue, stack, vector, map, multimap, set, multiset, bitset//not mentioned

    联合容器((associative container)   ):#include <set> set、multiset

    // set<string, less<string> > A; //set<string> A;

    String s1[6] = {“buffoon”, “thinkers”, “for”, “heavy”, “can”, “for”};

    set<string> A(s1, s1 + 6);

    集合被排序为: buffoon can for heavy thinkers

    并交差集:

    set_union(A.begin(), A.end, B.begin, B.end, insert_iterator<set<string> > (C, C.begin() ) );

    set_intersection(A.begin(), A.end, B.begin, B.end, insert_iterator<set<string> > (C, C.begin() ) );

    set_difference(A.begin(), A.end, B.begin, B.end, insert_iterator<set<string> > (C, C.begin() ) );

    lower_bound() 关键字为参数,返回一个迭代器.指向第一个不小于关键字参数的成员.

    upper_bound() 关键字为参数,返回一个迭代器.指向第一个大于关键字参数的成员.

    #include <map> map、multimap

    multimap<int, string> codes; //multimap<int, string, less<string>> codes;

    pair<const int, string> item (213, “los angeles”);

    codes.insert( item );

    cout << item.first << item.second;

    lower_bound(), upper_bound()同上set;

    equal_range() 关键字为参数,返回该关键字匹配的区间的迭代器。为返回两个值,将两个值封装在一具pair对象中

    pair<mutilmap<int, string>::iterator,

    mutilmap<int, string>::iterator > rang

    = codes.equal_range(718);

    multilmap<int, string>::iterator it;

    for (it = rang.first, it != rang.second, it++)

             cout << (*it).second << endl;

    STL算法:使用函数对象——函数符;

    int arr[] = {1, 2, 3};

    vector<double> dice(arr, arr + 3), dice1[10];

    ostream_iterator<double, char> out(cout, “ “);

    transform(dice.begin(), dice.end(), out, sqrt);

    double add(double, double);

    transform(dice.begin(), dice.end(), dice1.begin(), add);

    binder1st (f2, val) f1; //f2为二元函数,val将赋值给f2的第一个参数,f1(x);

    bind1st(multiplies<double> (), 2.5);

    binder2nd\bind2nd

    next_permutation(dice.begin(), dice.end());

    for_each() //需要特别注意。。。。

    cerr 标准错误流,没有被缓冲 clog标准错误流,被缓冲。

    Ifstream fin(“1.txt”, mode);

    ios_base::in ios_base::out ios_base::ate ios_base::app ios_base::turn ios_base::binay

    seekg() \ seekp() 前者将输入指针移到指定的文件位置,后者将输出指针移到指定的文件位置。也可以将seekg()用于ifstream,将seekp()用于oftream.(tellg() \ tellp())

    fin.seekg(distance, ios_base::beg); fin.seekg(distance, ios_base::cur) fin.seekg(distance, ios_base::end);

    #include<sstream>

    ostringstream outstr;

    outstr << “xiao”;

    string1 = outstr.str();

    istringstream instr(stirng1);

    #include<ctime>

    std::srand(std::time(0));

    std::rand()

  • 相关阅读:
    Leetcode Power of Two
    Leetcode Reverse Integer
    Leetcode Add Digits
    Leetcode Roman to Integer
    Python 函数的定义语法
    Python 函数的三种定义方式
    Python 函数的定义与调用
    Python 函数分类
    Python 为什么要使用函数
    Python 文件的二进制读写
  • 原文地址:https://www.cnblogs.com/dongxiao/p/2512177.html
Copyright © 2011-2022 走看看