std :: vector :: resize, [英] std::vector::resize ,

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

问题描述

在有效STL的第67页,Meyers写了关于调整大小:如果n比当前大小更小

,容器末尾的元素将是

销毁。


什么是销毁意思?在我看来,如果n小于

当前大小,可以简单地将内部大小计数器调整为n并完成。

On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller
than the current size, elements at the end of the container will be
destroyed."

What does "destroyed" mean? It seems to me that if n is smaller than the
current size, simple adjust the internal size counter to n and be done.

推荐答案

2005年10月17日星期一16:21:59 -0700,vsgdp < SP ** @ void.com>写道:
On Mon, 17 Oct 2005 16:21:59 -0700, "vsgdp" <sp**@void.com> wrote:
在有效STL的第67页,Meyers写了关于调整大小的文章:如果n比当前大小小,那么容器末尾的元素将是
被毁坏了。

什么被毁坏意思?在我看来,如果n小于
当前大小,可以简单地将内部大小计数器调整为n并完成。
On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller
than the current size, elements at the end of the container will be
destroyed."

What does "destroyed" mean? It seems to me that if n is smaller than the
current size, simple adjust the internal size counter to n and be done.




Destroyed意味着每个元素的析构函数都被调用...

向量可以包含类类型和POD类型的元素。


-

Bob Hairgrove
否********** @ Home.com


vsgdp写道:
vsgdp wrote:
在有效STL的第67页,Meyers写了关于调整大小的文章:如果n是比当前尺寸小,容器末端的元件将被销毁。


这意味着调用这些对象的析构函数。


什么是销毁?意思?在我看来,如果n小于
当前大小,可以简单地将内部大小计数器调整为n并完成。
On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller
than the current size, elements at the end of the container will be
destroyed."
It means that the destructors for these objects are called.

What does "destroyed" mean? It seems to me that if n is smaller than the
current size, simple adjust the internal size counter to n and be done.




适用于POD类型和类型与简单的析构函数。但是,

编译器不需要优化析构函数调用。

最佳


Kai-Uwe Bux




" Kai-Uwe Bux" < JK ******** @ gmx.net>在消息中写道

news:dj ********** @ murdoch.acc.Virginia.EDU ...

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:dj**********@murdoch.acc.Virginia.EDU...
vsgdp写道:
vsgdp wrote:
在有效STL的第67页,Meyers写了关于调整大小的文章:如果n比当前大小小,那么容器末尾的元素将被销毁。
这意味着调用这些对象的析构函数。

On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller
than the current size, elements at the end of the container will be
destroyed."
It means that the destructors for these objects are called.

什么是销毁?意思?在我看来,如果n小于
当前大小,可以简单地将内部大小计数器调整为n并完成。
What does "destroyed" mean? It seems to me that if n is smaller than the
current size, simple adjust the internal size counter to n and be done.



这适用于POD类型和类型琐碎的析构函数。但是,
编译器不需要优化析构函数调用。



That works for POD types and types with trivial destructor. However, the
compiler is not required to optimize the destructor calls away.




谢谢。 POD是什么意思?我还是不明白。如果你有一个类型C的向量大小为n的

向量,并且调整大小为m<

n,为什么必须调用析构函数?你可以把对象放在那里...... / b $ b坐在那里 - 记忆仍在那里。然后你打电话给push_back

on,你基本上是在对下一个元素进行分配,从而

覆盖它。 C'重载的运算符=或复制构造函数应该花费

来做正确的任务。

最好

Kai-Uwe Bux



Thanks. What does POD mean? I still do not understand. If you had a
vector of some class type C with vector size n, and resize to a new size m <
n, why must the destructors be called? You can just leave the objects
sitting there--the memory is still there. Then is you call push_back later
on, you are essentially doing an assignment to the next element, thereby
overwriting it. C''s overloaded operator = or copy constructor should take
care of doing a correct assignment.
Best

Kai-Uwe Bux



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

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