Skip to content

C++ Review

  1. int i=0;
    int i={0};
    int i{0};
    int i(0);
  2. 函数之内初始化为0,函数之外为未定义
  3. std::cout, cin, cerr, clog
  4. extern int i;不能初始化,常量extern可以用表达式 extern const x = i;
  5. 应用 int &ref=val;
  6. const int &r =i;不允许替代修改
  7. int *p=nullptr;
  8. int *p=0;
  9. int &const cur = &err 一直指向err
  10. const int *const pip=π
  11. 顶层const不能改值,底层指向一个const对象
    int *const cur = &err;   //if err is int. However if err is const int, then reverse.
    err=&a; //error
    *err=a; //valid
    /* In C/C++, the type of "int *" is allowed to up qualify to the type "const int *". */
  12. typedef char* pstring
    const pstring a=0;
    //equal to
    char *const a=0; //不能展开理解
    
  13. constexpr编译时候知道结果,指针必须初始化为nullptr or 0
  14. using SI=int; 
    typedef int SI
  15. auto
  16. decltype(a) c;
  17. string s(n,'c')
    s.empty()
    s.size();
    getline(is,s)
    s1==s2
    s[n]
  18. while(cin >> word)
  19. auto len = line.size()     unsigned int
  20. line.size() < 0!!!!
  21. 字面值不能相加
    s1+","+"hello";//valid
    ","+s1//error
  22. ctype
  23. for (decl:express)  for(auto c: str)
  24. for (auto &c:s)
  25. vector<T> v(n,val);
    vector<T> v(n);
    vector<T> v{a,b,c}
    v.push_back(i);
    
    vector<int>::size_type
    v.end()
    v.begin()
    
    *iter
    iter->mem
    
    while(s.begin()!=s.end())
    
    auto it = s.begin();
    vector<int>::iterator
    
    v.begin()
    v.cbegin()
    v.empty()
    
  26. iter1-iter2同一容器, different_type距离可正可负
  27. int *p = &ia[2]
  28. int k = p[-2]
  29. const char *str = s.c_str();
  30. vector<int> iv(begin(arr), end(arr))
  31. 尽量用标准库而不是数组
  32. 取模  (-21)%(-5) = -5   21%(-5) = 1
  33. cast_name<type>(expr)
    double s = i/j;
    static_cast<double>(i)/j
  34. switch case
  35. try catch
  36. 传引用参数,相当于直接使用,避免拷贝,返回额外信息
  37. 形参会自动忽略顶层const,所以尽量使用常量引用,会通用一些 const int &r = 42;
  38. 多维数组,第二维度之后的数组大小是数据类型不能省略
  39. 不要返回局部引用
  40. initializer_list<T>
  41. int *p1[10]
  42. type (*func)(params)
  43. auto func(int i) -> int(*) [10];
  44. 默认实参
  45. __func__函数名
    __FILE__文件名
    __LINE__当前行数
    __TIME__
    __DATE__编译日期
  46. -D NDEBUG
  47. 重载
  48. istringstream record(line)
  49. record >> name;
  50. vector
    deque//双向链表
    list
    forward_list
    array //更安全
    string
  51. iterator
    const_iterator
    reference
    const_reference
    value_type
    size_type
    different_type
  52. c1=c2
  53. a.swap(b)
  54. swap(a,b)
  55. c1={a,b,c,d}
  56. c(begin,end) //不支持array
  57. c.size()
  58. c.max_size()
  59. c.empty()
  60. c.insert()
  61. c.erase(args)
  62. c.clear()
  63. rbegin(),反向迭代器+1,获得上一个元素
  64. array<int,10> a= {42}   a[0]= 42,其他为0
  65. swap比赋值拷贝快得多
  66. array不支持吃{}和assign
  67. seq.assign(b,e)
  68. //vector和string不支持push_front
    
    c.push_back()
    c.insert(p,t)// insert t before p
    c.insert(p, n, t)
    c.insert(p, begin, end)
    //-------------\\\list
    c.front()
    c.begin()
    c.end()
    c.back()尾元素
    
    c.at(n)// exception
    auto &v = c.back()返回的是引用
    
    
    c.pop_front()
    c.pop_back()
    c.erase(p)
  69. list.resize(15)
  70. c.capacity()
  71. c.reverse()
  72. s.append()
  73. s.replace(start,len, “Ff”)
  74. s.substr(pos,n)
  75. s.find()
    s.rfind()
    s.find_first_of()
    s.find_last_of()
    s.find_first_not_of()
    s.find_last_not_of()
  76. s.compare(s2)
  77. s=to_string(int)
  78. double d= stod(s)//stoi stol stoll stoull
  79. stack/queu/priority_queue
  80. //stack
    s.pop()
    s.push()
    s.emplace()
    s.top()
    
    //queue
    q.front()
    q.back()
    q.top()
    q.push(item)
    
    // algorithm
    swap()
    find(begin, end, val)//find_if(begin, end, pred)
    fill
    copy//reverse_copy
    replace
    sort(begin(),end())
    ret=unique(b,e)迭代器最后一个不重复元素的位置
    find_if(b,e,lambda)
    
  81. lambda
    [capture list](parameter) -> return type {
    function;
    }
    
    
    auto f = []{return 42}
    //只有单一return不需要return type
  82. lambda不能有默认参数
  83. 可以捕获引用 [&v1]{return v1;}
  84. 尽量减少捕获数量
  85. 要对捕获的数值期望负责
  86. [=]值,[&]引用
  87. [=, &os](){}输出流必须引用
  88. bind(callable, arg_list)
  89. auto g = bind(f,a,b,c-2,c)占位符-1
  90. using std::placeholder::-1
  91. //list 特有的
    lst.merge(lst2);
    lst.merge(lst2,comp)
    lst.remove(val)
    lst.remove_if(pred)
    lst.reverse()
    lst.sort()
    lst.unique()
    
    lst.splice()
    
  92. map/set/unordered_map/unordered_set
    mapval.first
    mapval.second
  93. {{key,val},}
  94. pair<string,string> a {“key”,”Val”}
  95. make_pair<>()
  96. p1.first, p1.second
  97. mapped_type/value_type/key_type
  98. map[x]==x不存在会创建一个,所以必须用m.find(k)
  99. 无序容器使用桶实现,每个桶保存一个或者多个元素,用一个hash_func映射到桶里
  100. shared_ptr<T> p1; p.get();p.swap(q)p1.empty()
  101. make_shared<int>(42)
  102. new/ delete之后要dangling pointer,要赋值成null,和未初始化一样
