C++のvectorを指定して、指定された要素が含まれているかどうかを確認します。
vector内の要素の検索は、vectorがソートされていない限り、線形時間操作です。 The <algorithm>
ヘッダーは、検索に使用できる多くの機能を提供します。
1.使用する std::count
関数
最も簡単な解決策は、指定された値を持つvector内の要素の総数を数えることです。カウントがゼロ以外の場合、要素が見つかりました。これは、を使用して簡単に行うことができます std::count
関数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (std::count(v.begin(), v.end(), key)) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
2.使用する std::find
関数
効率的な解決策は、 std::find
指定された範囲の値を見つけるためのアルゴリズム。これは、 std::count
以来 count()
コンテナ全体をトラバースして要素数を取得します find()
一致する要素が見つかるとすぐに検索を停止します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (std::find(v.begin(), v.end(), key) != v.end()) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
3.使用する std::find_if
関数
使用することもできます std::find_if
述語を必要とするアルゴリズム。これは、検索が特定の条件を満たす必要がある場合に推奨されるアプローチです。たとえば、整数のvectorの最初の素数を見つけるか、文字列のvectorのある文字で終わる最初の文字列を見つけるように求められます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #include <vector> #include <algorithm> struct compare { int key; compare(int const &i): key(i) {} bool operator()(int const &i) { return (i == key); } }; int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (std::find_if(v.begin(), v.end(), compare(key)) != v.end()) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
4.使用する std::any_of
関数
これは、 std::find_if
アルゴリズム。述語を満たすシーケンスの最初の要素を指すイテレータを返す代わりに、述語がいずれかの要素に対してtrueを返し、それ以外の場合はfalseを返す場合はブール値trueを返します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #include <vector> #include <algorithm> struct compare { int key; compare(int const &i): key(i) {} bool operator()(int const &i) { return (i == key); } }; int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (std::any_of(v.begin(), v.end(), compare(key))) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
5.使用する std::none_of
関数
The std::none_of
アルゴリズムは正反対です std::any_of
つまり、述語が指定された範囲内のすべての要素に対してfalseを返す場合はtrueを返し、述語が少なくとも1つの要素に対してtrueを返す場合はfalseを返します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> #include <vector> #include <algorithm> struct compare { int key; compare(int const &i): key(i) {} bool operator()(int const &i) { return (i == key); } }; int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (!std::none_of(v.begin(), v.end(), compare(key))) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
6.使用する std::binary_search
関数
最後に、vectorが順序付けられている場合は、使用を検討してください std::binary_search
要素が指定された範囲内で見つかった場合はtrueを返し、それ以外の場合はfalseを返すアルゴリズム。 バイナリ検索 プロシージャはで実行されます O(log(n)) 時間。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7 }; int key = 6; if (std::binary_search(v.begin(), v.end(), key)) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
これで、C++でvectorに特定の要素が含まれているかどうかを判断できます。