add_new()用于向量的push_back()数组 [英] add_new() for array like push_back() for vector

查看:74
本文介绍了add_new()用于向量的push_back()数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




vector< T> v(100);

T * a =新T [100];


v.push_back(T());

是否可以将新T添加到数组以获取_contiguous_存储区域(上例中为101元素)?


-

Alex Vinokur

mailto:al **** @ connect.to
http://mathforum.org/library/view/10978.html

解决方案



" Alex Vinokur" <人**** @ big.foot.com>在留言中写道

news:c5 ************* @ ID-79865.news.uni-berlin.de ...



矢量< T> v(100);
T * a = new T [100];

v.push_back(T());
是否可以将新T添加到数组中获取_contiguous_存储区域(在上面的例子中为
101元素)?




不,你能做的就是分配一个新的101元素数组和复制元素

从旧阵列到新阵列。


但问题是什么?矢量已经连续,只需使用矢量。


john




" Alex Vinokur" ; <人**** @ big.foot.com>在留言中写道

news:c5 ************* @ ID-79865.news.uni-berlin.de ...



矢量< T> v(100);
T * a = new T [100];

v.push_back(T());
是否可以将新T添加到数组中获取_contiguous_存储区域(在上面的例子中为
101元素)?




不,你能做的就是分配一个新的101元素数组和复制元素

从旧阵列到新阵列。


但问题是什么?矢量已经连续,只需使用矢量。


john


" Alex Vinokur" <人**** @ big.foot.com>在留言中写道

news:c5 ************* @ ID-79865.news.uni-berlin.de ...



矢量< T> v(100);
T * a = new T [100];

v.push_back(T());
是否可以将新T添加到数组中get _contiguous_ storage area(对于



101元素在上面的例子中)?


是的。向量始终具有连续存储。只需说


const int sz = 100;

std :: copy(a,a + sz,std :: back_inserter(v));


br


Thorsten


(顺便说一下,一个人应该*总是*远离内置阵列C ++,除非

是*非常好的理由。

使用boost :: array<>和std :: vector<>而不是。)


Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for 101 element in an example above)?

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

解决方案


"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...

Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for 101 element in an example above)?



No, all you can do is allocate a new 101 element array and copy elements
from the old array to the new array.

But what''s the problem? vector is contiguous already, just use a vector.

john



"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...

Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for 101 element in an example above)?



No, all you can do is allocate a new 101 element array and copy elements
from the old array to the new array.

But what''s the problem? vector is contiguous already, just use a vector.

john


"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...

Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for


101 element in an example above)?

yes. A vector always has contiguous storage. Just say

const int sz = 100;
std::copy( a, a+sz, std::back_inserter( v ) );

br

Thorsten

(Btw, one should *always* stay away from builtin arrays in C++, unless there
is a *very* good reason.
Use boost::array<> and std::vector<> instead.)


这篇关于add_new()用于向量的push_back()数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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