Share via


list::resize

リストの新しいサイズを指定します。

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

パラメーター

  • _Newsize
    リストの新しいサイズ。

  • _Val
    新しいサイズが元のサイズよりも大きい場合に、リストに追加される新しい要素の値。 この値を省略した場合、新しい要素にはそのクラスの既定値が割り当てられます。

解説

リストのサイズが要求されたサイズ (_Newsize) よりも小さい場合は、要求されたサイズになるまで、リストに要素が追加されます。

リストのサイズが要求されたサイズよりも大きい場合は、リストのサイズが _Newsize になるまで、リストの末尾に近い要素から順に削除されます。

リストの現在のサイズが要求されたサイズと同じ場合は、何も実行されません。

size はリストの現在のサイズを反映します。

使用例

// list_resize.cpp
// compile with: /EHsc
#include <list>
#include <iostream>

int main( )
{ 
   using namespace std;
   list <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;
}
  

必要条件

ヘッダー: <list>

名前空間: std

参照

関連項目

list クラス

標準テンプレート ライブラリ