=运算符:向量 [英] = operator: vector

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

问题描述

你好,


我有一个循环,我需要保留两个向量,oldVector和

newVector。处理oldVector的元素,并将新元素添加到newVector中。在循环结束时,我只需要执行oldVector =

newVector。用于下一次迭代。我的问题是分配

销毁oldVector的内容并复制新元素

newVector?这很好,如果那些事情发生了,但如果没有,这是一个巨大的内存泄漏。


我看了矢量的来源,它不是很清楚在这种情况下,

会发生什么。 (有两个功能复制和未初始化的副本,

无法查看)。


KS

解决方案



KS写道:


您好,


我有一个循环,我需要保留两个向量,oldVector和

newVector。处理oldVector的元素,并将新元素添加到newVector中。在循环结束时,我只需要执行oldVector =

newVector。用于下一次迭代。我的问题是分配

销毁oldVector的内容并复制新元素

newVector?这很好,如果那些事情发生了,但如果没有,这是一个巨大的内存泄漏。


我看了矢量的来源,它不是很清楚在这种情况下,

会发生什么。 (有两个功能复制和未初始化的副本,

无法查看)。


KS



是的。 std :: library中的赋值很直观,可以直观地预测




/ Peter


" KS" < kn ******* @ gmail.com写信息

新闻:11 ********************** @ i3g2000cwc.googlegro ups.com ...


您好,


我有一个循环,我需要保留两个vector,oldVector和

newVector。处理oldVector的元素,并将新元素添加到newVector中。在循环结束时,我只需要执行oldVector =

newVector。用于下一次迭代。我的问题是分配

销毁oldVector的内容并复制新元素

newVector?这很好,如果那是什么,但如果没有,这是一个巨大的内存泄漏。



是的,你是对的。在赋值之后,oldVector中包含

的对象将被正确破坏,oldVector将包含

newVector元素的副本。


菲利普


" KS" < kn ******* @ gmail.comschrieb im Newsbeitrag

news:11 ********************** @ i3g2000cwc.googlegro ups.com ...


您好,


我有一个循环,我需要保留两个vector,oldVector和

newVector。处理oldVector的元素,并将新元素添加到newVector中。在循环结束时,我只需要执行oldVector =

newVector。用于下一次迭代。我的问题是分配

销毁oldVector的内容并复制新元素

newVector?这很好,如果那些事情发生了,但如果没有,这是一个巨大的内存泄漏。


我看了矢量的来源,它不是很清楚在这种情况下,

会发生什么。 (有两个功能复制和未初始化的副本,

无法查看)。



正如彼得和菲利普已经写过的那样,它有效。但是如果在将newVector分配给oldVector之后清除内容

,则交换这些向量可能比分配更有效。所以如果你做了类似的事情


oldVector = newVector;

newVector.clear();


它可能做得更好


oldVector.swap(newVector);

newVector.clear();


这个不会在newVector中创建元素的副本,但会将现有元素从newVector移动到oldVector(以及从oldVector

到newVector的元素)。


HTH

Heinz


Hello,

I have a loop in which I need to keep two vectors, oldVector and
newVector. The elements of oldVector are processed and new elements are
added to newVector. At the end of the loop I simply do "oldVector =
newVector" for the next iteration. My question is does is assignment
destroy the contents of the oldVector and copy the new elements of
newVector? Its good if thats whats happenning, but if not, it is a huge
memory leak.

I looked at the source of vector and its not very clear what happens in
this case. (There are two functions copy and uninitialized copy which
are not available for viewing).

KS

解决方案


KS wrote:

Hello,

I have a loop in which I need to keep two vectors, oldVector and
newVector. The elements of oldVector are processed and new elements are
added to newVector. At the end of the loop I simply do "oldVector =
newVector" for the next iteration. My question is does is assignment
destroy the contents of the oldVector and copy the new elements of
newVector? Its good if thats whats happenning, but if not, it is a huge
memory leak.

I looked at the source of vector and its not very clear what happens in
this case. (There are two functions copy and uninitialized copy which
are not available for viewing).

KS

Yes. Assignment in the std::library is intuitive and does what is
intuitively expected.

/Peter


"KS" <kn*******@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

Hello,

I have a loop in which I need to keep two vectors, oldVector and
newVector. The elements of oldVector are processed and new elements are
added to newVector. At the end of the loop I simply do "oldVector =
newVector" for the next iteration. My question is does is assignment
destroy the contents of the oldVector and copy the new elements of
newVector? Its good if thats whats happenning, but if not, it is a huge
memory leak.

Yes, you are correct. After the assignment, the objects which were contained
in oldVector will have been destructed properly, and oldVector will contain
copies of the elements of newVector.

Philip


"KS" <kn*******@gmail.comschrieb im Newsbeitrag
news:11**********************@i3g2000cwc.googlegro ups.com...

Hello,

I have a loop in which I need to keep two vectors, oldVector and
newVector. The elements of oldVector are processed and new elements are
added to newVector. At the end of the loop I simply do "oldVector =
newVector" for the next iteration. My question is does is assignment
destroy the contents of the oldVector and copy the new elements of
newVector? Its good if thats whats happenning, but if not, it is a huge
memory leak.

I looked at the source of vector and its not very clear what happens in
this case. (There are two functions copy and uninitialized copy which
are not available for viewing).

As Peter and Philip already wrote, it works. But if you clear the contents
of newVector after assigning it to oldVector, swapping those vectors might
be more efficient than assignment. So if you do something like

oldVector = newVector;
newVector.clear();

it might be better to do

oldVector.swap(newVector);
newVector.clear();

This would not create copies of elements in newVector but would move
existing elements from newVector to oldVector (and elements from oldVector
to newVector).

HTH
Heinz


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

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