STL:载体< T>到T [] [英] STL: vector<T> to T[]

查看:100
本文介绍了STL:载体< T>到T []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 -

STL向量中是否有方法将元素作为数组获取

而不是向量。


类似


vector< intvt;

vt.push_back(1);

vt.push_back(2) ;


int [] arr = vt。 xyz();

Hi -
Is there a method in STL vector to get the elements as an array
instead of as a vector.

Something like

vector<intvt;
vt.push_back(1);
vt.push_back(2);

int [] arr = vt. xyz ();

推荐答案

ne ******** @ gmail.com 写道:

STL向量中是否有方法将元素作为数组获取

而不是矢量。


类似


vector< intvt;

vt.push_back(1);

vt.push_back(2);


int [] arr = vt。 xyz();
Is there a method in STL vector to get the elements as an array
instead of as a vector.

Something like

vector<intvt;
vt.push_back(1);
vt.push_back(2);

int [] arr = vt. xyz ();



首先,你不能声明这样的数组。括号

跟随名称,他们需要包含大小。


其次,数组不能像这样初始化。你需要使用

指针:


int * arr =& vt [0];


V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复,请不要问

First of all, you cannot declare an array like that. Brackets
follow the name and they need to contain the size.

Second, arrays cannot be initialised like that. You need to use
pointers:

int *arr = &vt[0];

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


ne ***** ***@gmail.com 写道:

嗨 -

STL向量中是否有方法来获取元素作为数组

而不是矢量。


类似


vector< intvt;

vt.push_back(1);

vt.push_back(2);


int [] arr = vt。 xyz();
Hi -
Is there a method in STL vector to get the elements as an array
instead of as a vector.

Something like

vector<intvt;
vt.push_back(1);
vt.push_back(2);

int [] arr = vt. xyz ();



我认为你的程序习惯受到Java的严重影响,

通常你不需要转换C ++中的向量转换为数组,因为C ++中的向量实现,以及

'vector'',`iterator''和`algorithm''的合作,



如果你真的想把矢量内容的副本变成数组,

就可以这样:


int * arr = new int [vec.size()];

std :: copy(vec.begin(),vec.end(),arr);


或者如果你只是对矢量内容有一个参考(实际指针),你可以这样做:



std :: vector< int> :: const_iterator it = vec.begin();


但我认为后者是不可用的。

I think your program habit is heavily affected by Java,
It''s not that often you need to convert a vector in C++ into an array,
because of the vector implementation in C++, and the cooperation of
`vector'', `iterator'' and `algorithm''.

If you really wanna get a copy of the vector content into an array,
you can it like this:

int* arr = new int[vec.size()];
std::copy(vec.begin(), vec.end(), arr);

Or if you just have a reference(actually pointer) to the vector content,
you can do this way:

std::vector<int>::const_iterator it = vec.begin();

but I think the latter one is unusefull.

8月1日晚上7:30,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Aug 1, 7:30 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

vector< intvt;

vt .push_back(1);

vt.push_back(2);
vector<intvt;
vt.push_back(1);
vt.push_back(2);


int [] arr = vt。 xyz();
int [] arr = vt. xyz ();



首先,你不能声明这样的数组。括号

跟随名称,他们需要包含大小。


First of all, you cannot declare an array like that. Brackets
follow the name and they need to contain the size.



据我所知(请纠正我,如果我错了,因为我没有

。C ++标准的副本),std :: vector确实有一个默认的构造函数

在该声明中自动被调用,并将构造

a向量,大小为零。由于向量是动态的,因此以下

调用push_back()会自动将内部存储空间大小调整为
到必要的大小。

As far as I''m aware (please correct me if I''m wrong, as I don''t have a
copy of the C++ standard), std::vector does have a default constructor
that automatically gets called in that declaration, and will construct
a vector with a size of zero. Since vectors are dynamic, the following
invocations of push_back() automatically resize the internal storage
to the necessary size.


这篇关于STL:载体&lt; T&gt;到T []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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