C++ end()

Anonymous contributor's avatar
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.

  • 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

Syntax

unordered_set_name.end(n);

Parameters:

  • n (size_type, optional): The bucket index. Must be less than bucket_count().

Return value:

  • If n isn’t provided, returns an iterator pointing to the past-the-end position of the unordered_set.
  • If n is provided, returns a local_iterator pointing to the past-the-end position in bucket n. 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:

20
5
10
15

Note: The output order may vary because unordered_set does 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:

Code
Output

All contributors

Contribute to 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