问题描述
我有以下 std :: vector
声明:
std :: vector< ; std :: vector< std :: vector< int> > > m_input;
我正在初始化它如下:
m_input.resize (100);
m_output.resize(100);
for (int i = 0; i < 100; ++i) {
m_input [i].resize(100);
m_output[i].resize(100);
for (int j = 0; j < 100; ++j){
m_input [i][j].resize(100);
m_output[i][j].resize(100);
}
}
如何通过初始化列表?
推荐答案
std :: vector< T& c>有一个构造函数,它接受两个参数,一些元素和一个初始值。在你的情况下,你想用一个
std :: vector< std :: vector< int>的100个副本来初始化
,因此它会 m_input
> :m_input(100,X)
。现在, X
依次是100 std :: vector< int>
的向量,百整数:
std::vector<T>
has a constructor that takes two arguments, a number of elements and an initial value. In your case, you want to initialize m_input
with 100 copies of a std::vector<std::vector<int> >
, so it'd be : m_input(100, X)
. Now, that X
in turn is a vector of 100 std::vector<int>
, which in turn contains a hundred ints:
:m_input(100,std :: vector< std :: vector< int>>(100,std :: vector< ; int>(100,0)))
这篇关于多维向量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!