C ++中具有用户定义大小的2D向量
向量的向量称为2D向量。
算法
Begin
Declare a variable v to the 2D vector type.
Initialize values to the vector v.
Print “the 2D vector is:”.
for (int i = 0; i < v.size(); i++)
for (int j = 0; j < v[i].size(); j++)
print the value of 2D vector v[i][j].
End.示例
#include <iostream>
#include <vector> //header file for 2D vector in C++
using namespace std;
int main() {
vector<vector<int> > v{ { 4,5, 3, 10 }, // initializing 2D vector with values.
{ 2, 7, 11 },
{ 3, 2, 1, 12 } };
cout<<"the 2D vector is:"<<endl;
for (int i = 0; i < v.size(); i++) { // printing the 2D vector.
for (int j = 0; j < v[i].size(); j++)
cout << v[i][j] << " ";
cout << endl;
}
return 0;
}输出结果
the 2D vector is: 4 5 3 10 2 7 11 3 2 1 12
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短