// vector の中から指定値の要素を検索 // algorithm の find を使用 #include <iostream> #include <algorithm> #include <vector> using namespace std; int main( ) { cout << "[vector08] find in vector" << endl ; vector<int> iVec; cout << "vector size = " << iVec.size( ) << endl ; iVec.push_back( 10 ); iVec.push_back( 9 ); iVec.push_back( 8 ); cout << "vector size = " << iVec.size( ) << endl ; for ( vector<int>::iterator i = iVec.begin( ) ; i != iVec.end( ) ; i++ ) { cout << *i << endl ; } vector<int>::iterator first = iVec.begin( ); vector<int>::iterator last = iVec.end( ); vector<int>::iterator i_ret = find( first, last, 9 ); if ( i_ret != last ) { cout << "hit!!" << endl ; cout << "value = " << *i_ret << endl ; cout << "index = " << i_ret - first << endl ; } else { cout << "not found" << endl ; } cout << "size( ) = " << iVec.size( ) << endl ; cout << "max_size( ) = " << iVec.max_size( ) << endl ; cout << "capacity( ) = " << iVec.capacity( ) << endl ; return 0; } // find( ) の第2引数は、vector の指定範囲の最終要素+1 のイテレータを指定 // することに注意。 // for()文を使わなくていい恩恵は大きいですね。可読性もアップです。 // end of file