Published inDiary

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

http://next.sh/wp-content/smilies/icon_confused.gif 
http://next.sh/wp-content/smilies/icon_cool.gif 
http://next.sh/wp-content/smilies/icon_cry.gif 
http://next.sh/wp-content/smilies/icon_eek.gif 
http://next.sh/wp-content/smilies/icon_evil.gif 
http://next.sh/wp-content/smilies/icon_exclaim.gif 
http://next.sh/wp-content/smilies/icon_idea.gif 
http://next.sh/wp-content/smilies/icon_mad.gif 
http://next.sh/wp-content/smilies/icon_mrgreen.gif 
http://next.sh/wp-content/smilies/icon_neutral.gif 
http://next.sh/wp-content/smilies/icon_question.gif 
http://next.sh/wp-content/smilies/icon_razz.gif 
http://next.sh/wp-content/smilies/icon_redface.gif 
http://next.sh/wp-content/smilies/icon_rolleyes.gif 
http://next.sh/wp-content/smilies/icon_sad.gif 
http://next.sh/wp-content/smilies/icon_smile.gif 
http://next.sh/wp-content/smilies/icon_arrow.gif 
http://next.sh/wp-content/smilies/icon_biggrin.gif 
http://next.sh/wp-content/smilies/icon_surprised.gif 
http://next.sh/wp-content/smilies/icon_twisted.gif 
http://next.sh/wp-content/smilies/10.gif 
http://next.sh/wp-content/smilies/102.gif 
http://next.sh/wp-content/smilies/103.gif 
http://next.sh/wp-content/smilies/106.gif 
http://next.sh/wp-content/smilies/108.gif 
http://next.sh/wp-content/smilies/20.gif 
http://next.sh/wp-content/smilies/26.gif 
http://next.sh/wp-content/smilies/37.gif 
http://next.sh/wp-content/smilies/99.gif