C++ unordered_set - end() Function
The C++ unordered_set::end function returns the iterator pointing to the past-the-last element of the unordered_set container or one of its bucket. The past-the-last element of the set is the theoretical element that follows the last element. It does not point to any element, and hence could not be dereferenced.
Note: There is no guarantee on how elements are ordered in the unordered_set (or the bucket). But the range that goes from begin to end contains all elements of the unordered_set container (or the bucket).
Syntax
//container iterator iterator end() noexcept; const_iterator end() const noexcept; //bucket iterator local_iterator end ( size_type n ); const_local_iterator end ( size_type n ) const;
Parameters
n |
Specify the bucket number. It should be lower than the bucket_count. |
Return Value
An iterator to the past-the-last element of the unordered_set container or one of its bucket. If the sequence object is constant qualified, the function returns a const_iterator, else returns an iterator.
Time Complexity
Constant i.e, Θ(1)
Example:
In the example below, the unordered_set::end function returns the iterator pointing to the past-the-last element of the unordered_set container or one of its bucket. The unordered_set::end function is often used with unordered_set::begin function to specify a range including all elements of the unordered_set container or one of its bucket.
#include <iostream> #include <unordered_set> using namespace std; int main (){ unordered_set<int> uSet{55, 25, 128, 5, 72, -500, 45, 24, -20}; cout<<"The uSet contains: "; for(auto it = uSet.begin(); it != uSet.end(); ++it) cout<<*it<<" "; cout<<"\n\nThe uSet's bucket contains: "; for(unsigned int i = 0; i < uSet.bucket_count(); i++) { cout<<"\nThe bucket #"<<i<<" contains: "; for(auto it = uSet.begin(i); it != uSet.end(i); ++it) { cout<<*it<<" "; } } return 0; }
The output of the above code will be:
The uSet contains: 24 45 72 5 -20 128 25 -500 55 The uSet's bucket contains: The bucket #0 contains: -500 55 The bucket #1 contains: 45 The bucket #2 contains: 24 The bucket #3 contains: 25 The bucket #4 contains: The bucket #5 contains: 5 The bucket #6 contains: 72 The bucket #7 contains: -20 128 The bucket #8 contains: The bucket #9 contains: The bucket #10 contains:
❮ C++ <unordered_set> Library