unordered_set find() in C++

find(): This function is available in the File: unordered_set.h. This function is a build-in function in STL of C++.

Syntax:

std::unordered_set<int>::iterator std::unordered_set<int>::find(const int &__x)

This function takes one argument as its parameter. This function tries to locate an element in an unordered_set.

Parameters: One parameter is required for this method.

__x – Element to be located.

Returns: Iterator pointing to sought-after element, or end() if not found. This function takes a key and tries to locate the element with which the key matches.

File: unordered_set.h

Approach 1: When an element with the given key is present in the unordered_set.

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    unordered_set<intst = {9543210};

    int x = 4;
    unordered_set<int>::iterator it = st.find(x);

    if (it == st.end())
        cout << "Element is not found\n";
    else
        cout << "Element is found\n";

    return 0;
}

Output:

Element is found


Approach 2: When an element with the given key is not present in the unordered_set.

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    unordered_set<intst = {9543210};

    int x = 40;
    unordered_set<int>::iterator it = st.find(x);

    if (it == st.end())
        cout << "Element is not found\n";
    else
        cout << "Element is found\n";

    return 0;
}

Output:

Element is not found

No comments:

Post a Comment