C primer 第三章備忘。

2021-08-08 18:41:05 字數 1064 閱讀 1623

1、處理每個字元?使用基於範圍的for語句

for(declaration:expression)

statement;

其中expression部分是乙個物件,表示乙個序列

declaration部分負責定義乙個變數,用以訪問序列中的基礎元素

例子:

string str(「some string」);

for(auto c : str)

cout << c << endl;

2、列表初始化

vector< string > articles=;

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]=,, 

};

使用範圍for語句處理多維陣列

for(auto &row : ia)

for(auto &col : row)

col=0;

宣告成引用型別是因為要改變元素的值

型別別名簡化多維陣列的指標:

using int_array =int[4];

typedef int int_array[4];

c primer 筆記,第三章

初始化string物件的6種方式 string s1 預設空串 sting s2 s1 string s2 s1 string s3 value 直接初始化 string s3 value 拷貝初始化 string s4 n,c 由連續n個字元c組成的串在讀寫string物件時,string物件會自...

C primer第三章作業

使用恰當的using宣告重做1.4.1節 第11頁 和2.6.2節 第67頁 的練習 ifdef 1 include using std cin using std cout using std endl int main void cout sum endl return0 endif ifdef...

C Primer學習(第三章)

初始化string物件的方式 string s1 string s2 s1 string s2 s1 string s3 value string s3 value string s4 n,c 使用getline讀取一整行 int main int b begin a int e end a 對陣列...