C++ のベクターに項目が存在するかどうかを確認する
この投稿では、C++のvectorにアイテムが存在するかどうかを確認する方法について説明します。
1.使用する std::find
アルゴリズム
C++ のベクターに項目が存在するかどうかを確認する 1 つの方法は、 find()
からのアルゴリズム <algorithm>
ヘッダ。このアルゴリズムは、要素の範囲内で指定された値を検索し、最初に出現した値の反復子を返します。値が見つからない場合は、範囲の終わりまでの反復子を返します。 C++ で検索アルゴリズムを使用して項目がベクター内に存在するかどうかを確認するには、範囲の先頭と末尾、および検索する値の 3 つの引数を指定する必要があります。例えば:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; int target = -9; bool found = std::find(vec.begin(), vec.end(), target) != vec.end(); std::cout << std::boolalpha << found << std::endl; // true return 0; } |
使用する利点 std::find
それは、一致するものが見つかるとすぐに検索を停止するということです。つまり、最初に出現した値への反復子のみを返します。したがって、ベクトル内の値の複数の出現を検索するために検索アルゴリズムを使用しないでください。
2.使用する std::find_if
アルゴリズム
C++ のベクターに項目が存在するかどうかを確認するもう 1 つの方法は、 std::find_if
からのアルゴリズム <algorithm>
ヘッダ。の std::find_if
アルゴリズムは、述語が返す指定された範囲内の最初の要素への反復子を返します。 true
。項目がベクター内に存在するかどうかを確認するには、以下に示すように、述語が現在の要素とターゲットを一致させる必要があります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; int target = -9; auto it = std::find_if(vec.begin(), vec.end(), [&](const int& i) { return i == target; }); bool found = it != vec.end(); std::cout << std::boolalpha << found << std::endl; // true return 0; } |
3.使用する std::binary_search
アルゴリズム
ベクトルがソートされている場合は、 std::binary_search
要素が指定された範囲内で見つかったかどうかに応じてブール値を返すアルゴリズム。二分探索アルゴリズムは、反復ごとに探索時間を半分に短縮できるため、非常に効率的かつ高速です。範囲の先頭と末尾、および検索する値の 3 つの引数を取ります。たとえば、値が次の値であるかどうかを確認したい場合、 7
ソートされたベクトルに存在します {-6, -3, 1, 2, 7, 8}
、あなたは呼び出すことができます std::binary_search
このような:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec { -6, -3, 1, 2, 7, 8 }; int target = 7; bool found = std::binary_search(vec.begin(), vec.end(), target); std::cout << std::boolalpha << found << std::endl; // true return 0; } |
4.使用する std::any_of
アルゴリズム
The std::any_of
述語が指定された範囲内のいずれかの要素に対して true を返す場合、アルゴリズムは true を返します。項目がベクトル内に存在するかどうかを確認するには、述語がターゲットとの一致を見つける必要があります。例えば:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; int target = -9; bool found = std::any_of(vec.begin(), vec.end(), [&](int &i) { return i == target; }); std::cout << std::boolalpha << found << std::endl; // true return 0; } |
5.使用する std::count
アルゴリズム
The std::count
このアルゴリズムは、指定された値が要素の範囲内に出現する回数をカウントし、指定された範囲内の指定された値に一致する要素の数を返します。カウント アルゴリズムを使用して C++ のベクトルに項目が存在するかどうかを確認するには、範囲の先頭と末尾、およびカウントする値を指定する必要があります。たとえば、値 30 がベクトル {10, 20, 30, 40, 50} に存在するかどうかを確認したい場合は、次のようなことができます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; int target = -9; bool found = std::count(vec.begin(), vec.end(), target) > 0; std::cout << std::boolalpha << found << std::endl; // true return 0; } |
The std::count
アルゴリズムは非常にシンプルで有益ですが、アルゴリズムよりも遅くなります。 std::find
アルゴリズムはリスト全体を横断するため、 std::find
最初の試合で停止します。
6.Boostライブラリの使用
プロジェクトでBoostライブラリを使用する場合は、 boost::algorithm::any_of_equal
ヘッダーファイルからの関数 <boost/algorithm/cxx11/any_of.hpp>
。戻ります true
範囲内の要素のいずれかが指定された値と等しい場合。その使用法を以下に示します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <vector> #include <boost/algorithm/cxx11/any_of.hpp> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; int target = -9; bool found = boost::algorithm::any_of_equal(vec, target); std::cout << std::boolalpha << found << std::endl; // true return 0; } |
7. C++20範囲の使用
C++ 20 ranges library さまざまなビューアダプタを含む、さまざまな要素を処理するためのコンポーネントを提供します。 The std::views::filter
述語に一致する要素の範囲にわたるビューを作成します。次のコード例は、この関数の呼び出しを示しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <ranges> #include <vector> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; int target = -9; auto match = vec | std::views::filter([&target](auto &v) { return v == target; }); std::vector<int> matches {match.begin(), match.end()}; bool found = matches.size() > 0; std::cout << std::boolalpha << found << std::endl; // true return 0; } |
C++20 より前では、以下のようなことを実行して、一致するコンテナーを構築できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec { 6, 3, 8, -9, 1, -2, 8 }; int target = -9; std::vector<int> matches; std::copy_if(vec.begin(), vec.end(), std::back_inserter(matches), [&](int v) { return v == target; }); bool found = matches.size() > 0; std::cout << std::boolalpha << found << std::endl; // true return 0; } |
これで、C++のvectorにアイテムが存在するかどうかを確認できます。