如何为 3D 矢量声明多个值
How do I declare multiple values for a 3D vector?
我有 typedef typedef std::vector<std::vector<std::vector<double>>> three_d_vector;
来声明 3D 向量;但我想知道如何,如果可以的话,如何在批量列表中声明各个值,就像数组一样。
假设您正在研究 C++11,并且您所要求的是初始化向量的直接方法:
#include <vector>
using namespace std;
int main()
{
typedef vector<vector<vector<double>>> three_d_vector;
three_d_vector v =
{
{
{ 1, 2, 3 }, { 4, 5, 6 }
},
{
{ 0, 0, 1 }, { 1, 0, 0 }
},
{
{ 4, 0, 1 }, { 1, 0, 5 }
},
};
}
但是,请注意,以这种方式定义 3D 矢量将使您能够自由地为每个维度使用不同的矢量大小。换句话说,这将是合法的:
three_d_vector v =
{
{
{ 1, 2, 3, 7 }, { 4, 5, 6 }
},
{
{ 0, 0, 1 }, { 1, 0, 0, 8, 9, 15 }
},
{
{ 4, 0, 1 }, { 1, 0 }
},
};
这是需要的,可接受的还是不可接受的取决于你的用例;无论如何,我想你可能想看看Boost.MultiArray。
您需要
在 C++11 模式下编译(例如,使用 gcc 的命令行开关-std=c++11
)并使用如下语法:
three_d_vector vec = { { {1., 2.}, {3., 4., 17., 25.} },
{ {5., 6., 12.}, {7., 8.}, {0., 22.} },
{ {9., 10.}, {11.}, {45.}, {33.}, {37., 11.} };
使用std::vector<std::vector<std::vector<double>>>
不会非常连贯。请改用Boost.MultiArray
。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 无法将结构注册为增强几何体3D点
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- 使用 vector 在 c++ 中声明 3D 数组结构
- 使用vector的c++ 3D数组声明
- 用blitz++声明3d数组
- 在c++中如何在3D维向量中声明3D维向量
- 如何为 3D 矢量声明多个值
- 动态3d数组的声明和赋值