Reducing the capacity of a vector - C++ STL

C++ examples for STL:vector

Description

Reducing the capacity of a vector

Demo Code

#include <cinttypes>
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
    vector<int32_t> stlVector {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

    while (stlVector.size() > 0)
    {//  w  ww  .  j a  v a2s.c o m
        auto iterator = stlVector.end() - 1;
        stlVector.erase(iterator);

        if ((stlVector.size() * 2) == stlVector.capacity())
        {
            stlVector.shrink_to_fit();
        }

        cout << "The size is: " << stlVector.size() << endl;
        cout << "The capacity is: " << stlVector.capacity() << endl << endl;
    }

    return 0;
}

Result


Related Tutorials