Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

vector::capacity

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

size_type capacity( ) const;

The current length of storage allocated for the vector.

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.

// 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.

Header: <vector>

Namespace: std

Show:
© 2015 Microsoft