Home C++ Unordered Maps Functions end() C++ std::unordered_map end() Function Definition In C++, std::unordered_map::end() returns an iterator to the position after the last pair . It is a marker, not a real pair, so you must never dereference it .
You use it to know when a loop is finished, and to test the result of find(), which returns end() when nothing matches. Include <unordered_map> to use it.
This is a member of std::unordered_map<Key, T>, a hash table of key and value pairs . Each key is unique. Find, insert, and erase are usually average constant time. Unlike std::map, keys are not sorted and iteration order is unspecified. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax Call end with empty brackets. Use it as the stop value in a loop or as a “not found” test.
if (m.find("Zoe") == m.end()) {
// not found
}
for (auto it = m.begin(); it != m.end(); ++it) {
// ...
} Call What it does m.end()Returns an iterator one past the last pair. On a const map it returns a const_iterator.
unordered_map has no reverse iterators. Do not write *m.end() and do not use m.end() - 1, because these iterators do not support subtraction.
Library declarations iterator end() noexcept;
const_iterator end() const noexcept; Parameters Parameter Description None end() takes no parameters. Call it with empty brackets.
Return Value Return Value Description iteratorAn iterator to the position after the last pair. It is equal to begin() if the unordered_map is empty.
C++ Examples 1. Test a search result 2. Get the last pair 3. An empty map Example 1 — end() Means Not Found Compare the result of find() with end().
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, int> age = {{"Mia", 21}, {"Raj", 25}};
for (const std::string name : {"Raj", "Zoe"}) {
auto it = age.find(name);
if (it == age.end()) {
std::cout << name << ": not found\n";
} else {
std::cout << name << ": " << it->second << '\n';
}
}
return 0;
} How It Works find() returns an iterator. It cannot return nullptr, so end() is the agreed way to say “nothing found”.For "Raj", the iterator points at a real pair, so it is not equal to end() and the value 25 is printed. For "Zoe", find() returns end(), so the first branch runs. The code never reads a value from end(). Example 2 — end() and std::prev() Step back one position from end() to reach the last pair, which has the biggest key.
#include <iostream>
#include <iterator>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<int, std::string> m = {{5, "five"}, {1, "one"}, {9, "nine"}};
if (!m.empty()) {
auto last = std::prev(m.end());
std::cout << "last: " << last->first << " " << last->second << '\n';
}
return 0;
} How It Works m.end() is one position after the last pair, so reading it directly would be an error.std::prev(m.end()) moves one position back, onto the last pair. That is the pair with the biggest key, 9.The !m.empty() check matters. On an empty map end() equals begin(), and stepping back would be undefined. Example 3 — end() on an Empty Map On an empty map begin() and end() are the same, so a loop does not run.
#include <iostream>
#include <unordered_map>
int main() {
std::unordered_map<int, int> m;
std::cout << std::boolalpha;
std::cout << "begin == end: " << (m.begin() == m.end()) << '\n';
int loops = 0;
for (auto it = m.begin(); it != m.end(); ++it) {
++loops;
}
std::cout << "loop ran " << loops << " times\n";
m[1] = 100;
std::cout << "begin == end now: " << (m.begin() == m.end()) << '\n';
return 0;
} begin == end: true
loop ran 0 times
begin == end now: false How It Works An empty map has no first pair, so begin() and end() are the same position, and the comparison is true. The loop condition it != m.end() is false at once, so the body never runs and loops stays 0. After m[1] = 100, there is a real first pair. begin() now points at it, and end() is still one position after, so they differ. Frequently Asked Questions What does std::unordered_map::end() return? An iterator to the position after the last pair. It does not point at a real pair.
Can I dereference end()? No. That is undefined behavior. Only compare it with other iterators.
How do I get the last pair of an unordered_map? unordered_map has no reverse iterators. Prefer looking up a known key with find(), or walk with begin()/end().
Why does find() return end()? A search needs a way to say "not found". Because end() is never a real pair, it is a safe signal.
What is end() on an empty map? It equals begin(), so any loop from begin() to end() does nothing.
Next: equal_range() Keep going through the std::unordered_map functions. Get the range of pairs whose key equals the given key.
equal_range() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion