简体   繁体   English

使用c ++在多维向量中分配变量的最佳方法是什么?

[英]What is the best way to assign variables in a multidimensional vector using c++?

I have a matrix value like this 我有这样的矩阵值

3   1   30
5   8   1
1   5   2
0   23  7

Currently I know only this way to assign variable to a multidimensional vector. 目前,我只知道将变量分配给多维向量的这种方式。

vector<vector <int> > vec;
vec[0][0]=3;
vec[0][1]=1;
vec[0][2]=30;
.
.
vec[3][2]=7;

For smaller matrix values this is ok but larger values this becomes harder. 对于较小的矩阵值,这是可以的,但较大的值将变得更加困难。 Is there any other way to assign variables in a multidimensional vectors. 还有其他方法可以在多维向量中分配变量。

EDIT 编辑

I am using codeblocks and I am getting this unusual error 我正在使用代码块,但出现此异常错误

#include<iostream>
#include<vector>


int main()
{


    std::vector<std::vector<int> > vec = {{3,1,30},{5,8,1},{1,5,2},{0,23,7}};

    return 0;
}

error 错误

error: could not convert ‘{{3, 1, 30}, {5, 8, 1}, {1, 5, 2}, {0, 23, 7}}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<std::vector<int> >’

Use nested for loops: Example being: 使用嵌套的for循环:示例为:

for (int i = 0; i < MAX_VECTOR_SIZE; i++)
{
    for(int j = 0; j < MAX_SUB_VECTOR_SIZE; j++)
    {
        vec[i][j] = value;
    }
} 

Don't forget to substitute your given values where necessary. 不要忘记在必要时替换给定的值。

std::vector<std::vector<int>> array2d{
    {3 ,  1,   30},
    {5,   8,   1},
    {1  , 5,   2},
    {0 ,  23,  7}
};

You may do 你可以做

const std::vector<std::vector<int>> v = {
    {3,   1,   30},
    {5,   8,   1},
    {1,   5,   2},
    {0,   23,  7}
};

It largely depends upon what you want to do - the "best" way is dependent upon your objective. 它在很大程度上取决于您想做什么-“最佳”方式取决于您的目标。 But using the example given in your question, the most convenient way is via std::initializer_list construction: 但是使用问题中给出的示例,最方便的方法是通过std::initializer_list构造:

std::vector<std::vector<int>> vec = {{3,1,30},{5,8,1},{1,5,2},{0,23,7}};

You can assign the single dimension vector first 您可以先分配一维矢量

vector<int> vec1 {3,1,30};
vector<int> vec2 {5,8,1};
vector<int> vec3 {1,5,2};
vector<int> vec4 {0,23,7};

And then assign the 2d vector 然后分配2d向量

vector<vector<int>> vector2d;
vector2d.push_back(vec1);
vector2d.push_back(vec2);
vector2d.push_back(vec3);
vector2d.push_back(vec4);
  1. If you use manual initialization as in you original code, be careful, you probably want to use vector 's push_back instead of direct call to [] : since your vector is still empty, access via [] will cause segmentation fault. 如果您像在原始代码中那样使用手动初始化,请当心,您可能要使用vectorpush_back而不是直接调用[] :由于矢量仍然为空,因此通过[]访问将导致分段错误。

  2. If you have C++11 use vector 's constructor taking an (aggregate) initializer_list . 如果您具有C++11使用vector的构造函数(并使用一个(汇总) initializer_list This can be handy: 这可能很方便:

     std::vector<std::vector <int> > vec; vec = { {3, 1 , 30}, {5, 8 , 1 }, {1, 5 , 2 }, {0, 23, 7 } }; 

For more details see here . 有关更多详细信息,请参见此处 Here I assume your matrix is in Row-major order, ie each of the internal std::vector<int> represents a row of the matrix. 在这里,我假设您的矩阵按优先顺序排列,即每个内部std::vector<int>代表矩阵的一行。

  1. Finally, IMHO the easiest way is to use a linear algebra library like Eigen if you can. 最后,恕我直言,最简单的方法是使用线性代数库,例如Eigen Eigen provides handy initialization methods: for example using << (see here for examples). Eigen提供了方便的初始化方法:例如,使用<< (有关示例,请参见此处 )。

EDIT 编辑


For you compilation error : make sure that you enable at least C++11 . 对于编译错误 :确保至少启用C++11 If you use g++ add -std=c++11 to your compiler's tag. 如果使用g++则将-std=c++11添加到编译器的标记中。

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

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