TheC++std::unordered_set::cend() function is used to return a const_iterator pointing to the past-the-last element in the unordered_set container. The const_iterator returned by the cend does not point to any element but the position followed by the last element in the unordered_set container.
In STL CPP, 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_terators are not allowed to modify pointed elements available in the unordered_set container.
SyntaxThe unordered_set::cend() function is similar to the unordered_set::end() function. The cend() function returns only a const_iterator, whereas the end() function returns only an iterator.
Following is the syntax of std::unordered_set::cend() function.
const_iterator cend() const noexcept; or const_local_iterator cend ( size_type n ) const;Parameters
This function returns a const_iterator pointing to the position followed by the last element in the unordered_set container.
Example 1Let's look at the following example, where we are going to use the loop 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
Let us compile and run the above program, this will produce the following result −
myUset contains: 500 400 300 200 100Example 2
Consider the following example, where we are going to use the cend() function that accepts i as 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
If we run the above code it will generate the following output −
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 3
In the following example, we are going to use the cend() function to get the elements of the unordered_set container by iterating over the unordered_set using the while loop.
#include <iostream> #include <unordered_set> #include <string> using namespace std; int main() { unordered_set<int> myset = { 10,20,30,40,50 }; cout<<"Elements of myUset are: "<<endl; unordered_set<int>::const_iterator it; // declare an iterator it = myset.begin(); while (it != myset.cend()) { cout << *it << "\n"; ++it; // iterate to the next element } cout << endl; return 0; }Output
Following is the output of the above code −
Elements of myUset are: 50 40 30 20 10
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4