C++ primer:第三章备忘。

来源:互联网 发布:wampserver for mac 编辑:程序博客网 时间:2024/06/05 14:51

1、处理每个字符?使用基于范围的for语句
for(declaration:expression)
statement;

其中expression部分是一个对象,表示一个序列
declaration部分负责定义一个变量,用以访问序列中的基础元素
例子:
string str(“some string”);
for(auto c : str)
cout << c << endl;

2、列表初始化
vector< string > articles={“a”,”an”,”the”};

3、理解复杂的数组声明
由内而外,从右往左
int arr[10];
int* ptrs[10];
int& ref[10]; //错误:不存在引用的数组
int (*parr)[10]=&arr;
int (&arrRef)[10]=arr;

4、C风格字符串
< string.h >
strlen
strcmp
strcat
strcpy

string s;
const char* str=s.c_str();

5、多维数组

int ia[3][4];int arr[10][20][30];

由内而外顺序阅读,ia含有3个元素的数组,而其中每个元素都是一个含有4个元素的数组

arr等同。

初始化:

int ia[3][4]={    {0,1,2,3},    {4,5,6,7},    {8,9,10,11} };

使用范围for语句处理多维数组

for(auto &row : ia)    for(auto &col : row)       col=0;

声明成引用类型是因为要改变元素的值

类型别名简化多维数组的指针:
using int_array =int[4];
typedef int int_array[4];

原创粉丝点击