Check if Set Contains a Value in C++

In this article, we will discuss different ways to check if a Set contains a given value or not in C++.

Introduction

Suppose we have a set of strings i.e.

// Set of Strings
std::set<std::string> setOfStrs = { "the",
                                    "is",
                                    "of",
                                    "from",
                                    "at",
                                    "hello",
                                    "the" };

Now we want to check if a given string exists in this set or not. Let’s do this using two different techniques i.e.

Check if Set Contains an element using set::find()

The std::set class provides a member function find() to search a given element in set i.e.

iterator find (const value_type& val);

It accepts the element as an argument and looks for that element in the set. If the element is found in the set, then it returns an iterator pointing to that element in the set, otherwise it returns an iterator pointing to the end of set i.e. iterator returned by the set::end() function.

Let’s use set::find() function to check if “at” exists in above set or not i.e.

// search for the given string in set
std::set<std::string>::iterator it = setOfStrs.find("at");

// Check if Set contains the value "at"
if (it != setOfStrs.end())
{
    std::cout << "String 'at' found in set" << std::endl;
}

We can access the value of set using the iterator returned by the set::find() function. But. we can not modify the element pointed by this iterator, because this element is used for indexing inside the set, changing it will make the indexing wrong inside the set. Therefore we can access the element through iterator but cannot modify it in Set.



    Check if Set Contains an element using set::count()

    std::set provides a member function count() to find the occurrence count of a given element in set i.e.

    size_type count (const value_type& val) const;

    It accepts the element as argument and looks for its occurrence count in the set. As std::set can contain only unique elements, so occurrence count of the given element can be only 0 or 1 only i.e,

    If set contains the element then count will be 1, otherwise count will be 0.

    Let’s use set::count() function to check if “hello” exists in above set or not i.e.

    // Check if Set contains the value "hello"
    if (setOfStrs.count("hello") != 0)
    {
        std::cout << "'hello' found in set" << std::endl;
    }

    Checkout the complete example as follows,

    #include <iostream>
    #include <string>
    #include <set>
    
    
    int main()
    {
        // Set of Strings
        std::set<std::string> setOfStrs = { "the",
                                            "is",
                                            "of",
                                            "from",
                                            "at",
                                            "hello",
                                            "the" };
    
        // search for the given string in set
        std::set<std::string>::iterator it = setOfStrs.find("at");
    
        // Check if Set contains the value "at"
        if (it != setOfStrs.end())
        {
            std::cout << "String 'at' found in set" << std::endl;
        }
    
    
        // Check if Set contains the value "hello"
        if (setOfStrs.count("hello") != 0)
        {
            std::cout << "'hello' found in set" << std::endl;
        }
    
        return 0;
    }

    Output:

    String 'at' found in set
    'hello' found in set

    Check if Set contains an element using set::contains() in C++20

    In C++20, a new member function of Set class has been introduced. It accepts an element as argument, and returns true if the set contains the given value, otherwise returns false.

    Let’s see the complete example,

    #include <iostream>
    #include <string>
    #include <set>
    
    
    int main()
    {
        // Set of Strings
        std::set<std::string> setOfStrs = { "the",
                                            "is",
                                            "of",
                                            "from",
                                            "at",
                                            "hello",
                                            "the" };
    
        // Check if Set contains the value "hello"
        if (setOfStrs.contains("hello"))
        {
            std::cout << "'hello' found in set" << std::endl;
        }
    
        return 0;
    }

    Output:

    'hello' found in set

    Important Point: You need to use C++20 compiler for using the set::contains() function in C++.

    Set find() vs count()

    Both set::find() and set:: count() helps to check the existence of an element in set. But why two different method for same thing ?

    Complexity wise both find() and count() are same. Main difference is that set::find() returns the iterator of the given element. Iterator is useful if we have user define objects in set, instead of primitive data types.

    As set::find() returns the iterator, We can perform operations on the object pointed by iterator, also we can modify the mutable content of that object

    Suppose we have a struct Student i.e.

    // Student with ID and name
    // Name is mutable, so can be modified by const object
    struct Student
    {
    	int mId;
    	mutable std::string mName;
    	Student(int id, std::string name) :
    			mId(id), mName(name)
    	{
    	}
    };

    A Functor / Comparator, to compare Student objects using ID i.e.

    // Student comparator / Functor
    // Compare objects with ID only
    struct StudentComp
    {
    	bool operator()(const Student& lhs, const Student& rhs) const
    	{
    		return lhs.mId < rhs.mId;
    	}
    };

    Let’s Create a set of Student objects i.e.

    // Set of Student class objects
    std::set<Student, StudentComp> setOfStudents ={ Student(1, "Shaun"),
                                                    Student(2, "Riti"),
                                                    Student(3, "Aadi"),
                                                    Student(4,"Vicky"),
                                                    Student(5, "Renu") };

    Find a Student object with id 3 using set::find() and modify its name, because mName is mutable and can be modifies eve with const objects i.e.

    // Find a student with id 3
    std::set<Student, StudentComp>::iterator it = setOfStudents.find( Student(3, ""));
    
    // Check if Iterator is valid
    if (it != setOfStudents.end())
    {
        // Modify the mutuable content which is not
        // used in comparision or indexing
        it->mName = "Aarav";
        std::cout << it->mId << " :: " << it->mName << std::endl;
    }

    Now search for an element with count() i.e.

    // Check if a student with ID 3 exists in set
    // Not possible to fetch the iterator with count(),
    // It returns the count only
    if (setOfStudents.count(Student(3, "")) != 0)
    {
        std::cout << it->mId << " Exist in Set" << std::endl;
    }

    We will not get any iterator with set::count(), so its not possible to perform any operation or modify content with count().

    The Complete example is as follows,

    #include <iostream>
    #include <string>
    #include <set>
    
    // Student with ID and name
    // Name is mutable, so can be modified by const object
    struct Student
    {
    	int mId;
    	mutable std::string mName;
    	Student(int id, std::string name) :
    			mId(id), mName(name)
    	{
    	}
    };
    
    // Student comparator / Functor
    // Compare objects with ID only
    struct StudentComp
    {
    	bool operator()(const Student& lhs, const Student& rhs) const
    	{
    		return lhs.mId < rhs.mId;
    	}
    };
    
    int main()
    {
        // Set of Student class objects
        std::set<Student, StudentComp> setOfStudents ={ Student(1, "Shaun"),
                                                        Student(2, "Riti"),
                                                        Student(3, "Aadi"),
                                                        Student(4,"Vicky"),
                                                        Student(5, "Renu") };
    
    
    
        // Find a student with id 3
        std::set<Student, StudentComp>::iterator it = setOfStudents.find( Student(3, ""));
    
        // Check if Iterator is valid
        if (it != setOfStudents.end())
        {
            // Modify the mutuable content which is not
            // used in comparision or indexing
            it->mName = "Aarav";
            std::cout << it->mId << " :: " << it->mName << std::endl;
        }
    
    
        // Check if a student with ID 3 exists in set
        // Not possible to fetch the iterator with count(),
        // It returns the count only
        if (setOfStudents.count(Student(3, "")) != 0)
        {
            std::cout << it->mId << " Exist in Set" << std::endl;
        }
    
    
        return 0;
    }

    Output:

    3 :: Aarav
    3 Exist in Set

    Summary

    We learned about two different ways to check if an element exists in Set or not in C++.

    Scroll to Top