简体   繁体   English

如何用C++中的大小初始化3维向量

[英]How to initialize 3 dimension vector with the size in C++

For example, if we initialize vector<vector<vector<double>>> f , the dimension in each direction is not specified.例如,如果我们初始化vector<vector<vector<double>>> f ,则未指定每个方向的维度。 So, I am wondering, what command should we insert to make f have size of [3][4][5] .所以,我想知道,我们应该插入什么命令来使f大小为[3][4][5] Do we use new or something else?我们使用新的还是其他的?

Thanks for helping me out!谢谢你帮我解决!

vector<vector<vector<double>>> f(3, vector<vector<double>>(4, vector<double>(5)));

If you have already declared the vector and you want to initialize it, this is one way you can do it:如果你已经声明了向量并且你想初始化它,这是你可以做到的一种方法:

vector<vector<vector<double>>> f;
f = vector<vector<vector<double>>>(3, vector<vector<double>>(4, vector<double>(5)));
std::vector<std::vector<std::vector<double>>> f(3, std::vector<std::vector<double>>(4, std::vector<double>(5,0.0)));

For anyone who wants it without namespace std.对于任何想要没有命名空间 std 的人。 Also the values in this f[3][4][5] are initialised to 0.0 which might be useful.此 f[3][4][5] 中的值也被初始化为 0.0,这可能很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM