gpt4 book ai didi

c++ - 当新大小小于当前大小时,std::vector::resize() 是否会重新分配?

转载 作者:IT老高 更新时间:2023-10-28 22:27:00 28 4
gpt4 key购买 nike

Possible Duplicate:
std::vector resize downward

如果我 resize()一个 std::vector比当前大小小一些, vector 是否有可能分配新内存?

出于性能原因,这对我很重要。

最佳答案

不,resize将永远不会重新分配。

In case the container shrinks, all iterators, pointers and references to elements that have not been removed remain valid after the resize and refer to the same elements they were referring to before the call.

(来自here)

鉴于此,我们可以确定不会发生重新分配。

关于c++ - 当新大小小于当前大小时,std::vector::resize() 是否会重新分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14319930/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com