boost::find

提供: C++入門
2015年11月7日 (土) 00:20時点におけるDaemon (トーク | 投稿記録)による版

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索
スポンサーリンク

boost::findは、コンテナ内の値を簡単に探せます。

読み方

boost::find
ぶーすと ふぁいんど

概要

boostboost::findstd::vector の値を探すことが簡単にできます。イテレータでグルグル回して比較する必要はありません。

boost_find_vector.cpp の例

ソースコード boost_find_vector.cpp

#include <iostream>
#include <vector>
 
#include <boost/range/algorithm.hpp>
 
using namespace std;
 
int
main (int argc, char *argv[])
{
        vector<int> v;
 
        v.push_back ( 1 );
        v.push_back ( 2 );
        v.push_back ( 3 );
        v.push_back ( 4 );
 
        vector<int>::iterator it = boost::find(v, 3);
 
        cout << *it << endl;
 
        return 0;
}

コンパイル

c++  -I/usr/local/include boost_find_vector.cpp -o boost_find_vector

実行例

% ./boost_find_vector
3

値が見つからない例

ソースコード boost_find_vector.cpp

#include <iostream>
#include <vector>
 
#include <boost/range/algorithm.hpp>
 
using namespace std;
 
int
main (int argc, char *argv[])
{
        vector<int> v;
 
        v.push_back ( 1 );
        v.push_back ( 2 );
        v.push_back ( 3 );
        v.push_back ( 4 );
 
        vector<int>::iterator it = boost::find(v, 100); // 見つからない値
 
        cout << *it << endl;
        bool b = (it == v.end());
        cout << b << endl;
 
        return 0;
}

コンパイル

c++  -I/usr/local/include boost_find_vector.cpp -o boost_find_vector

実行例

値が見つからない場合は、イテレータは、end()と同じ値です。

% ./boost_find_vector
-1
1

関連項目




スポンサーリンク