C++数组
发布时间 :
字数:371
阅读 :
创建一个vector
vector <向量元素类型> x={元素1, 元素2, ..}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| vector<vector<int>> x(3, vector<int>(4));
vector<vector<int>> data = { {7, 4, 0, 8}, {2, 0, 3, 5}, {6, 1, 7, 0}, };
cin >> data.at(0).at(3); cout << data.at(0).at(3);
cout << data.size();
cout << data.at(0).size();
|
长度不一的vector
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| vector<vector<int>> data(3);
data.at(0).push_back(1); data.at(0).push_back(2); data.at(0).push_back(3);
data.at(1).push_back(4); data.at(1).push_back(5); data.at(1).push_back(6); data.at(1).push_back(7);
data.at(2).push_back(8); data.at(2).push_back(9);
for (int i = 0; i < 3; i++) { for (int j = 0; j < data.at(i).size(); j++) cout << data.at(i).at(j) << ","; cout<< endl; }
|
输出
其他数组array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| vector<int> data(3); array<int, 3> data; int data[3];
vector<vector<int>> data(3, vector<int>(4)); cout << data.at(1).at(2);
array<array<int, 4>, 3> data = {}; cout << data.at(1).at(2);
int data[3][4] = {}; cout << data[1][2];
|
转载请注明来源 https://tianweiye.github.io