it returns the size of the storage space currently allocated for the vector, expressed in terms of elements.This capacity is not necessarily equal to the vector size. It can be equal or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion.
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. The theoretical limit on the size of a vector is given by member max_size.
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
for (int i=0; i<100; i++)
myvector.push_back(i);
cout << "size: " << (int) myvector.size() << endl;
cout << "capacity: " << (int) myvector.capacity() << endl;
cout << "max_size: " << (int) myvector.max_size() << endl;
return 0;
}
complexity of capacity() = O(1)
Output of above code:
size: 100
capacity: 128
max_size: **********