一些学习C++过程中的笔记。

变量三种不同的初始化方法:
c-like:int a = 5;
constructor:int b (3);
uniform:int c {2};

auto和decltype

整数前缀0,0x分别表示8与16进制。整数后缀u,l,ll,ul(不区分大小写)分别表示unsigned, long, longlong, unsigned long

浮点数可用e表示科学计数。浮点数后缀f,l(不区分大小写)分别表示float, long double,默认为double

多个“”内字符串串接,也可用行末的\来换行。字符,字符串前缀u,U,L(区分大小写)表示char16_t, char32_t, wchar_t,默认char为8bit。字符串前缀u8,R分别表示编码格式UTF-8和原始的字符串,后者中\和'"是字符串中的文字,不用转义,用法如下:
R"(string with \backslah)"
R"&%$(string with \backslash)&%$"
以上两者都和"string with \\backslash"一样的

#define lines are preprocessor directives, and as such are single-line instructions that -unlike C++ statements- do not require semicolons (;) at the end; the directive extends automatically until the end of the line. If a semicolon is included in the line, it is part of the replacement sequence and is also included in all replaced occurrences.

short-circuit
This is mostly important when the right-hand expression has side effects, such as altering values:
if ( (i<10) && (++i<n) ) { /*...*/ } // note that the condition increments i
Here, the combined conditional expression would increase i by one, but only if the condition on the left of && is true, because otherwise, the condition on the right-hand side (++i<n) is never evaluated.

getline()没有用,是cin里还有上次剩余的\n
加上cin.ignore();

scope
global scope
namespace scope
local scope

static storage & automatic storage

pointers & arrays
They have a lot in common but not the same. The main difference is that pointer can be assigned a different address, whereas array can never be assigned anything:

int * foo, * bar;
int array [6];
foo = bar;                                                                           
array = foo;

compile with g++:

In function ‘int main()’:
error: incompatible types in assignment of ‘int*’ to ‘int [6]’
  array = foo;
          ^~~

type aliases, typedef / using
Type aliases can be used to reduce the length of long or confusing type names, but they are most useful as tools to abstract programs from the underlying types they use. For example, by using an alias of int to refer to a particular kind of parameter instead of using int directly, it allows for the type to be easily replaced by long (or some other type) in a later version, without having to change every instance where it is used.

Overloading Operators
C++ allows most operators to be overloaded.

2's complement (补码)
取反码再加1
为什么?
因为最高位的意义变为,1为负,0为正
如在用一个二时制三位数来表示int时
111即十时制-1
(-1)*2^2 + 1*2^1 + 1*2^0 = -4 + 2 + 1 = -1

二进制  十进制
000     0
001     1
010     2
011     3
100    -4
101    -3
110    -2
111    -1

001即十进制1,先取反,110,再加1,得到111,即-1
111即十进制-1,先取反,000,再加1,得到001,即1