以下のコードはC++の本から入手したものですが、初期化がどのように機能するのか理解できません。
私が見ることができるものから、行を通るループサイクリングの外側と、列を通る内側のループサイクリングがあります。しかし、それは私が理解していない配列への値の割り当てです。
#include <iostream>
using namespace std;
int main()
{
int t,i, nums[3][4];
for(t=0; t < 3; ++t) {
for(i=0; i < 4; ++i) {
nums[t][i] = (t*4)+i+1; //I don't understand this part/line
cout << nums[t][i] << ' ';
}
cout << '\n';
}
return 0;
}
だからここにいくつかの質問があります。
- 2Dint配列の初期化がわかりません
nums[3][4]
。(t*4)+i+1
コンパイラが何をどこに割り当てるかを認識できるように、を分離するものは何ですか? - 割り当てられている値に基づいて、行と列に格納される値を知るにはどうすればよいですか?
- アスタリスクがあるのはなぜですか?
- かっこは何の
t*4
ためにありますか?
初期化2次元配列は次の例のように見えることを理解しています。
#include <iostream>
using namespace std;
int main() {
char str[3][20] = {{"white" "rabbit"}, {"force"}, {"toad"}}; //initialize 2D character array
cout << str[0][0] << endl; //first letter of white
cout << str[0][5] << endl; //first letter of rabbit
cout << str[1][0] << endl; //first letter of force
cout << str[2][0] << endl; //first letter of toad
return 0;
}
そして、私が知っていることから、このように記憶に残っています。
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 w h i t e r a b b i t 0
1 f o r c e 0
2 t o a d 0
ありがとうございました。