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:

Download  Run Code

 
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:

Download  Run Code

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:

Download  Run Code

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:

Download  Run Code

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:

Download  Run Code

 
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:

Download Code

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:

Download Code

 
Before C++20, you can do something like below to construct a container with matches:

Download  Run Code

That’s all about checking if an item is present in a vector in C++.