In this article, we will discuss different ways to check if a vector contains an element or not.

Table of Contents

C++: Check if an item exits in vector using find()

In C++, we have a STL Algorithm find(start, end, item), it accepts three arguments,

  • start -> Iterator pointing to the start of a range
  • end -> Iterator pointing to the end of a range
  • item -> The element that need to be searched in range.

It iterates over the elements in the range from start to end-1 and looks for the element in range that is equal to item. As soon as it finds the first match, it returns the iterator of that element. If no match is found then it returns the iterator pointing to the end of range.

So, to check if an element exist in vector or not, we can pass the start & end iterators of vector as initial two arguments and as the third argument pass the value that we need to check. If element exists in the vector, then it will return the iterator pointing to that element. Whereas, if element does not exist in the vector, then it returns the iterator pointing to the end of vector.

Using this concept, we have created a generic function to check if vector contains the element or not,

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

using namespace std;

template <typename T>
bool contains(vector<T> vec, const T & elem)
{
    bool result = false;
    if( find(vec.begin(), vec.end(), elem) != vec.end() )
    {
        result = true;
    }
    return result;
}

int main()
{
    vector<int> numbers{ 11, 22, 33, 44, 55, 66};

    int elem = 44;

    if(contains(numbers, elem))
    {
        cout<<"Element exist in vector" <<endl;
    }
    else
    {
        cout<<"Element does not exist in vector" <<endl;
    }
    
    return 0;
}

Output:

Element exist in vector

This generic function can be used with any type of vector to check if contains an element or not.

C++: Check if item exits in vector using range based for loop

Iterate over all the elements in vector using range based for loop and check any element is equal to the item provided. For example,

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

using namespace std;

template <typename T>
bool contains(vector<T> vec, const T & elem)
{
    bool result = false;
    for (auto & x : vec)
    {
        if (x == elem)
        {
            result = true;
            break;
        }
    }
    return result;
}

int main()
{
    vector<int> numbers{ 11, 22, 33, 44, 55, 66};

    int elem = 44;

    if(contains(numbers, elem))
    {
        cout<<"Element exist in vector" <<endl;
    }
    else
    {
        cout<<"Element does not exist in vector" <<endl;
    }
    
    return 0;
}

Output:

Element exist in vector

This generic function can be used with any type of vector to check if contains an element or not.

C++: Check if item exits in vector and get index position

In the both the previous examples, we tested if the vector contains the element or not. But if you want to know that where exactly the element exist in the vector, then we need to iterate over vector using indexing and look for the element and returns a pair of bool & int. If element is found in the vector then it returns the pair of <true, index of element>. Whereas, if element doesn’t exist in the vector then it returns a pair of <false, -1>. For example,

#include<iostream>
#include<vector>

using namespace std;

template <typename T>
pair<bool, int> contains(vector<T> vec, const T & elem)
{
    pair<bool, int> result(false, -1);
    for(int i = 0; i < vec.size(); i++)
    {
        if (vec[i] == elem)
        {
            result.first = true;
            result.second = i;
            break;
        }
    }
    return result;
}

int main()
{
    vector<int> numbers{ 11, 22, 33, 44, 55, 66};

    int elem = 44;
    auto result = contains(numbers, elem); 

    if(result.first)
    {
        cout<<"Element exist in vector at index : " << result.second<<endl;
    }
    else
    {
        cout<<"Element does not exist in vector" <<endl;
    }
    
    return 0;
}

Output:

Element exist in vector at index : 3

This generic function can be used with any type of vector to check if contains an element or not.

C++: Check if item exits in vector using any_of()

STL provides an another algorithm any_of() i.e. any_of(start, end, callback). It accepts three arguments,

  • start -> Iterator pointing to the start of a range
  • end -> Iterator pointing to the end of a range
  • callback -> The unary function that returns a bool value

It iterates over all elements in the range from start to end-1 and calls the callback function on each element. For any element in the range, if callback returns the true then any_of() stops the iteration and returns true, otherwise returns false in end.

Let’s use this to check if vector contains the element or not,

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

using namespace std;

template <typename T>
bool contains(vector<T> vec, const T & elem)
{
    return any_of(vec.begin(), vec.end(), [&](const auto & x){
        return x == elem;
    });
}

int main()
{
    vector<int> numbers{ 11, 22, 33, 44, 55, 66};

    int elem = 44;

    if(contains(numbers, elem))
    {
        cout<<"Element exist in vector" <<endl;
    }
    else
    {
        cout<<"Element does not exist in vector" <<endl;
    }
    
    return 0;
}

Output:

Element exist in vector

This generic function can be used with any type of vector to check if contains an element or not.

Summary

We learned about several ways to test if the vector contains a given element or not.