C++ STL Set Find Method

C++C++Beginner
Practice Now

Introduction

In this lab, you will learn about the find() method of STL Set in C++ programming. Set is used to store a unique list of values and automatically provides an ordering to its elements. By default, the ordering is in ascending order. The find() method returns an iterator to the element that is searched in the set container. If the element is not found, the iterator points to the position just after the last element in the set.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/comments -.-> lab-96236{{"`C++ STL Set Find Method`"}} cpp/variables -.-> lab-96236{{"`C++ STL Set Find Method`"}} cpp/data_types -.-> lab-96236{{"`C++ STL Set Find Method`"}} cpp/operators -.-> lab-96236{{"`C++ STL Set Find Method`"}} cpp/for_loop -.-> lab-96236{{"`C++ STL Set Find Method`"}} cpp/function_parameters -.-> lab-96236{{"`C++ STL Set Find Method`"}} end

Include necessary headers

In the first step we include the necessary headers iostream and set.

#include <iostream>
#include <set>

Use the standard namespace

In the second step we use the standard namespace.

using namespace std;

Declare the set

In the third step we declare a set to store integers.

set<int> s;

Insert elements into the set

In the fourth step we insert integer elements in the set.

s.insert(5);
s.insert(39);
s.insert(64);
s.insert(82);
s.insert(35);
s.insert(54);

Print the elements of the set

In the fifth step we print the elements of the set using an iterator.

for (auto it = s.begin(); it != s.end(); ++it) {
        cout << " " << *it;
}

Find an element in the set

In the sixth step we find an element in the set using the find() method.

auto it = s.find(39);

Print elements greater than the given element

In the seventh step we print the elements of the set that are greater than or equal to the element found in the set using the find() method.

for (; it != s.end(); ++it) {
        cout << " " << *it;
}

Erase an element from the set

In the eighth step we erase an element from the set using the erase() method.

s.erase(39);

Full Code

#include <iostream>
#include <set>

using namespace std;

int main() {
    // Declare a set
    set<int> s;

    // Insert elements into the set
    s.insert(5);
    s.insert(39);
    s.insert(64);
    s.insert(82);
    s.insert(35);
    s.insert(54);

    // Print the elements of the set
    for (auto it = s.begin(); it != s.end(); ++it) {
        cout << " " << *it;
    }

    // Find an element in the set
    auto it = s.find(39);

    // Print elements greater than the given element
    for (; it != s.end(); ++it) {
        cout << " " << *it;
    }

    // Erase an element from the set
    s.erase(39);

    return 0;
}

Summary

In this lab, you learned how to use the find() method of STL Set in C++ programming. You also learned how to declare a set, insert elements in a set, print the elements of a set, erase an element from a set, and find an element in a set using the find() method.

You also learned about the unique property and automatic ordering of elements of a set. You can use this basic knowledge of find() and sets to perform more complex operations with sets in C++.

Other C++ Tutorials you may like