list::resize
針對清單指定新的大小。
void resize(
size_type _Newsize
);
void resize(
size_type _Newsize,
Type _Val
);
參數
_Newsize
清單的新大小。_Val
要加入之新項目的值設定為 清單中,如果新的大小大於原始大小。 如果省略這個值,新項目指定類別的預設值。
備註
如果清單的大小小於所要求的大小, _Newsize,項目加入至清單,直到達到所要求的大小。
如果清單的大小大於所要求的大小,最接近項目清單的結尾刪除清單,直到到達大小 _Newsize。
如果清單中的目前大小與要求的大小,則不會採取任何動作所用的相同。
大小 反映清單的目前大小。
範例
// 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