基础知识罗列
char v[6];
char* p;
[] 表示 array of
*表示pointer to
数组的大小应该是一个constant表达式
一元* 表示 contents of
一元&表示 address of
除了以前学过的for循环
新标准增加了一种可以range-for语句:
int v[] = {1, 2, 3};
for (auto x : v){
std::cout << x << std::endl;
}
for (auto x : {1, 2, 3}){
std::cout << x << std::endl;
}
我们来看看下面两种读法:
for (auto i=0; i!=10; ++i) // copy elements
v2[i]=v1[i];
set i to zero; while i is not 10, copy the ith element and increment i .
那range-for的读法如下:
for every element of v, from the first to the last, place a copy in x and print it.
如果我们不仅仅是需要对对数组元素的拷贝进行处理,而是要对数据元素进行处理,eg:
for ( auto& x : v)
& 在声明中表示 reference to
引用特别适合用在函数参数中,这样就不会进行拷贝
如果不想function的参数被改变,又不想进行拷贝,eg:
void func ( const vector& x);
函数参数设置为const引用非常常用
有一个nullptr关键字 表示空指针,对所有指针类型都适用
以前都是用NULL来表示,但有一个问题:NULL和0是可以替换的,如果有一个0,这时就无法区分是空指针还是数值0(ps:在一些著名的json开源库中,这个问题坑了一大批使用者)
经常检查指针是否为空是必须的,尽量使用新标准提供的nullptr
如果我们不使用带初始化的for循环,我们可以使用while循环来代替
cout <<
cin >>
<< 读put to(用于输出) >> 读get from(用于输入)