The unordered_map::cend() is a built-in function in C++ STL which returns an iterator pointing to the position past the end element in the container or in one of its bucket. In an unordered_map object, there is no guarantee that which specific element is considered its first element. But all the elements in the container are covered since the range goes from its begin to its end until invalidated.
There are two variant of this function.
Syntax-1:
unordered_map.cend()
Parameters: This function does not accept any parameter.
Return type: The function returns an iterator to the element past the end of the container.
// CPP program to illustrate // unordered_map cend() #include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, int> ump; // inserting data into unordered_map ump[1] = 2; ump[3] = 4; ump[5] = 6; // here 'it' can not be modified for (auto it = ump.cbegin(); it != ump.cend(); ++it) cout << it->first << " " << it->second << endl; return 0; } |
5 6 1 2 3 4
Syntax-2:
unordered_map.cend ( size n )
Parameters: This function accepts parameter size n which should be lower than bucket count.
Return type: The function returns an iterator to the one of its bucket count.
// CPP program to illustrate // unordered_map cend() #include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, int> ump; // inserting data into unordered_map ump[1] = 2; ump[3] = 4; ump[5] = 6; cout << "unordered_map bucket contains \n"; for (int i = 0; i < ump.bucket_count(); i++) { cout << "Bucket " << i << " contains "; for (auto it = ump.cbegin(i); it != ump.cend(i); ++it) cout << it->first << " " << it->second; cout << endl; } return 0; } |
unordered_map bucket contains Bucket 0 contains Bucket 1 contains 1 2 Bucket 2 contains Bucket 3 contains 3 4 Bucket 4 contains Bucket 5 contains 5 6 Bucket 6 contains
How is cend() different from end()?
cend() is const version of end(). Similarly cbegin() is a const version of begin(). For example, the following code shows compiler error because we try to modify value in iterator.
// CPP program to illustrate // unordered_map cend() #include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, int> ump; // inserting data into unordered_map ump[1] = 2; ump[3] = 4; ump[5] = 6; // here 'it' can not be modified for (auto it = ump.cbegin(); it != ump.cend(); ++it) it->second = 10; // COMPILER ERROR return 0; } |
Output :
Compilation Error in CPP code :- prog.cpp: In function 'int main()': prog.cpp:19:20: error: assignment of member 'std::pair::second' in read-only object it->second = 10; // COMPILER ERROR
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- vector :: cbegin() and vector :: cend() in C++ STL
- list cbegin() and cend() function in C++ STL
- array::cbegin() and array::cend() in C++ STL
- multimap::cbegin() and multimap::cend() in C++ STL
- map cbegin() and cend() function in C++ STL
- set cbegin() and cend() function in C++ STL
- multiset cbegin() and cend() function in C++ STL
- unordered_set cend() function in C++ STL
- unordered_multiset cend() function in C++ STL
- unordered_multimap cend() function in C++ STL
- match_results cbegin() add cend() in C++ STL
- forward_list::cend() in C++ STL with Example
- std::sort() in C++ STL
- unordered_map in C++ STL
- fill() and fill_n() functions in C++ STL
- std::transform() in C++ STL (Perform an operation on all elements)
- Count number of unique Triangles using STL | Set 1 (Using set)
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- Iterators in C++ STL
- accumulate() and partial_sum() in C++ STL : numeric header
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

