「boost::find」の版間の差分

提供: C++入門
移動: 案内検索
(Daemon がページ「Boost::find」を「boost::find」に移動しました)
 
行1: 行1:
<!--
+
[[boost::find]]は、[[コンテナ]]内の値を簡単に探せます。
vim: filetype=mediawiki
+
 
-->
+
'''読み方'''
 +
;[[boost::find]]:ぶーすと ふぁいんど
  
 
__TOC__
 
__TOC__
  
 
== 概要 ==
 
== 概要 ==
 
+
[[boost]]の[[boost::find]] で [[std::vector]] の値を探すことが簡単にできます。[[イテレータ]]でグルグル回して比較する必要はありません。
[[boost]]の[[boost::find]] で [[std::vector]] の値を探すことが簡単にできます。[[Iterator]]でグルグル回して比較する必要はありません。
+
 
+
 
== boost_find_vector.cpp の例 ==
 
== boost_find_vector.cpp の例 ==
 
 
=== ソースコード boost_find_vector.cpp ===
 
=== ソースコード boost_find_vector.cpp ===
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <iostream>
 
#include <iostream>
行38: 行35:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
=== コンパイル ===
 
=== コンパイル ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
g++  -I/usr/local/include boost_find_vector.cpp -o boost_find_vector
+
c++  -I/usr/local/include boost_find_vector.cpp -o boost_find_vector
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
=== 実行例 ===
 
=== 実行例 ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
% ./boost_find_vector
 
% ./boost_find_vector
 
3
 
3
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== 値が見つからない例 ==
 
== 値が見つからない例 ==
 
 
=== ソースコード boost_find_vector.cpp ===
 
=== ソースコード boost_find_vector.cpp ===
 
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <iostream>
 
#include <iostream>
行67: 行57:
 
main (int argc, char *argv[])
 
main (int argc, char *argv[])
 
{
 
{
 
 
         vector<int> v;
 
         vector<int> v;
  
行84: 行73:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
=== コンパイル ===
 
=== コンパイル ===
 
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
g++  -I/usr/local/include boost_find_vector.cpp -o boost_find_vector
+
c++  -I/usr/local/include boost_find_vector.cpp -o boost_find_vector
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
=== 実行例 ===
 
=== 実行例 ===
 
+
値が見つからない場合は、[[イテレータ]]は、end()と同じ値です。
値が見つからない場合は、[[Iterator]]は、end()と同じ値です。
+
 
+
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
% ./boost_find_vector
 
% ./boost_find_vector
行100: 行84:
 
1
 
1
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
 
== 関連項目 ==
 
== 関連項目 ==
 
 
* [[boost]]
 
* [[boost]]
 
* [[std::vector]]
 
* [[std::vector]]
 +
<!-- vim: filetype=mediawiki
 +
-->

2015年11月7日 (土) 00:20時点における最新版

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

関連項目