This tutorial will discuss about the usage of vector’s contains() function.

Many times we encounter a scenario where we need to check if a vector contains a given element or not. Ironically vector does not have any function contains(), therefore we have created a generic function, that you can use with any kind of vector to check if the given vector contains an element or not. The code for the contain() function is as follows,

// Check if vector contains an element
template <typename T>
bool contains(
        const std::vector<T>& vecObj,
        const T& element)
{
    // Get the iterator of first occurrence
    // of given element in vector
    auto it = std::find(
                    vecObj.begin(),
                    vecObj.end(),
                    element) ;

    return it != vecObj.end();
}

It is a template function, which accepts a vector as the first argument and a value as the second argument. It will return true, if vector contains the given value. Otherwise it will return false.

Inside this contains() function we are using the STL’s find() function. The find() function accepts three arguments,

  • First is the iterator pointing to the start of the range.
  • Second is the iterator pointing to the end of the range.
  • Third is the element that need to be checked.

It iterates over all the elements in the range and look for any element that matches the given value. As soon as it finds a match, it returns an iterator pointing to the location of that element. But if the element does not exist in the vector or range it will return an iterator pointing to the end of the range. By checking the returned value of the find() function, we can confirm if the vector contains a given element or not.

Let us see detailed example where we will create a vector with some hard coded values, and then we will use the contains() function to check if the vector contains a given value or not.

Let’s see the complete example,

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

// Check if vector contains an element
template <typename T>
bool contains(
        const std::vector<T>& vecObj,
        const T& element)
{
    // Get the iterator of first occurrence
    // of given element in vector
    auto it = std::find(
                    vecObj.begin(),
                    vecObj.end(),
                    element) ;

    return it != vecObj.end();
}

int main()
{
    // Create a vector of numbers
    std::vector<int> vecOfNumbers {21, 22, 23, 24, 25, 26};

    // Check if value 100 exists in the vector
    if (contains(vecOfNumbers, 100))
    {
        std::cout<<" Vector contains the value 100" << std::endl;
    }
    else
    {
        std::cout<<" Vector does not contains the value 100" << std::endl;;
    }


    // Check if value 24 exists in the vector
    if (contains(vecOfNumbers, 24))
    {
        std::cout<<" Vector contains the value 24" << std::endl;
    }
    else
    {
        std::cout<<" Vector does not contains the value 24" << std::endl;;
    }

    return 0;

}

Output :

Vector does not contains the value 100
 Vector contains the value 24

Summary

Today we learned about a way to create vector contains() function in C++. Thanks.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.