向量向量的初始化?

发布于 2024-09-06 08:22:44 字数 172 浏览 6 评论 0原文

有没有一种方法可以像初始化矩阵一样快速地初始化向量向量?

typedef int type;

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

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

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;  //???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萤火眠眠 2024-09-13 08:22:44

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

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

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

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);

c 中++0x,您可以像数组一样初始化标准容器。

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.

皓月长歌 2024-09-13 08:22:44
std::vector<std::vector<int>> vector_of_vectors;

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

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

或者你可以这样做:

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

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

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;
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;
陪我终i 2024-09-13 08:22:44

在 C++0x 中,我认为您可以使用与矩阵相同的语法。

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

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

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

或者甚至

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

In C++0x, I think you can use the same syntax as for your 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:

#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));
遗失的美好 2024-09-13 08:22:44

如果矩阵完全填充 -

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;
..
.

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

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

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文