C++ Primer Note

C++ Primer


chapter 1 Variable, Basic type


  1. bool, int, char, double, float
  2. unsigned,long long
    • Use an unsigned type when you know that the values cannot be negative
    • Use double rather than float
    • Don’t Mix Signed and Unsigned Types
  3. object is a region of memory that can contain data and has a type.
    • Don’t use Uninitialized Variables
  4. scope of a name
    • grobal scope
    • block scope
ReferencePointer
int &r = valint *ptr = &val
bind to a objecthold the address of another object
not a objecta object
can’t reblind to a new objectcan be assigned and copied
must be initializedcan be invaild

Note: ModernC++ programs generally should avoid using NULL and use nullptr instead.

  1. void* Pointer
    • it can hold the address of any object, but the type of the object at that address is unknown.

Tip:It can be easier to understand complicated pointer or reference declarations if you read them from right to left.

  1. const
    • const int &r = i; //r is bound to i;but can’t change i;
    • int *const ptr = & i; //ptr always point to i;
    • const int *ptr = & i; // ptr can point to another object,but can’t change i;
    • const int *const ptr = & i;
  2. constexpr
  3. auto
    • auto f(){/***/} // C++14才支持
  4. decltype(推断函数返回类型)
    • decltype(f()) i = x; // i return the type of f()
    • decltype((variable))?decltype(variable)?

chapter 2 Vector,string,array


  1. namespace
    • Headers Should Not Include using Declarations
  2. string
    • operate
      • os<<s,in>>s // 按空格记录
      • getline(is,s) // 按行记录
      • s.empty(), s.size()
      • s[n],+,-,==,!=,>,<
    • string::size_type //足够储存string的size,储存string size的变量类型,可以用auto替代
    • range for语句
      • for( auto i : str){ /***/}
    • char
      • ispunct()
      • toupper()
      • isspace()
  3. vector
    • If differing element values are needed, it is usually more efficient to define an empty vector.
    • operate
      • push_back()
      • empty(), size()
      • ==, !=, <, >
    • vector< int >::size_type
    • 使用range for避免下标超出
  4. iterator
    • begin(), end()
    • operate
      • ++it, --it, *it, ==, !=
    • binary search(二分查找)
  5. Array
    • Tip:If you don’t know exactly how many elements you need, use a vector

    • int *ptr = &arr[0] // 或int *ptr = arr;

    • use iterator

      int *beg = begin(arr), *en = end(arr);
      while(beg!=en)
      	cout<<*beg++<<endl;
      
    • use array init vector

      • vector< int>v( begin(arr), end(arr) );

chapter 3 Expression


  1. Arithmetic Operators
    • 单目优先性高于双目
    • “+,-”,*,/,%,+,-
    • m%(-n) = m%n, (-m)%n = -m%n
  2. logic Operators
    • AND,OR,NOT,equal
  3. Assignment is Right associate
    • "="的运算是从右往左,
    • ”=“的优先性极低,比关系操作符低
    • ”=“,"=="
  4. 自加,自减
    • Adivse: 只在需要的时候采用后缀++,(后缀++需要返回一个拷贝的对象和值,而前缀只返回对象本身)
    • 使代码尽量简洁
  5. The Condition Operate
    • cond ? exp1 : exp2;
  6. Bitwise Operator
    • ~,<<,>>,&,^,|
    • 建议unsigned定义
  7. sizeof

chapter 4 Statement


  1. Null statement
    • Null statements should be commented.
  2. if…else…
    • if与临近else匹配
  3. switch
  4. while, for, do while
    • 在for内定义的变量只在循环内可见
  5. range for
    • If we want to write to the elements in the sequence, the loop variable must be a reference type.
  6. break, continue
  7. try…expect…
    • throw
    • try{/**/}catch{//}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值