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.
using namespace std;
int main ()
for (int i=0; i<100; i++)
cout << "size: " << (int) myvector.size() << endl;
cout << "capacity: " << (int) myvector.capacity() << endl;
cout << "max_size: " << (int) myvector.max_size() << endl;
complexity of capacity() = O(1)
Output of above code: