Check if an item is present in a vector in C++
This post will discuss how to check if an item is present in a vector in C++.
1. Using std::find algorithm
One way to check if an item is present in a vector in C++ is to use the find() algorithm from the <algorithm> header. This algorithm searches for a given value in a range of elements, and returns an iterator to the first occurrence of the value. If the value is not found, it returns an iterator to the end of the range. To use the find algorithm to check if an item is present in a vector in C++, you need to provide three arguments: the beginning and the end of the range, and the value that you want to search for. For example:
|
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; } |
The advantage of using std::find is that it stops searching as soon as a match is found. That means it only returns an iterator to the first occurrence of the value. Therefore, you should not use the find algorithm to find multiple occurrences of the value in the vector.
2. Using std::find_if algorithm
Another way to check if an item is present in a vector in C++ is to use the std::find_if algorithm from the <algorithm> header. The std::find_if algorithm returns an iterator to the first element in the specified range for which the predicate returns true. To check whether an item is present in a vector, the predicate should match the current element with the target, as shown below:
|
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. Using std::binary_search algorithm
If the vector is sorted, you can use the std::binary_search algorithm that returns a boolean value depending upon whether the element is found in the specified range or not. The binary search algorithm is very efficient and fast, as it can reduce the search time by half in each iteration. It takes three arguments: the beginning and the end of the range, and the value that you want to search for. For example, if you want to check if the value 7 is present in the sorted vector {-6, -3, 1, 2, 7, 8}, you can call std::binary_search like this:
|
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. Using std::any_of algorithm
The std::any_of algorithm returns true if the predicate returns true for any of the elements in the specified range. To check whether an item is present in a vector, the predicate should find a match with the target. For example:
|
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. Using std::count algorithm
The std::count algorithm counts how many times a given value appears in a range of elements, and returns the count of elements matching the specified value in the specified range. To use the count algorithm to check if an item is present in a vector in C++, you need to provide the beginning and the end of the range, and the value that you want to count. For example, if you want to check if the value 30 is present in the vector {10, 20, 30, 40, 50}, you can do something like this:
|
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 algorithm is very simple and informative to use, but it is slower than the std::find algorithm since it traverses the whole list, whereas std::find stops on the first match.
6. Using Boost library
If you use the Boost library in your project, you may use the boost::algorithm::any_of_equal function from header file <boost/algorithm/cxx11/any_of.hpp>. It returns true if any of the elements in the range are equal to the specified value. Its usage is demonstrated below:
|
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. Using C++20 ranges
The C++20 ranges library provides components for dealing with ranges of elements, including a variety of view adaptors. The std::views::filter creates a view over the range of elements that matches a predicate. The following code example shows invocation for this function:
|
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; } |
Before C++20, you can do something like below to construct a container with matches:
|
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; } |
That’s all about checking if an item is present in a vector in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)