我可以建立一个重复序列构造
vector<vector<int> >
使用重复序列构造?
我知道我可以建立一个像
vector<int> v(10, 0);
,但我不知道如何建立使用这种相同的方法载体的载体。谢谢!
我可以建立一个重复序列构造
vector<vector<int> >
使用重复序列构造?
我知道我可以建立一个像
vector<int> v(10, 0);
,但我不知道如何建立使用这种相同的方法载体的载体。谢谢!
只要它传递一个矢量作为第二个参数:
// 10 x 10 elements, all initialized to 0
vector<vector<int> > v(10, vector<int>(10, 0));
vector<vector<int> > v(10, vector<int>(30, 0));
将创建与每个30零10层的载体。
explicit vector (size_type n, const T& value= T(), const Allocator& = Allocator());
重复序列构造:初始化向量与其内容集合到一个重复,n
次,value
副本。
vector< vector<int> > vI2Matrix(3, vector<int>(2,0));
创建3个向量,每个向量2个零。
本质上,可以使用向量矢量来表示二维数组。
下面是一个源代码示例:
#include <iostream>
#include <vector>
using namespace std;
main()
{
// Declare size of two dimensional array and initialize.
vector< vector<int> > vI2Matrix(3, vector<int>(2,0));
vI2Matrix[0][0] = 0;
vI2Matrix[0][1] = 1;
vI2Matrix[1][0] = 10;
vI2Matrix[1][1] = 11;
vI2Matrix[2][0] = 20;
vI2Matrix[2][1] = 21;
cout << "Loop by index:" << endl;
int ii, jj;
for(ii=0; ii < 3; ii++)
{
for(jj=0; jj < 2; jj++)
{
cout << vI2Matrix[ii][jj] << endl;
}
}
return 0;
}