unordered_set end() in C++

end(): This function is available in the File: unordered_set.h

Syntax:

std::unordered_set<int>::iterator std::unordered_set<int>::end()

This function returns a read-only (constant) iterator that points one past the last element in the unordered_set.

The iterator returned by the end does not point to any element, but to the position that follows the last element in the unordered_set container. Thus, the value returned shall not be dereferenced.

Parameters: This function takes one parameter (or it does not take any parameter).

File: unordered_set.h

Approach 1: When the function does not take any argument.

C++

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

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

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

    return 0;
}

Output:

12 10 45 3


Approach 2: When the function takes an argument.

C++

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

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

    for (int i = 0; i < st.bucket_count(); i++)
    {
        cout << "Bucket " << i << " contains: ";
        for (auto it = st.begin(i); it != st.end(i); it++)
            cout << *it << " ";
        cout << "\n";
    }

    return 0;
}


Output:

Bucket 0 contains: Bucket 1 contains: Bucket 2 contains: Bucket 3 contains: 10 45 3 Bucket 4 contains: Bucket 5 contains: 12 Bucket 6 contains:


No comments:

Post a Comment