是否可以创建矢量矢量? [英] Is it possible to create a vector of vector?

查看:55
本文介绍了是否可以创建矢量矢量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



新手提醒:我来自C方面,努力学习C ++。


我需要一个二维数据结构。我首先尝试了一个常规的

向量,并用我自己的结构和指针添加了第二维。我的b $ b无法使它工作。太多的非法任务。


我的下一次尝试是创建一个矢量矢量(或矢量列表,

等)。编译器(MSVC ++)似乎不喜欢这样的结构。我想b $ b猜我可以试试gcc,但是让我们听听专家的意见。


我们非常欢迎您的建议。


-RFH


Newbie alert: I come from the C side, struggling to learn C++.

I need a two-dimensional data structure. I first tried a regular
vector and added the 2nd dimension with my own structs and pointers. I
couldn''t make it work. Too many illegal assignments.

My next attempt was to create a vector of vector (or list of vector,
etc.). The compiler (MSVC++) doesn''t seem to like such structure. I
guess I could try gcc, but let''s hear the experts'' opinions.

Your suggestions are most welcome.

-RFH

推荐答案

Ramon F Herrera写道:
Ramon F Herrera wrote:

我的下一次尝试是创建一个矢量矢量(或矢量列表,

等)。
My next attempt was to create a vector of vector (or list of vector,
etc.).



这似乎是一种非常合理的方法。

This seems a very reasonable approach.


编译器(MSVC ++)没有'似乎不喜欢这样的结构。
The compiler (MSVC++) doesn''t seem to like such structure.



你能发一个证明问题的小例子。


你能否更具体地解决这个问题你有。

LR

Can you please post a small example that demonstrates the problem.

Can you also be more specific about the problem that you''re having.
LR


2月23日23:38,Ramon F Herrera< ra ... @ conexus。 netwrote:
On 2 jun, 23:38, Ramon F Herrera <ra...@conexus.netwrote:

新手警报:我来自C方面,努力学习C ++。


我需要一个两个 - 维度数据结构。我首先尝试了一个常规的

向量,并用我自己的结构和指针添加了第二维。我的b $ b无法使它工作。太多的非法任务。


我的下一次尝试是创建一个矢量矢量(或矢量列表,

等)。编译器(MSVC ++)似乎不喜欢这样的结构。我想b $ b猜我可以试试gcc,但是让我们听听专家的意见。


我们非常欢迎您的建议。


-RFH
Newbie alert: I come from the C side, struggling to learn C++.

I need a two-dimensional data structure. I first tried a regular
vector and added the 2nd dimension with my own structs and pointers. I
couldn''t make it work. Too many illegal assignments.

My next attempt was to create a vector of vector (or list of vector,
etc.). The compiler (MSVC++) doesn''t seem to like such structure. I
guess I could try gcc, but let''s hear the experts'' opinions.

Your suggestions are most welcome.

-RFH



Mi english不太好。我试图解释。


例如:


std :: vector< std :: vector< int myVector; //这会创建一个向量

整数向量


现在,你可以这样做:


std :: vector< intvector;

vector.push_back(1);


myVector.push_back(vector);


然后:


std :: cout<< myVector [0] [0]<<的std :: ENDL; //这个打印1


如果你想初始化一个固定大小的矢量矢量,请使用

以下行:


std :: vector< std :: vector< int myVector(number_of_rows,

std :: vector< int>(number_of_colums,default_value));


http://www.sgi.com/tech/stl/Vector.html 了解

构造函数的调用。


我希望你理解。


问候

-

Mauro Ciancio

Mi english is not so good. Im trying to explain.

For example:

std::vector< std::vector<int myVector; // this creates a vector of
vector of integers

Now, you can do this:

std::vector<intvector;
vector.push_back(1);

myVector.push_back(vector);

then:

std::cout << myVector[0][0] << std::endl; // this prints 1

If you want to initialize a vector of vector with a fixed size use the
following line:

std::vector< std::vector<int myVector (number_of_rows,
std::vector<int>(number_of_colums, default_value));

Read http://www.sgi.com/tech/stl/Vector.html to understand the
constructor calls.

I hope you understand.

Regards
--
Mauro Ciancio


Ramon F Herrera写道:
Ramon F Herrera wrote:

新手警报:我来自C方面,努力学习C ++。


我需要一个二维数据结构。我首先尝试了一个常规的

向量,并用我自己的结构和指针添加了第二维。我的b $ b无法使它工作。太多的非法任务。


我的下一次尝试是创建一个矢量矢量(或矢量列表,

等)。编译器(MSVC ++)似乎不喜欢这样的结构。我想b $ b猜我可以试试gcc,但是让我们听听专家的意见。


我们非常欢迎您的建议。


-RFH
Newbie alert: I come from the C side, struggling to learn C++.

I need a two-dimensional data structure. I first tried a regular
vector and added the 2nd dimension with my own structs and pointers. I
couldn''t make it work. Too many illegal assignments.

My next attempt was to create a vector of vector (or list of vector,
etc.). The compiler (MSVC++) doesn''t seem to like such structure. I
guess I could try gcc, but let''s hear the experts'' opinions.

Your suggestions are most welcome.

-RFH



#include< vector>

int main(int argc,char * argv []){

std :: vector< std :: vector< int vectovect;

vectovect.resize(20);

for(std :: vector< std :: vector< int :: iterator i = vectovect.begin();

i!= vectovect.end(); ++ i){

i->调整大小(30);

}

}


-

Daniel Pitts的'科技博客:< http://virtualinfinity.net/wordpress/>

#include <vector>

int main(int argc, char *argv[]) {
std::vector<std::vector<int vectovect;
vectovect.resize(20);
for (std::vector<std::vector<int::iterator i = vectovect.begin();
i != vectovect.end(); ++i) {
i->resize(30);
}
}

--
Daniel Pitts'' Tech Blog: <http://virtualinfinity.net/wordpress/>


这篇关于是否可以创建矢量矢量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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