共用方式為


deque::resize

Specifies a new size for a deque.

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

Parameters

  • _Newsize
    The new size of the deque.

  • _Val
    The value of the new elements to be added to the deque if the new size is larger that the original size. If the value is omitted, the new elements are assigned the default value for the class.

Remarks

If the deque's size is less than the requested size, _Newsize, elements are added to the deque until it reaches the requested size.

If the deque's size is larger than the requested size, the elements closest to the end of the deque are deleted until the deque reaches the size _Newsize.

If the present size of the deque is the same as the requested size, no action is taken.

size reflects the current size of the deque.

Example

// deque_resize.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

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

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

   c1.resize( 5 );
   cout << "The size of c1 is now: " << c1.size( ) << endl;
   cout << "The value of the last element is now " << c1.back( ) << endl;

   c1.resize( 2 );
   cout << "The reduced size of c1 is: " << c1.size( ) << endl;
   cout << "The value of the last element is now " << c1.back( ) << endl;
}

Output

The size of c1 is: 4
The value of the last element is 40
The size of c1 is now: 5
The value of the last element is now 0
The reduced size of c1 is: 2
The value of the last element is now 20

Requirements

Header: <deque>

Namespace: std

See Also

Concepts

deque Class

deque Class Members

deque::size and deque::resize

Standard Template Library