C++ 多维向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/823562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:27:48  来源:igfitidea点击:

Multi-dimensional vector

c++vector

提问by Ari

How can I create a 2D vector? I know that in 2D array, I can express it like:

如何创建二维矢量?我知道在二维数组中,我可以这样表达:

a[0][1]=98;
a[0][2]=95;
a[0][3]=99;
a[0][4]=910;

a[1][0]=98;
a[1][1]=989;
a[1][2]=981;
a[1][3]=987;

How can one do this using the C++ STL Vector?

如何使用 C++ STL Vector 做到这一点?

回答by Ari

vector<vector<int> > a;

vector<vector<int> > a;

If you want to define the rows and columns,

如果要定义行和列,

vector<vector<int> > a{{11, 2, 4}, {4, 5, 6}, {10, 8, -12}};

vector<vector<int> > a{{11, 2, 4}, {4, 5, 6}, {10, 8, -12}};

回答by David Rodríguez - dribeas

std::vector< std::vector< int > > a; // as Ari pointed

Using this for a growing matrix can become complex, as the system will not guarantee that all internal vectors are of the same size. Whenever you grow on the second dimension you will have to explicitly grow all vectors.

将此用于增长矩阵可能会变得复杂,因为系统将无法保证所有内部向量的大小相同。每当您在第二维上增长时,您都必须明确地增长所有向量。

// grow twice in the first dimension
a.push_back( vector<int>() );
a.push_back( vector<int>() );

a[0].push_back( 5 ); // a[0].size() == 1, a[1].size()==0

If that is fine with you (it is not really a matrix but a vector of vectors), you should be fine. Else you will need to put extra care to keep the second dimension stable across all the vectors.

如果这对你没问题(它不是一个真正的矩阵,而是一个向量的向量),你应该没问题。否则,您将需要格外小心,以保持所有向量的第二维稳定。

If you are planing on a fixed size matrix, then you should consider encapsulating in a class and overriding operator() instead of providing the double array syntax. Read the C++ FAQ regarding this here

如果您计划使用固定大小的矩阵,那么您应该考虑封装在一个类中并覆盖 operator() 而不是提供双数组语法。在此处阅读有关此的 C++ 常见问题解答

回答by aJ.

std::vector< std::vector<int> > a;

    //m * n is the size of the matrix

    int m = 2, n = 4;
    //Grow rows by m
    a.resize(m);
    for(int i = 0 ; i < m ; ++i)
    {
        //Grow Columns by n
        a[i].resize(n);
    }
    //Now you have matrix m*n with default values

    //you can use the Matrix, now
    a[1][0]=98;
    a[1][1]=989;
    a[1][2]=981;
    a[1][3]=987;

//OR
for(i = 0 ; i < m ; ++i)
{
    for(int j = 0 ; j < n ; ++j)
    {      //modify matrix
        int x = a[i][j];
    }

}

回答by Beno?t

If you don't haveto use vectors, you may want to try Boost.Multi_array. Here is a linkto a short example.

如果你不具备使用载体,你可能想尝试Boost.Multi_array。这是一个简短示例的链接

回答by IluxaKuk

Declaration of a matrix, for example, with 5 rows and 3 columns:

矩阵的声明,例如,5 行 3 列:

vector<vector<int> > new_matrix(5,vector<int>(3));

Another way of declaration to get the same result as above:

获得与上述相同结果的另一种声明方式:

vector<int> Row;    

One row of the matrix:

矩阵的一行:

vector<Row> My_matrix;

My_matrix is a vector of rows:

My_matrix 是行向量:

My_matrix new_matrix(5,Row(3)); 

回答by Igor Oks

As Ari pointed, vector< vector< int>>is the right way to do it.

正如 Ari 指出的那样,这vector< vector< int>>是正确的做法。

In addition to that, in such cases I always consider wrapping the inner vector (actually, whatever it represents) in a class, because complex STL structures tend to become clumsy and confusing.

除此之外,在这种情况下,我总是考虑将内部向量(实际上,无论它代表什么)包装在一个类中,因为复杂的 STL 结构往往会变得笨拙和混乱。

回答by Aditya Goel

Just use the following method to use 2-D vector.

只需使用以下方法来使用二维向量。

int rows, columns;        

// . . .

vector < vector < int > > Matrix(rows, vector< int >(columns,0));

                                  Or

vector < vector < int > > Matrix;
Matrix.assign(rows, vector < int >(columns, 0));

// Do your stuff here...
int rows, columns;        

// . . .

vector < vector < int > > Matrix(rows, vector< int >(columns,0));

                                  Or

vector < vector < int > > Matrix;
Matrix.assign(rows, vector < int >(columns, 0));

// Do your stuff here...

This will create a Matrix of size rows * columns and initializes it with zeros because we are passing a zero(0) as a second argument in the constructor i.e vector < int > (columns, 0).

这将创建一个大小为 rows * columns 的矩阵并用零初始化它,因为我们在构造函数中传递了一个零(0)作为第二个参数,即 vector < int > (columns, 0)。

回答by Robert S. Barnes

dribeas' suggestion is really the way to go.

dribeas 的建议确实是要走的路。

Just to give a reason why you might want to go the operator() route, consider that for instance if your data is sparse you can lay it out differently to save space internally and operator() hides that internal implementation issue from your end user giving you better encapsulation and allowing you to make space or speed improving changes to the internal layout later on without breaking your interface.

只是为了说明您可能想要使用 operator() 路线的原因,请考虑例如如果您的数据稀疏,您可以将其布局不同以节省内部空间,而 operator() 则向您的最终用户隐藏了该内部实现问题您可以更好地封装,并允许您在不破坏界面的情况下腾出空间或加快对内部布局的更改。