// resize( )の後はイテレータを取り直せ。 // Cでいうrealloc( )の振る舞いなので指し示す先が移っていることがある。 #include <iostream> #include <vector> using namespace std; int main( ) { cout << "[vector13] resize( )" << endl ; vector<int> sVec; // 例としてわざと、領域(サイズとは違う)を小さくしておく sVec.reserve( 3 ); sVec.push_back( 7 ); sVec.push_back( 8 ); sVec.push_back( 9 ); cout << "vector size = " << sVec.size( ) << endl ; cout << "vector reserved size = " << sVec.capacity( ) << endl; for ( vector<int>::iterator i = sVec.begin( ) ; i != sVec.end( ) ; i++ ) { cout << *i << endl ; } cout << "svVec.begin( ) = " << sVec.begin( ) << endl; // ここで capacity を越えるリサイズをおこなってみる // 追加要素分は 1 で初期化 cout << "resize( )" << endl; sVec.resize( 5, 1 ); cout << "vector size = " << sVec.size( ) << endl ; cout << "vector reserved size = " << sVec.capacity( ) << endl; for ( i = sVec.begin( ) ; i != sVec.end( ) ; i++ ) { cout << *i << endl ; } cout << "svVec.begin( ) = " << sVec.begin( ) << endl; return 0; } /* 1. resize( )前後で、sVec.begin( ) の値が変っています。 要注意ですね。 2. 同じく、push_back( )、insert( )、erase( ) も領域を移動する ことがありますので要注意です。 3. 極力、vectorのインスタンスを生成した時には、余裕を持って reserve( ) しておくとよいです。パフォーマンスも。 */ // end of file