Does std::vector::resize() ever reallocate when new size is smaller than current size? [duplicate]

给你一囗甜甜゛ 提交于 2020-01-03 07:25:08

问题


Possible Duplicate:
std::vector resize downward

If I resize() an std::vector to some size smaller than its current size, is it possible that the vector will ever allocate new memory?

This is important to me for performance reasons.


回答1:


No, resizeing to a smaller size will never reallocate.

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.

(From here)

Given that, we can be sure that a reallocation cannot have happened.




回答2:


resize() when reducing only changes the logical size. Others have already answered this, so I am adding nothing here. The purpose of this is to optimise for speed, as it does not need to reallocate or move any data.

However when you want to optimise for memory usage, C++11 has introduced a further function shrink_to_fit() that you may call after your resize() (or even at any other time) which will actually ensure you don't pay for any memory you don't want.




回答3:


No. The vector will never, ever shrink it's memory, except in a few quite specific conditions. Remember that when the vector resizes, iterators are invalidated, so it cannot go around doing this behind your back- it's an observable change, and the Standard specifies exactly when this may or may not happen.




回答4:


No.

Vector uses two values: size and capacity. The size is the actual number of elements stored in the vector, while the capacity reffers to the allocated reserved space in memory. The performance increase comes from allocating more space than is needed.

Only when the request for resize which will result in a capacity greater than the current one is performed, will the re-allocation take place. Also, if you try to insert a lot of elements in the middle of the vector, it will impact on the algorithm performance since all elements after the inserted ones need to be moved (sometimes, this can trigger reallocation, if you exceed capacity of the vector).

You can use the reserve member function to further increase speed: the reserve member function will make sure that the capacity is set to a specific value.

You can read more about std::vector on page 148 - in the book The C++ Standard Library: A Tutorial and Reference.




回答5:


First you have to measure what you want to optimize, performance alone is not sufficient, what do you mean ? UI reactivity ? that need a typical user action for which you measure time. Heavy algorithm ? and so on ... Then you have to find where is the bottleneck, may be memory, disk access, etc, and at the end may be vector::resize, but only at the end ! And the way you ask your question, I bet my $ that vector::resize will not be the bottleneck.

Be confident the way STL is designed, check your code before modifying STL behavior ;-)



来源:https://stackoverflow.com/questions/14319930/does-stdvectorresize-ever-reallocate-when-new-size-is-smaller-than-current

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!