向量的向量的初始化? [英] Initialization of a vector of vectors?

查看:153
本文介绍了向量的向量的初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方式来初始化向量的矢量在相同,快速,你初始化矩阵的方式?

  typedef int type; 

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

vector< vector< type> >矢量矩阵// ???


解决方案

p>

  typedef int类型; 
type elements [] = {0,1,2,3,4,5,6,7,8,9};
vector< int> vec(elements,elements + sizeof(elements)/ sizeof(type));根据您可以使用以下内容:


pre> 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.push_back(row_0_vec);
vectorMatrix.push_back(row_1_vec);

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


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

解决方案

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.

这篇关于向量的向量的初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