在调用push_back之前调整vector的大小更好吗? [英] Is it better idea to do resize of vector before calling push_back?

查看:69
本文介绍了在调用push_back之前调整vector的大小更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有时会遇到问题,而我正试图在

向量上执行push_back。目前我正在调整向量大小增加大小

一个然后push_back似乎代码工作正常。


这是一个更好的主意要调整大小befoire调用push_back吗?


问候,

Tarun

Hello,

I am facing problem sometimes while I am trying to do push_back on a
vector. Currently I am doing resize of the vector increasing the size
by one and then push_back and seems like the code is working fine.

Is it a better idea to do resize befoire calling push_back?

Regards,
Tarun

推荐答案



Tarun skrev:

Tarun skrev:

您好,


我正面临问题有时当我试图在

向量上做push_back时。目前我正在调整向量大小增加大小

一个然后push_back似乎代码工作正常。


这是一个更好的主意调整大小befoire调用push_back?
Hello,

I am facing problem sometimes while I am trying to do push_back on a
vector. Currently I am doing resize of the vector increasing the size
by one and then push_back and seems like the code is working fine.

Is it a better idea to do resize befoire calling push_back?



没理由这样做。如果要推送许多

元素,并且你知道要添加多少元素(大约),请使用调整大小。


/ Peter

No reason to do that. Use resize if you are going to push_back many
elements and you know how many elements (approximately) you will add.

/Peter


" peter koch" < pe *************** @ gmail.comwrote:
"peter koch" <pe***************@gmail.comwrote:

Tarun skrev:
Tarun skrev:


我正在尝试在

向量上执行push_back时遇到问题。目前我正在调整向量大小增加大小

一个然后push_back似乎代码工作正常。


这是一个更好的主意调整大小befoire调用push_back?
I am facing problem sometimes while I am trying to do push_back on a
vector. Currently I am doing resize of the vector increasing the size
by one and then push_back and seems like the code is working fine.

Is it a better idea to do resize befoire calling push_back?



没理由这样做。如果你要推送许多

元素并且你知道要添加多少元素(大约),请使用resize。


No reason to do that. Use resize if you are going to push_back many
elements and you know how many elements (approximately) you will add.



我会假设你们正在谈论预备而不是调整大小。

否则我同意彼得。只有在分析显示时才使用保留

存在瓶颈。

I''m going to assume you guys are talking about reserve and not resize.
Otherwise I agree with peter. Only use reserve when profiling has shown
there is a bottleneck.


Daniel T.写道:
Daniel T. wrote:

peter koch < pe *************** @ gmail.comwrote:
"peter koch" <pe***************@gmail.comwrote:

> Tarun skrev:
>Tarun skrev:


>>我正在尝试在
向量上执行push_back时遇到问题。目前我正在调整向量的大小,将
大小增加一个,然后再推送回来,看起来代码工作正常。

调整大小befoire是一个更好的主意吗调用push_back?
>>I am facing problem sometimes while I am trying to do push_back on a
vector. Currently I am doing resize of the vector increasing the
size by one and then push_back and seems like the code is working
fine.

Is it a better idea to do resize befoire calling push_back?


没理由这样做。如果要推送许多
元素,并且知道要添加多少元素(大约),请使用resize。


No reason to do that. Use resize if you are going to push_back many
elements and you know how many elements (approximately) you will add.



我会假设你们正在谈论预备而不是调整大小。

否则我同意彼得的意见。仅在分析时使用保留

显示存在瓶颈。


I''m going to assume you guys are talking about reserve and not resize.
Otherwise I agree with peter. Only use reserve when profiling has
shown there is a bottleneck.



使用

''push_back''时插入的性能不一定是麻烦。从较小的大小到较大的频繁重新分配可能会导致堆碎片,这可能会影响以后的性能,而不会显示在分析雷达上。在您知道的任何时候使用''reserve''(确保

)所有插入后的大小。如果那是

的情况(即你知道大小是多少),那么为什么不调整大小并使用

而不是作业?


V

-

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

我请不要回复最热门的回复,请不要问

Performance of the insertions is not necessarily the trouble when using
''push_back''. Frequent reallocations from smaller size to larger can
cause heap fragmentation which may impede the performance later and not
even show up on profiling radars. Use ''reserve'' any time you know (for
sure) what the size will be after all insertions. And if that''s the
case (i.e. you know what the size is), then why not just resize and use
the assignment instead?

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


这篇关于在调用push_back之前调整vector的大小更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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