In C++, the vector does not have a specific function to find the item in a vector. The algorithm library has a find function that can be used to find whether an item is present in a vector or not, if the item is present you can get the indexed number of it as well. The header algorithm provides various functions to find an item in a vector. In order to use the find method you must have to add the #include <algorithm> in the header file.
In this detailed guide, we will learn different methods to find out if an item is present in a vector in C++.
Methods to Find Out if an Item is Present in a Vector in C++
Following are the functions that you can use in C++ to find an element within a vector under the algorithm header:
1: Using count() Function
One of the simplest functions for finding the specific element in a vector is using the count function. This function finds the total number of elements present in a vector, if the count is greater than zero, the element is present in the vector.
In the below example, we have used the count function to determine if a specific given number is present in the numbers vector.
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> numbers = { 1, 3, 5, 7, 9, 11 };
int key;
std::cout << "Enter a number to search: ";
std::cin >> key;
if (std::count(numbers.begin(), numbers.end(), key)) {
std::cout << "The number is present";
}
else {
std::cout << "The number is not present";
}
return 0;
}
2: Use find() Function
Find function in C++, is a more convenient way to find out an element in a vector. It is the fastest way and better the count because count searches the number in the whole vector and the find function stops as soon as it finds the specific number. The find function accepts three arguments:
The iterating point is to start the range, the end is to stop the range and the item is the element that needs to be searched in a vector.
The following code uses the find function to check if a specific number is present in the vector or not:
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> numbers = { 1, 3, 5, 7, 9, 11 };
int key;
std::cout << "Enter the key: ";
std::cin >> key;
if (std::find(numbers.begin(), numbers.end(), key) != numbers.end()) {
std::cout << "number found";
}
else {
std::cout << "number not found";
}
return 0;
}
3: Use any_of() function
The algorithm library provides another function for finding an element in a vector. It takes three parameters:
The start is the start of the iterating point, the end is the ending point of the iteration and call back is the unary function, it returns the bool value either true or false.
#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> numbers = { 1, 3, 5, 7, 9, 11 };
int num;
std::cout << "enter number: ";
std::cin >> num;
if (std::any_of(numbers.begin(), numbers.end(), compare(num))) {
std::cout << "number found";
}
else {
std::cout << "number not found";
}
return 0;
}
4: Using Range-based for Loop
The range-based loop can also be used to search the specific element in a vector. It is a generic function and can be used to search for items in any type of element.
The following example code uses the range-based loop to search key 4 exists in the vector numbers or not:
#include <vector>
#include <algorithm>
using namespace std;
template <typename T>
bool contains(vector<T> vec, const T & key)
{
bool result = false;
for (auto & x : vec)
{
if (x == key)
{
result = true;
break;
}
}
return result;
}
int main()
{
vector<int> numbers{ 1, 3, 5, 7, 9, 11};
int num;
cout << "Enter a number to search: ";
cin >> num;
if(contains(numbers, num))
{
cout<<"Number exists in vector" <<endl;
}
else
{
cout<<"Number does not exist in vector" <<endl;
}
return 0;
}
Bottom Line
There are several functions provided by the algorithm header in C++ to find if an item is present in a vector. These include count(), find(), any_of(), and range-based for loops. By using these functions, you can easily find out if a specific item is present in a vector or not.