C++自修入門實境秀、C++ Primer 5版研讀秀 34/ ~v6函式-6.2.5.main Handling Command-Line O...





Passing a Multidimensional Array

傳入一個多維陣列

1:30

Instead, what appears to be a multidimensional array is an array of arrays.

所謂多謂陣列其實就是一個陣列的陣列

其實就是一個含有至少一個陣列的陣列



3:00:00 3:1:10

// matrix points to the first element in an array whose elements are arrays of ten ints

void print(int (*matrix)[10], int rowSize) { /* . . . */ }

declares matrix as a pointer to an array of ten ints.

We can also define our function using array syntax. As usual, the compiler ignores the first dimension, so it is best not to include it:

我們也能使用陣列語法定義我們的函式。一如以往,編譯器會忽略第一個維度,所以最好是不要包括它:

// equivalent definition

void print(int matrix[][10], int rowSize) { /* . . . */ }

declares matrix to be what looks like a two-dimensional array. In fact, the parameter is a pointer to an array of ten ints.





17:30

6.2.5. main: Handling Command-Line Options

6.2.5 main :處理命令列選項

main函式 28:40

main若有參數,其參數傳遞,即是陣列參數的範例



頁219

22:10

練習6.21

29:40

int returnLarger(int i, int* ip)

{

if (i >= *ip)

{

return i;

}

else

return *ip;

}

int main()

{

int i = 3,j=33; int* ip = &j;

cout << returnLarger(i, ip) << endl;

return 0;

}

練習6.22

41:20

void swap_two_pointers(int *& i, int *& j)

{

int* q = i;

i = j;

j = q;

}

int main()

{

int i = 3,j=33;

int* ip = &i, * jp = &j;;

swap_two_pointers(ip, jp);

return 0;

}

Keep Visual Studio nice and tidy

1:4:50 https://youtu.be/LjgAYSuzKEE

不想在開啟Visual Studio 時載入上次關閉前的狀態可到option內設定

1:12:40

Reset tool windows 擴充功能



練習6.23

1:19:50

void print(const int* cp)

{

cout << *cp << endl;

}

int main()

{

int i = 0, j[2] = { 0, 1 };

print(&i);

print(j);

return 0;

}

1:31:58

void print(const int* beg,const int*end)// Using the Standard Library Conventions

{

while (beg!=end)

{

cout << *beg++ << endl;

}

}

int main()

{

int i = 0, j[2] = { 0, 1 };

print(&i,(&i)+1);//可見即使是int型別也能當作陣列(指針)來處理

//指標指針(pointer)+1 即向前移動一個位置

print(begin(j),end(j)); // Using the Standard Library Conventions

return 0;

1:43:40

void print(const int ia[], size_t sz)//Explicitly Passing a Size Parameter

{

for (size_t i = 0; i < sz; i++)

{

cout << ia[i] << endl;

}

}

int main()

{

int i = 0, j[2] = { 0, 1 };

print(&i,1);//可見指標指針(pointer)是完全可以當作陣列來處理

//參數是個陣列型別,可我們傳的是一個指標。

print(j,size(j));

return 0;

}

1:56:00

void print_Array_Reference_Parameters(const int(&arr)[2])

{

for (int i : arr)

{

cout << i << endl;

}

}



int main()

{

int i = 0, j[2] = { 0, 1 };

print_Array_Reference_Parameters(j);

return 0;

2:35:50

void print_Passing_a_Multidimensional_Array_ref(const int(&arr)[2][2])

{

//for (const auto &a:arr)

for (const auto &a:arr)

{

for (auto ae : a)

cout << ae << endl;

}

}

int main()

{

int i = 0, j[2][2] = { {0,1},{ 1,0} };

print_Passing_a_Multidimensional_Array_ref(j);

return 0;

}

2:47:49 3:6:40

void print_Passing_a_Multidimensional_Array_pointer(const int(*arr)[2],int rowSize)

{

for (size_t i = 0; i < rowSize; i++)

{

for (int a :arr[i])

{

cout << a << endl;

}

}



}

void print_Passing_a_Multidimensional_Array_pointer_size_t(const int(*arr)[2], size_t rowSize)

{

for (size_t i = 0; i < rowSize; i++)

{

for (int a : arr[i])

{

cout << a << endl;

}

}

}

void print_Passing_a_Multidimensional_Array_multiArr(const int arr[][2], int rowSize)

{

for (size_t i = 0; i < rowSize; i++)

{

for (int a : arr[i])

cout << a << endl;

}

}

int main()

{

int i = 0, j[2][2] = { {0,1},{ 1,0} };

print_Passing_a_Multidimensional_Array_pointer_size_t(j,size(j));

return 0;

}

標頭檔Chapter6.h:

void print_Passing_a_Multidimensional_Array_pointer(const int (*arr)[2], int rowSize);//練習6.23

void print_Passing_a_Multidimensional_Array_pointer_size_t(const int (*arr)[2], size_t rowSize);//練習6.23

//這裡的int 是陣列元素的型別,不是陣列的大小,所以不能是unsigned,而須是元素的型別(在這裡是int)。因為二維陣列可看作是row和column,所以才叫rowSize

//而因為指標沒有指定大小,猶如下式第一維度大小的省略,所以須另外再給個第一維大小的參數

//而因為第一維元素的型別為陣列,所以……(還是懷疑這裡int,應該是size_t 的unsinged比較對,因為要告知大小,才能做迭代(iterate)也。

//果然用size_t也可以,且應該比較對,否則如果是負值,這迭代(iterate)的迴圈怎麼寫才好?

void print_Passing_a_Multidimensional_Array_multiArr(const int arr[][2],int rowSize);//練習6.23

3:47:30 3:52:00

VisualStudio 2019

讓c++的程式碼進化、簡潔:

Need to auto-modernize your code to C++11/14/17? Want to discover latent bugs and issues in your code by leveraging LLVM's static analyzer and C++ Core Guidelines?

Unleash the power of clang-tidy on your Visual C++ projects with the Clang Power Tools VS extension: http://msft.social/OmWt4p

4:1:20

練習6.24

4:5:00 新增一個cpp檔(源碼檔(source file))

並沒有問題,會將陣列中的元素一一印出

留言

熱門文章