C primer 習題 第三章 31 45

2021-08-11 08:18:15 字數 3867 閱讀 1989

int main()

#include 

using

std::vector;

int main()

不初始化scores,則scores裡面所有元素的初始值將是未定義。

p1 += p2 - p1;

功能是將p1指向p2指向的元素。

當p1是常量指標時非法。

int main()

return

0;}

#include 

#include

using

std::cout;

using

std::endl;

using

std::vector;

int main()

for (int i = 0; i < 10; i++)

if (arr1[i] != arr2[i])

issame = false;

cout

<< (issame ? "yes" : "no") << endl;

vector

ivec1, ivec2;

for (int i = 0; i < 10; i++)

cout

<< (ivec1 == ivec2 ? "yes" : "no") << endl;

return

0;}

const

char ca = ;

const

char *cp = ca;

while (*cp)

將字串列印出來,最後沒有空字元,所以不會跳出迴圈。

兩指標(同一陣列)相減表示之間相差距離,指標加上乙個整數表示指標移動到某一位置,兩個指標相加並沒有實際意義。

#include 

#include

using

std::cin;

using

std::cout;

using

std::endl;

using

std::string;

int main()

cout

<< (issame ? "yes" : "no") << endl;

string str1, str2;

str1 = "hello";

str2 = "hello";

cout

<< (str1 == str2 ? "yes" : "no") << endl;

return

0;}

#include 

#include

using

std::cin;

using

std::cout;

using

std::endl;

int main()

#include 

#include

using

std::cin;

using

std::cout;

using

std::endl;

using

std::vector;

int main()

return

0;}

#include 

#include

using

std::cin;

using

std::cout;

using

std::endl;

using

std::vector;

int main()

for (auto ix : arr)

return

0;}

#include 

using

std::cin;

using

std::cout;

using

std::endl;

int main()

; //1

for (int(&i)[4] : arr)

for (int j : i)

cout

<< j << " ";

cout

<< endl;

//2for (int i = 0; i < 3; i++)

for (int j = 0; j < 4; j++)

cout

<< arr[i][j] << " ";

cout

<< endl;

//3for (int(*i)[4] = arr; i != arr + 3; i++)

for (int *j = *i; j != *i + 4; j++)

cout

<< *j << " ";

cout

<< endl;

return

0;}

#include 

using

std::cin;

using

std::cout;

using

std::endl;

int main()

; using int_arr = int[4];

//1for (int_arr &i : arr)

for (int j : i)

cout

<< j << " ";

cout

<< endl;

//2for (int i = 0; i < 3; i++)

for (int j = 0; j < 4; j++)

cout

<< arr[i][j] << " ";

cout

<< endl;

//3for (int_arr *i = arr; i != arr + 3; i++)

for (int *j = *i; j != *i + 4; j++)

cout

<< *j << " ";

cout

<< endl;

return

0;}

#include 

using

std::cin;

using

std::cout;

using

std::endl;

int main()

; using int_arr = int[4];

//1for (auto &i: arr)

for (auto &j : i)

cout

<< j << " ";

cout

<< endl;

//2for (auto i = 0; i < 3; i++)

for (auto j = 0; j < 4; j++)

cout

<< arr[i][j] << " ";

cout

<< endl;

//3for (auto i = arr; i != arr + 3; i++)

for (auto j = i; j != i + 4; j++)

cout

<< *j << " ";

cout

<< endl;

return

0;}

C primer 習題 第三章 1 10

using std cin using std cout using std endl using std cerr include include using std cin using std cout using std endl using std string int main void ...

C primer 第三章備忘。

1 處理每個字元?使用基於範圍的for語句 for declaration expression statement 其中expression部分是乙個物件,表示乙個序列 declaration部分負責定義乙個變數,用以訪問序列中的基礎元素 例子 string str some string for...

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物件會自...