Can pop_back() ever reduce the capacity of a vector? (C++)

前端 未结 3 1135
野的像风
野的像风 2020-12-20 16:07

According to the C++ standard, is std::vector::pop_back() ever allowed to reduce the capacity of the vector?

I am asking because I would like t

3条回答
  •  执念已碎
    2020-12-20 16:38

    According to http://en.cppreference.com/w/cpp/container/vector/pop_back

    No iterators or references except for back() and end() are invalidated.

    Therefore it may not reallocate. There is no C++11 tag on that page, which implies this is also true in 03. I will dig up the section references and edit them in for completeness.

    Edit: Even better: From C++03: [lib.container.requirements] (23.1), paragraph 10:

    no erase(), pop_back() or pop_front() function throws an exception.

    Same wording at 23.2.1/10 in N3337 (~C++11).

提交回复
热议问题