In this article, we will learn how to find an element in a Set. After that we will look a simple way to check if Set contains a given value or not.

To find an element in a set, the set class provides the find() function.

const_iterator find (const value_type& val) const;
iterator find (const value_type& val);

It accepts the value as an argument and returns an iterator pointing to that value. If the value does not exist in the set, then it will return an iterator pointing to the element next to the last element of set; this is the iterator returned by the end() function. We can also use this function to check if a value exists in a set or not.

Let’s see an example. We will create a set of integers and initialize it with certain values. Now, we will look for the value 23 in the set.

std::set<int> setObj = {11, 22, 23, 34, 45};

// Look for value 23
std::set<int>::iterator it = setObj.find(23);

// Check if iterator is valid
if (it != setObj.end())
{
    std::cout << "Value 23 found!" << std::endl;
}
else
{
    std::cout << "Value 23 not found!" << std::endl;
}

We had called the find() function on the set and had passed the value 23 to it. It had returned an iterator. Then, we had validated if this iterator was valid or not. For that, we had compared it with the value returned by the end function of the set. If it wasn’t equal to that, then it meant the iterator was valid. In that case, the iterator was valid, so it had returned an iterator to the value 23 in the set. We could even access the value, but the iterator that was returned here was a const iterator, so we couldn’t change the value using it. We can not modify the value in the set.

Let’s see another example. We will look for a value that does not exist in the set. We will pass 43 to the find function, and it will return an iterator. We will then compare the iterator with the end function. Since the value does not exist in the set, it will match the value returned by the end function. This way, we can look for a value in a set.

#include <iostream>
#include <set>

int main()
{
    std::set<int> setObj = {11, 22, 23, 34, 45};

    // Look for value 23
    std::set<int>::iterator it = setObj.find(23);

    // Check if iterator is valid
    if (it != setObj.end())
    {
        std::cout << "Value 23 found!" << std::endl;
    }
    else
    {
        std::cout << "Value 23 not found!" << std::endl;
    }

    // Look for value 43
    it = setObj.find(43);

    // Check if iterator is valid
    if (it != setObj.end())
    {
        std::cout << "Value 43 found!" << std::endl;
    }
    else
    {
        std::cout << "Value 43 not found!" << std::endl;
    }

    return 0;
}

Output:

Value 23 found!
Value 43 not found!

Check if Set Contains a Value using contains() from C++20

C++20 introduced a function named contains(). It accepts a value as an argument and returns a boolean value. If the element exists in the set, it will return true. If the value does not exist in the set, it will return false. We can use it to check if a set contains a given value or not.

Let’s see an example. We’ll create a set of integers and initialize it with certain values. Then, we will check if the value 23 exists in the set or not using the contains() function. If you just want to check if a set contains a particular value, you can use the new contains function.

#include <iostream>
#include <set>

int main()
{
    std::set<int> setObj = {11, 22, 23, 34, 45};

    // Checking if value 23 exists using the contains function
    if (setObj.contains(23))
    {
        std::cout << "Value 23 exists in the set!" << std::endl;
    }
    else
    {
        std::cout << "Value 23 does not exist in the set!" << std::endl;
    }

    return 0;
}

Output:

Value 23 exists in the set!

To compile this code, you need a compiler that supports C++20. If you are using linux, then you can use following command,

g++ --std=c++20 example.cpp 

Summary

We learned about two ways to find values in a Set in C++.

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.