c++三维静态数组的定义与作为函数的传递
生活随笔
收集整理的這篇文章主要介紹了
c++三维静态数组的定义与作为函数的传递
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在c++中,我們可以定義三維數組,并且可以將之作為參數直接傳遞。
定義:
#include <iostream> #include <windows.h> using namespace std; const int x = 10; const int y = 10; const int z = 10; int main() {double foo[x][y][z];for (int i = 0; i < x; i++) {for (int j = 0; j < y; j++) {for (int k = 0; k < z; k++) {foo[i][j][k] = 1.0;}}}cout << foo[0][0][0] << endl; // 1.0system("pause");return 0; }如上所示,我們設置的是靜態數組,所以必須在定義三維數組之前確定其大小,為了程序的可維護性,建議使用const int進行定義。
?
將三維數組作為參數傳遞:
#include <iostream> #include <windows.h> using namespace std; const int x = 10; const int y = 10; const int z = 10; int bar(double arr[][y][z]); int main() {double foo[x][y][z];for (int i = 0; i < x; i++) {for (int j = 0; j < y; j++) {for (int k = 0; k < z; k++) {foo[i][j][k] = 1.0;}}}cout << foo[0][0][0] << endl; // 1.0 bar(foo);system("pause");return 0; } int bar(double arr[][y][z]) {cout << "function invoked value: " << arr[1][1][1] << endl;return 0; }如上所示,最終結果為:
1 function invoked value1注意,在傳遞三維數組作為參數時,數組的第一個[]中為空,而第二第三個不能為空。
這樣,對于大部分情況下的三維數組就可以輕松處理了。
轉載于:https://www.cnblogs.com/zhuzhenwei918/p/8849644.html
總結
以上是生活随笔為你收集整理的c++三维静态数组的定义与作为函数的传递的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个栈桢对应着一个方法
- 下一篇: 对做的一个小的C++聊天室的总结