unordered_set cend() in C++

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

Syntax:

std::unordered_set<int>::const_iterator std::unordered_set<int>::cend() const

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

Parameters: This function takes one argument (or no argument).

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;
    st.insert(9);
    st.insert(4);

    for (auto it = st.cbegin(); it != st.cend(); it++)
        cout << *it << " ";

    return 0;
}

Output:

4 9


Approach 2: When the functions take an argument.

C++

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

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

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

    return 0;
}


Output:

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


No comments:

Post a Comment