Does a vector assignment invalidate the `reserve`?

前端 未结 3 1636
天命终不由人
天命终不由人 2021-02-01 15:03

Suppose I write

std::vector littleVector(1);
std::vector bigVector;

bigVector.reserve(100);
bigVector = littleVector;

Does t

3条回答
  •  执笔经年
    2021-02-01 15:06

    This depends on the allocator traits.

    Here's an excerpt from http://en.cppreference.com/w/cpp/container/vector/operator%3D:

    If std::allocator_traits::propagate_on_container_copy_assignment() is true, the target allocator is replaced by a copy of the source allocator. If the target and the source allocators do not compare equal, the target (*this) allocator is used to deallocate the memory, then other's allocator is used to allocate it before copying the elements.(since C++11)

    Basically, the memory is reallocated with the new allocator, if the allocators are incompatible (if they cannot deallocate each-other's memory.

    It shouldn't matter between vector implementations, but between allocator implementations (which makes sense).

提交回复
热议问题