我定义了两个struct
class foo() {
public:
struct spatialEntry {
bool block;
CoherenceState_t state;
};
struct accumulationEntry {
uint64_t offset;
std::vector<spatialEntry> pattern;
};
std::vector<int> v;
std::list<accumulationEntry> accumulationTable;
foo() {
v.resize(16);
}
};
现在我想初始化 std::vector<spatialEntry>
的大小到 16 喜欢 v
.我该怎么做?
最佳答案
只需为包含该成员的类定义一个构造函数,然后将 resize()
定义为:
class foo() {
public:
//...
struct accumulationEntry
{
uint64_t offset;
std::vector<spatialEntry> pattern;
accumulationEntry()
{
pattern.resize(16); //<--------- this is what you want?
}
};
std::vector<int> v;
std::list< accumulationEntry > accumulationTable;
foo()
{
v.resize(16);
}
};
但是如果你使用 resize
,那么最好这样做:
accumulationEntry() : pattern(16) //<--- note this
{
//pattern.resize(16);
}
即,使用成员初始化列表。对 foo
也做同样的事情。
关于c++ - 调整结构中的 vector 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13978990/