次の方法で共有


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

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

Requirements

Header: <vector>

Namespace: std

See Also

Concepts

vector Class

vector Members

vector::size and vector::capacity

Standard Template Library