共用方式為


vector::resize

Specifies a new size for a vector.

void resize(
   size_type _Newsize
);
void resize(
   size_type _Newsize,
   Type _Val
);

Parameters

  • _Newsize
    The new size of the vector.

  • _Val
    The value of new elements added to the vector if the new size is larger that the original size. If the value is omitted, the new objects are assigned the default value.

Remarks

If the container's size is less than the requested size, _Newsize, elements are added to the vector until it reaches the requested size. If the container's size is larger than the requested size, the elements closest to the end of the container are deleted until the container reaches the size _Newsize. If the present size of the container is the same as the requested size, no action is taken.

size reflects the current size of the vector.

Example

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

int main( )
{ 
   using namespace std;   
   vector <int> v1;
   
   v1.push_back( 10 );
   v1.push_back( 20 );
   v1.push_back( 30 );

   v1.resize( 4,40 );
   cout << "The size of v1 is " << v1.size( ) << endl;
   cout << "The value of the last object is " << v1.back( ) << endl;

   v1.resize( 5 );
   cout << "The size of v1 is now " << v1.size( ) << endl;
   cout << "The value of the last object is now " << v1.back( ) << endl;
}

The size of v1 is 4
The value of the last object is 40
The size of v1 is now 5
The value of the last object is now 0

Requirements

Header: <vector>

Namespace: std

See Also

Concepts

vector Class

vector Members

Standard Template Library