Does vector::erase reduce vector::capacity?

c++ vector remove element by value
vector pop front c++
vector::erase time complexity
std::vector remove first element
vector::erase return
vector erase cplusplus
c++ delete object from vector

Cppreference only says:

Removes specified elements from the container. 1) Removes the element at pos. 2) Removes the elements in the range [first; last).

Invalidates iterators and references at or after the point of the erase, including the end() iterator.

The iterator pos must be valid and dereferenceable. Thus the end() iterator (which is valid, but is not dereferencable) cannot be used as a value for pos.

The iterator first does not need to be dereferenceable if first==last: erasing an empty range is a no-op.

Not necessarily no. When reading the C++ standard (and cppreference proxies the standard remarkably well), if something is not explicitly mentioned, then assume such a something is not required.

It would possibly be sub-optimal for a C++ Standard Library implementation to do so.

why std::vector item deletion does not reduce its capacity?, Starting with C++11, we can call the vector::shrink_to_fit function after clear() which reduces the capacity of the vector to fir the size. It works by “requesting” a  Does this description mean that the capacity of a vector can be decreased by resize? I understood that the capacity was never decreased in order to reduce the overhead of allocating/deallocating dynamic memory. Can somebody enlighten me? F.Z.

No. That's implied by the fact that iterators, pointers and references prior to the point of erase remain valid. Reducing the capacity would require a reallocation.

Delete vector contents and free up memory in C++, How do you remove the last element of a vector? No, you cannot reduce the capacity of a vector without copying. However, you can control how much new allocation growth by checking capacity() and call reserve() every time you insert something. The default behavior for std::vector is to grow its capacity by a factor of 2 every time new capacity is needed. You can growth it by your own magic ratio:

With other constraints as complexity or iterator validity, that might force implementation in some way.


Complexity: Linear on the number of elements erased (destructions) plus the number of elements after the last element deleted (moving).

So decreasing capacity and moving old objects is a resized buffer is not possible. Except in the case when first and begin are the same.

Decreasing just the capacity number would be possible, but I don't see any sane implementation doing that.

Does clearing a vector affect its capacity?, from the end, and the container size is decreased by 1. It can be equal or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion. Notice that this capacity does not suppose a limit on the size of the vector. When this capacity is exhausted and more is needed, it is automatically expanded by the container (reallocating it storage space).

The only vector operations that affect the capacity are the ones that invalidate all iterators, pointers and references, because they have reallocated the storage and moved the elements.

There is nothing in the Allocator concept that would allow it to change the allocation size in place, so the capacity can't change in that manner either.

In theory an implementation could specialise on std::allocator and reallocate, under the "As-if" rule, but I doubt there are any serious implementations that would do such a thing.

vector::push_back() and vector::pop_back() in C++ , How do I remove a specific element from a vector? Removes from the vector either a single element ( position) or a range of elements ( [first,last) ). This effectively reduces the container size by the number of elements removed, which are destroyed. Because vectors use an array as their underlying storage, erasing elements in positions other than the vector end causes the container to

A quick example could have cleared your doubt :( compiled using VS2017 )

#include <iostream>
#include <vector>

int main()
    std::vector<int> intVec{ 1,2,3,4,5,6,7,8,9,10 };
    std::cout << "Capacity and size before erase : " << intVec.capacity() << ", "<< intVec.size() << std::endl;
    intVec.erase(intVec.begin() + 3);
    std::cout << "Capacity and size after erase : " << intVec.capacity() << ", " << intVec.size() << std::endl;
    return 0;

Output :

Capacity and size before erase : 10, 10

Capacity and size after erase : 10, 9

Note that while capacity() doesn't decrease after the erase, the size() certainly does.

vector erase() and clear() in C++, This effectively reduces the container size by the number of elements removed, which are vector::pop_back: Delete last element (public member function ). The length is: 5 0 1 2 0 0 In the above example, we’ve used the resize() function to set the vector’s length to 5. This tells variable array that we’re intending to use the first 5 elements of the array, so it should consider those in active use.

"Right" way to deallocate an std::vector object, std::vector::shrink_to_fit. void shrink_to_fit();. Shrink to fit. Requests the container to reduce its capacity to fit its size. Vector capacity is never reduced when resizing to smaller size because that would invalidate all iterators, rather than only the ones that would be invalidated by the equivalent sequence of pop_back() calls. [ edit ] Example

vector::erase - C++ Reference, A std::vector automatically increases its capacity upon insertion as needed, but it Remove half of the elements v.erase(v.begin() + 50, v.end()); // Reduces the size In C++11 we can use the shrink_to_fit() member function for a similar effect: This can be seen from inspection of the formula for VC because the parameter m appears by itself ( i.e.m 1) and only once ( Box 1). Interventions that reduce adult mosquito density, such as environmental management of larval sites would have 1st order effects.

vector::shrink_to_fit - C++ Reference, std::remove, std::remove_if in c++ · Difference between std::remove and Vectors are same as dynamic arrays with the ability to resize itself clear() function is used to remove all the elements of the vector container, thus making it size 0. from the container, thus reducing its size by the number of elements removed. Methods and Results. Using analytical solutions to updated equations for vectorial capacity we build on previous work to show that, while adult killing methods can be highly effective under many circumstances, other vector control methods are frequently required to fill effective coverage gaps.

  • Looks like it does get resized:
  • @IanA.B.King Of course the size have to be changed since an element is removed, but the capacity doesn't have to change.
  • comparable Does clearing a vector affect its capacity? It mentions: the standard does not specify this.
  • > When reading the C++ standard (and cppreference proxies the standard remarkably well), if something is not explicitly mentioned, then assume such a something is not required. So I guess the answer is it's not necessary?
  • In short the answer is indeed "not necessarily".
  • And complexity requirement implies too.
  • "other constraints ... iterator invalidity" - the iterator being invalidated isn't a constraint. By the standard saying it's invalidated, the emphasis is that there is no requirement on the implementation to do anything to keep it valid. The implementation doesn't NEED to go out of it's way to invalidate it, because it's UB to use it - which might be .... that it works.
  • @UKMonkey: I meant "which iterator should stay valid", so that if f() should not invalidate iterators, then reallocation could not happen. it invalidate some implementation as COW for string for example.
  • Ahh - that makes much more sense :)
  • It's worth detailing your toolchain when producing a counter-example.
  • This doesn't show the standard requires no change in capacity, merely that it allows no change
  • @Caleth :My intention was to show with an example that erase does not trigger a reallocation, thereby leading to no change in capacity.
  • a single example cannot serve as proof of a general concept
  • ..i mean your example is fine, but you cannot draw any conclusion from it for the general case