C++ 向量向量的初始化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3061247/
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-28 11:56:10  来源:igfitidea点击:

Initialization of a vector of vectors?

c++stlvectormatrix

提问by Eric

Is there a way to initialize a vector of vectors in the same ,quick, manner as you initialize a matrix?

有没有办法以与初始化矩阵相同的快速方式来初始化向量向量?

typedef int type;

type matrix[2][2]=
{
{1,0},{0,1}
};

vector<vector<type> > vectorMatrix;  //???

采纳答案by sinek

For the single vector you can use following:

对于单个向量,您可以使用以下内容:

typedef int type;
type elements[] = {0,1,2,3,4,5,6,7,8,9};
vector<int> vec(elements, elements + sizeof(elements) / sizeof(type) );

Based on that you could use following:

基于此,您可以使用以下内容:

type matrix[2][2]=
{
   {1,0},{0,1}
};

vector<int> row_0_vec(matrix[0], matrix[0] + sizeof(matrix[0]) / sizeof(type) );

vector<int> row_1_vec(matrix[1], matrix[1] + sizeof(matrix[1]) / sizeof(type) );

vector<vector<type> > vectorMatrix;
vectorMatrix.push_back(row_0_vec);
vectorMatrix.push_back(row_1_vec);

In c++0x, you be able to initialize standard containers in a same way as arrays.

c++0x 中,您可以以与数组相同的方式初始化标准容器。

回答by Brett

std::vector<std::vector<int>> vector_of_vectors;

then if you want to add, you can use this procedure:

那么如果你想添加,你可以使用这个程序:

vector_of_vectors.resize(#rows); //just changed the number of rows in the vector
vector_of_vectors[row#].push_back(someInt); //this adds a column

Or you can do something like this:

或者你可以做这样的事情:

std::vector<int> myRow;
myRow.push_back(someInt);
vector_of_vectors.push_back(myRow);

So, in your case, you should be able to say:

所以,在你的情况下,你应该能够说:

vector_of_vectors.resize(2);
vector_of_vectors[0].resize(2);
vector_of_vectors[1].resize(2);
for(int i=0; i < 2; i++)
 for(int j=0; j < 2; j++)
   vector_of_vectors[i][j] = yourInt;

回答by Mike Seymour

In C++0x, I think you can use the same syntax as for your matrix.

在 C++0x 中,我认为您可以使用与matrix.

In C++03, you have to write some tedious code to populate it. Boost.Assign might be able to simplify it somewhat, using something like the following untested code:

在 C++03 中,您必须编写一些繁琐的代码来填充它。Boost.Assign 可能会稍微简化它,使用类似以下未经测试的代码:

#include <boost/assign/std/vector.hpp>

vector<vector<type> > v;
v += list_of(1)(0), list_of(0)(1);

or even

甚至

vector<vector<type> > v = list_of(list_of(1)(0))(list_of(0)(1));

回答by Umang

If the matrix is completely filled -

如果矩阵完全填充 -

vector< vector<int> > TwoDVec(ROWS, vector<int>(COLS));
//Returns a matrix of dimensions ROWS*COLS with all elements as 0
//Initialize as -
TwoDVec[0][0] = 0;
TwoDVec[0][1] = 1;
..
.

Update : I found there's a better way here

更新:我发现有一个更好的办法在这里

else if there are variable number of elements in each row(not a matrix) -

否则如果每行中有可变数量的元素(不是矩阵) -

vector< vector<int> > TwoDVec(ROWS);
for(int i=0; i<ROWS; i++){
    while(there_are_elements_in_row[i]){           //pseudocode
        TwoDVec[i].push_back(element);
    }
}