vector::capacity

Returns the number of elements that the vector could contain without allocating more storage.

size_type capacity( ) const;

Return Value

The current length of storage allocated for the vector.

Remarks

The member function resize will be more efficient if sufficient memory is allocated to accommodate it. Use the member function reserve to specify the amount of memory allocated.

Example

// vector_capacity.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
  using namespace std;
    vector <int> v1;
    v1.reserve(10);

    v1.push_back(1);
    cout << "The length of storage allocated is "
        << v1.capacity() << "." << endl;

    v1.push_back(2);
    cout << "The length of storage allocated is now "
        << v1.capacity() << "." << endl;
}
The length of storage allocated is 10.
The length of storage allocated is now 10.

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

vector::size and vector::capacity

Standard Template Library