C++ end()
Anonymous contributor
Published Jan 30, 2026
Contribute to Docs
The end() method returns an iterator that points to the past-the-end position of an unordered_set. This iterator marks one past the last element and cannot be dereferenced. Because unordered_set does not maintain a defined order, iteration proceeds in the container’s internal hash-table order.
Syntax
unordered_set_name.end(n);
Parameters:
n(size_type, optional): The bucket index. Must be less thanbucket_count().
Return value:
- If
nisn’t provided, returns an iterator pointing to the past-the-end position of theunordered_set. - If
nis provided, returns alocal_iteratorpointing to the past-the-end position in bucketn. If the bucket is empty,begin(n) == end(n).
Example
In this example, iteration runs from begin() to end() to print every element in the unordered_set:
#include <iostream>#include <unordered_set>using namespace std;int main() {unordered_set<int> unique_numbers = {10, 5, 20, 15};for (auto it = unique_numbers.begin(); it != unique_numbers.end(); ++it) {cout << *it << "\n";}return 0;}
Here is a possible output:
2051015
Note: The output order may vary because
unordered_setdoes not store elements in any defined sequence.
Codebyte Example
This example retrieves iterators for a specific bucket in the unordered_set and prints all elements stored in that bucket:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours