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;

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.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;
}

Output

The length of storage allocated is 1.
The length of storage allocated is now 2.

See Also

vector Class | vector Members | vector::size and vector::capacity Sample

Show:
© 2014 Microsoft