Check if Element Exists in C++ Vector

In this article, we will learn how to search for an element in a vector and Check if an Element Exists in C++ Vector with examples using 3 ways

• By using the STL std::find() Algorithm..
• By using Range-Based for Loop.
• By using STL any_of() Algorithm.

1. std::find() to Check if Element Exists in C++ Vector

In this method, we are making use of the find() algorithm of STL. This find() method searches an element on a given range. We are giving the range from beginning to end so we can find it in the whole vector. The third argument is the element that you want to find.

Let us see this with below code example.

``````#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
vector<int> vec {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

cout <<"Vector Elements are :";
for (size_t i = 0; i < vec.size(); ++i)
{
cout << vec[i] << " ";
}
cout << endl;
int ElementToFind = 0;

cout << "Please enter the element you want to find:";
cin >> ElementToFind;

if (*find(vec.begin(), vec.end(), ElementToFind) == ElementToFind)
{
cout << "The element is present in vector";
}
else
{
cout << "The element is NOT present in vector";
}

cout << endl;

return 0;
}
``````

Output:
Vector Elements are :1 2 3 4 5 6 7 8 9 10
Please enter the element you want to find:4
The element is present in vector

Vector Elements are :1 2 3 4 5 6 7 8 9 10
Please enter the element you want to find:12
The element is NOT present in vector

2. Range-Based for Loop to Check if Element Exists in C++ Vector

In this method we are iterating over the vector by using the range based for loop. We are comparing each element with the element given by the user and if it match we found what we are looking for. If not then we display a message that we did not find what you asked us to find.

Let us see with the below code.

``````#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> vec {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
bool elementfound = false;
cout <<"Vector Elements are :";
for (size_t i = 0; i < vec.size(); ++i)
{
cout << vec[i] << " ";
}
cout << endl;
int ElementToFind = 0;

cout << "Please enter the element you want to find:";
cin >> ElementToFind;

for (const auto &item : vec)
{
if (item == ElementToFind)
{
elementfound = true;
break;
}
}

if (elementfound)
{
cout << "The element is present in vector";
}
else
{
cout << "The element is NOT present in vector";
}

cout << endl;

return 0;
}
``````

Output:
Vector Elements are :1 2 3 4 5 6 7 8 9 10
Please enter the element you want to find:5
The element is present in vector

Vector Elements are :1 2 3 4 5 6 7 8 9 10
Please enter the element you want to find:12
The element is NOT present in vector

3. any_of() Algorithm to Check if Element Exists in C++ Vector

In this method, we are going to make use of the any_of() algorithm of STL. This algorithm iterate from beginning to end and validate the
elements on a function that is passed as an argument. We are making use of the lambda function to compare the elements of the vector with the element given by the user to find.

Let us see this in code action.

``````#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
vector<int> vec {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
bool elementfound = false;
cout <<"Vector Elements are :";
for (size_t i = 0; i < vec.size(); ++i)
{
cout << vec[i] << " ";
}
cout << endl;
int ElementToFind = 0;

cout << "Please enter the element you want to find:";
cin >> ElementToFind;

if (any_of(vec.begin(), vec.end(), [&](const int& elem) { return elem == ElementToFind; }))
{
elementfound = true;
}

if (elementfound)
{
cout << "The element is present in vector";
}
else
{
cout << "The element is NOT present in vector";
}

cout << endl;

return 0;
}``````

Output:
Vector Elements are :1 2 3 4 5 6 7 8 9 10
Please enter the element you want to find:3
The element is present in vector

Vector Elements are :1 2 3 4 5 6 7 8 9 10
Please enter the element you want to find:55
The element is NOT present in vector