简体   繁体   English

如何初始化未知大小的多维向量,C ++

[英]How initialize multi dimensional vectors of an unknown size, C++

in my project I would need create variable multidimensional matrix by std:vector with variable size. 在我的项目中,我需要通过std:vector创建可变多维矩阵,并且可变大小。 I found correctly code below, which can create this: 我在下面找到了正确的代码,可以创建:

template<typename T, std::size_t N>
struct md_vector
{
    using type = std::vector<typename md_vector<T, N - 1>::type>;
};

 template<typename T>
 struct md_vector<T, 1>
 {
    using type = std::vector<T>;
 };

 Template<typename T, std::size_t N>
 using md_vector_t = typename md_vector<T, N>::type;


//using
//this create 3 dimensional vectors of int - std:vector<std:vector<std:vector<int>>>
 md_vector_t<int, 3> matrix;

But now i dont know ho use this matrix? 但是现在我不知道怎么用这个矩阵? I would like initialize whole matrix to 0....by example 4 dimensional matrix, each vector 7 integers.... 7 x 7 x 7 x 7 integers ? 我想将整个矩阵初始化为0 ....通过示例4维矩阵,每个向量7个整数.... 7 x 7 x 7 x 7整数? And after initialize how i can assign value? 初始化后如何分配值? matrix[0][0][0][0] - this i cannot, because I dont know dimension size. matrix [0] [0] [0] [0] - 我不能,因为我不知道尺寸大小。

Thank you very much for some advice... 非常感谢您的一些建议......

The easiest way to deal with this insane situation is to use resize() . 处理这种疯狂情况的最简单方法是使用resize() Let the compiler figure out how to initialize the vector: 让编译器弄清楚如何初始化向量:

md_vector_t<int, 4> matrix;

matrix.resize(7);

This is going to resize the first dimension. 这将调整第一个维度的大小。 Now, you need to resize each individual element in the first dimension, in order to construct the second dimension. 现在,您需要调整第一维中每个单独元素的大小,以构建第二维。

for (auto &a:matrix)
    a.resize(7);

Before soon, you figure out how to let range iteration work for you. 在此之前,您将弄清楚如何让范围迭代为您工作。 Something like this: 像这样的东西:

md_vector_t<int, 4> matrix;

matrix.resize(7);

for (auto &a:matrix)
{
    a.resize(7);
    for (auto &b:a)
    {
        b.resize(7);
        for (auto &c:b)
        {
             c.resize(7);
        }
    }
}

Counting this off on my fingers, I'm pretty fairly certain this'll initialize all four dimensions here. 把它计算在我的手指上,我非常确定这会在这里初始化所有四个维度。

Before C++11, this would've been agony. 在C ++ 11之前,这将是痛苦的。

Now, I'm not sure exactly why you say "matrix[0][0][0][0] - this i cannot, by i dont know dimension size". 现在,我不确定你为什么说“matrix [0] [0] [0] [0] - 我不能,因为我不知道尺寸大小”。 Because that's exactly how you would go around peeking and poking at this sucker. 因为这就是你如何偷看和戳戳这个傻瓜。

Each element in the matrix will be value-constructed to 0. If you want to initialize the values to something else, just use the same nested range iteration approach to initialize the values. 矩阵中的每个元素都将构造为0.如果要将值初始化为其他值,只需使用相同的嵌套范围迭代方法来初始化值。

Now, as far as a template for initializing a matrix of arbitrary dimensions, it should be trivial to take this same approach and use the same approach as your existing template: a specialization for a 1-dimensional vector, and a the default template that invokes resize() , then performs range iteration to recursively invoke itself on the values in its container. 现在,就初始化任意维矩阵的模板而言,采用相同的方法并使用与现有模板相同的方法应该是微不足道的:一维向量的专门化,以及调用的默认模板resize() ,然后执行范围迭代,以递归方式调用其容器中的值。

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

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