提问人:denz 提问时间:4/16/2017 最后编辑:denz 更新时间:4/16/2017 访问量:50
是否可以在复制构造期间使用初始值设定项列表初始化向量的值数组?
Is it possible to initialize an array of values for a vector during copy construction using the initializer list?
问:
我想使用复制构造函数的初始值设定项列表将 1 到 9 的值添加到我的向量中,而不是在构造函数的主体中调用 pushElements。这怎么可能?
Hane::Hane(int val, bool veri){
}
Hane::Hane():m_myvalue(0), m_myveri(false) {
pushElements();
}
Cell::~Cell() {}
void Cell::pushElements() {
m_vector = { 1,2,3,4,5,6,7,8,9 };
}
与Hane.h相比
private:
std::vector<int> m_vector;
答:
3赞
zett42
4/16/2017
#1
只需使用采用初始值设定项列表 (8) 的构造函数:
struct myclass
{
std::vector<int> m_vector;
myclass() : m_vector{ 1,2,3,4,5,6,7,8,9 } {}
};
...或者更简单,直接初始化向量:
struct myclass
{
std::vector<int> m_vector{ 1,2,3,4,5,6,7,8,9 };
};
评论