【发布时间】:2020-12-04 18:05:53
【问题描述】:
我想在构造struct 时设置成员向量的大小。原因是我发现resize() 可能很慢。
这里有一些最小的代码试图做我想做的事,但是坏了。
#include <iostream>
#include <vector>
using namespace std;
struct Struct_w_Vector {
~Struct_w_Vector() {} // destructor
int n;
vector<double> h(n, 0.0);
Struct_w_Vector(int n) : n(n) {
// create vector h of size n filled with 0.0 now because resize() takes much more time
} // constructor
};
int main() {
vector<double> h(10, 0.0); // what I would like to make happen inside of the struct
Struct_w_Vector v(10);
return 0;
}
是否可以将名为h 的double 的vector 的大小设置为n 在构造时填充全0(不使用调整大小)?
感谢您的宝贵时间。
【问题讨论】: