C++ Unordered_set:: cbegin() Function
The C++std::unordered_set::cbegin() function is used to return the const_iterator pointing to the first element in the unordered_set container. The returned const_iterator becomes equal to the end(), if the unordered_set is empty.
A const_iterator is an iterator that points to a const value (like a pointer) over elements and provides access to each individual element. const_iterators are not allowed to modify pointed elements available in the unordered_set container.
Syntax
Following is the syntax of std::unordered_set::cbegin() function.
const_iterator cbegin() const noexcept; or const_local_iterator cbegin ( size_type n ) const;
Parameters
- n − It indicates the bucket number that must be less than bucket_count.
Return Value
This function returns an const iterator pointing to the first element in the unordered_set container.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of unordered_set::cbegin() function.
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main () {
std::unordered_set<std::string> myUset =
{"100","200","300","400","500","600","700","800"};
cout<<"Contents of the myUset are: "<<endl;
for(auto it: myUset)
cout<<it<<" ";
cout<<"\nAn iterator of the first is: ";
auto it = myUset.cbegin();
cout<<*it;
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
Contents of the myUset are: 700 600 500 800 400 300 200 100 An iterator of the first is: 700
Example 2
Consider the following example, where we are going to use the loop- inside the cbegin() function to display the element of the container in a range.
#include <iostream>
#include <string>
#include <unordered_set>
int main () {
std::unordered_set<std::string> myUset =
{"100","200","300","400","500"};
std::cout << "myUset contains:";
for ( auto it = myUset.cbegin(); it != myUset.cend(); ++it )
std::cout << " " << *it;
std::cout << std::endl;
return 0;
}
Output
If we run the above code it will generate the following output −
myUset contains: 500 400 300 200 100
Example 3
In the following example, we are going to use the cbegin() function that accepts i as a parameter to return the elements of each bucket.
#include <iostream>
#include <string>
#include <unordered_set>
int main () {
std::unordered_set<std::string> myUset = {"100", "200", "300", "400", "500"};
std::cout << "myUset's buckets contain:\n";
for ( unsigned i = 0; i < myUset.bucket_count(); ++i) {
std::cout << "bucket #" << i << " contains:";
for ( auto local_it = myUset.cbegin(i); local_it!= myUset.cend(i); ++local_it )
std::cout << " " << *local_it;
std::cout << std::endl;
}
return 0;
}
Output
Following is the output of the above code −
myUset's buckets contain: bucket #0 contains: bucket #1 contains: 400 bucket #2 contains: 500 bucket #3 contains: bucket #4 contains: 100 bucket #5 contains: bucket #6 contains: bucket #7 contains: bucket #8 contains: bucket #9 contains: bucket #10 contains: 300 bucket #11 contains: 200 bucket #12 contains:
Example 4
Following is the example, where we are going to use the cbegin() function to get the const iterator pointing to the first element.
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
int main () {
unordered_set<int> myUset = {10, 20, 30, 40, 50};
cout << "Iterator pointing to the first element of the bucket 4 is: ";
auto it = myUset.cbegin(4);
cout<<*it<<endl;
return 0;
}
Output
Output of the above code is as follows −
Iterator pointing to the first element of the bucket 4 is: 30