共用方式為


vector::resize

做為向量指定新的大小。

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

參數

  • _Newsize
    向量的新大小。

  • _Val
    如果新的大小大於原始大小,會將新的項目加入至向量。 如果省略這個值,指定的新物件的預設值。

備註

如果容器大小小於所要求的大小, _Newsize,元素會加入向量,直到達到所要求的大小。 如果容器大小大於所要求的大小,最接近項目容器的結尾刪除容器,直到到達大小 _Newsize。 如果容器的大小與要求的大小相同,則不採取任何動作。

大小 反映向量的目前大小。

範例

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

需求

標題: <vector>

命名空間: std

請參閱

參考

vector Class

標準樣板程式庫