In C++11, std::unordered_map::cend() returns a read-only iterator to the position after the last pair. Like end(), it is only a marker and must never be dereferenced.
It is the partner of cbegin(). Together they describe the whole map for loops and algorithms that must not change it. 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 cend with empty brackets. Use it as the end of a read-only loop.
C++
for (auto p = m.cbegin(); p != m.cend(); ++p) {
// read p->first and p->second
}
n = std::distance(m.cbegin(), m.cend());
Call
What it does
m.cend()
Returns a const_iterator one past the last pair, even when the unordered_map is not const.
You can compare a const_iterator with a normal iterator. So m.find(k) == m.cend() works even though find() returns an iterator.
Library declarations
C++
const_iterator cend() const noexcept; // C++11
Parameters
Parameter
Description
None
cend() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
const_iterator
A read-only iterator to the position after the last pair. It equals cbegin() for an empty map.
C++ Examples
Example 1 — cend() Ends a Read-Only Loop
Walk the unordered_map from cbegin() to cend() and print each pair.
C++
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<int, std::string> day = {{3, "Wed"}, {1, "Mon"}, {2, "Tue"}};
for (auto it = day.cbegin(); it != day.cend(); ++it) {
std::cout << it->first << " = " << it->second << '\n';
}
return 0;
}
Output
1 = Mon
2 = Tue
3 = Wed
How It Works
The unordered_map sorts the keys, so cbegin() points at key 1, not at the first one that was written.
The loop keeps going while the iterator differs from cend(). After the pair for key 3, the iterator reaches cend() and the loop stops.
Because both ends are const_iterators, the loop can read every pair but cannot change any of them.
Example 2 — cend() to Test find()
Mix an iterator from find() with cend() in one comparison.
C++
#include <iostream>
#include <unordered_map>
#include <string>
int main() {
std::unordered_map<std::string, int> ids = {{"ann", 1}, {"ben", 2}};
auto a = ids.find("ben");
auto b = ids.find("cy");
std::cout << std::boolalpha;
std::cout << "ben found: " << (a != ids.cend()) << '\n';
std::cout << "cy found: " << (b != ids.cend()) << '\n';
return 0;
}
Output
ben found: true
cy found: false
How It Works
find() on a non-const map returns a normal iterator, while cend() is a const_iterator.
The two types can be compared with !=. For "ben" the iterator points at a real pair, so it differs from cend().
For "cy", find() returns the end position, which equals cend(), so the answer is false.
Example 3 — cend() with std::distance
Count the pairs by measuring the distance between cbegin() and cend().
C++
#include <iostream>
#include <iterator>
#include <unordered_map>
int main() {
std::unordered_map<char, int> m = {{'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}};
auto n = std::distance(m.cbegin(), m.cend());
std::cout << "distance: " << n << '\n';
std::cout << "size: " << m.size() << '\n';
return 0;
}
Output
distance: 4
size: 4
How It Works
std::distance(first, last) counts how many steps lead from first to last.
From cbegin() to cend() it takes four steps, one for each pair. An unordered_map iterator can only step one at a time, so this is a slow walk.
The result matches size(), which is the fast and correct way to count. The example only shows what the two markers mean.
Frequently Asked Questions
A const_iterator to the position after the last pair. It is a marker and not a real pair.
Both mark the end. cend() always returns a const_iterator, so it fits loops that must not change the unordered_map.
Yes. An iterator and a const_iterator of the same map can be compared with == and !=.
No. It does not point at a pair. Doing so is undefined behavior.