TheC++std::unordered_set::end() function is used to return an iterator pointing to the past-the-last element in the unordered_set container. The iterator returned does not point to any element but the position followed by the last element in the unordered_set container.
An iterator is an object (like a pointer) over elements that provides access to each individual element.
SyntaxThe unordered_set::end() function is similar to the unordered_set::cend() function, where the end() function returns only an iterator and the cend() function returns a const iterator.
Following is the syntax of std::unordered_set::end() function.
iterator end() noexcept; const_iterator end() const noexcept; or local_iterator end (size_type n); const_local_iterator end (size_type n) const;Parameters
This function returns an iterator pointing to the position followed by the last element in the unordered_set container.
Example 1In the following example, we are going to use the loop inside the end() function and displaying the elements 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.begin(); it != myUset.end(); ++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 100Example 2
Consider the following example, where we are going to use the end() function that accepts i as a parameter and return the element 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.begin(i); local_it!= myUset.end(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 3
Let's look at the following example, where we are going to use the end() function and getting the elements of the container by iterating over the unordered_set.
#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.end()) { cout << *it << "\n"; ++it; // iterate to the next element } cout << endl; return 0; }Output
Output of the above code is as follows −
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