map cbegin() and cend() function in C++ STL
- map::cbegin() is a built-in function in C++ STL which returns a constant iterator referring to the first element in the map container. Since map container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container’s sorting criterion.
Syntax:
map_name.cbegin()
Parameters: The function does not accept any parameter.
Return Value: The function returns a constant iterator referring to the first element in the map container.
// C++ program to illustrate// the map::cbegin() function#include <bits/stdc++.h>usingnamespacestd;intmain(){// initialize containermap<int,int> mp;// insert elements in random ordermp.insert({ 2, 30 });mp.insert({ 1, 40 });mp.insert({ 3, 60 });mp.insert({ 4, 20 });mp.insert({ 5, 50 });autoite = mp.cbegin();cout <<"The first element is: ";cout <<"{"<< ite->first <<", "<< ite->second <<"}\n";// prints the elementscout <<"\nThe map is : \n";cout <<"KEY\tELEMENT\n";for(autoitr = mp.cbegin(); itr != mp.cend(); ++itr) {cout << itr->first<<'\t'<< itr->second <<'\n';}return0;}chevron_rightfilter_noneOutput:The first element is: {1, 40} The map is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50 - map::cend() is a built-in function in C++ STL which returns a constant iterator pointing to the theoretical element that follows last element in the multimap. Since map container contains the element in an ordered way, cend() will point to that follows the last element according to the container’s sorting criterion.
Syntax:
map_name.cend()
Parameters: The function does not accept any parameter.
Return Value: The function returns a constant iterator pointing to the theoretical element that follows the last element in the map.
// C++ program to illustrate// the map::cend() function#include <bits/stdc++.h>usingnamespacestd;intmain(){// initialize containermap<int,int> mp;// insert elements in random ordermp.insert({ 2, 30 });mp.insert({ 1, 40 });mp.insert({ 3, 60 });mp.insert({ 4, 20 });mp.insert({ 5, 50 });// print the elementscout <<"\nThe map is : \n";cout <<"KEY\tELEMENT\n";for(autoitr = mp.cbegin(); itr != mp.cend(); ++itr) {cout << itr->first<<'\t'<< itr->second <<'\n';}return0;}chevron_rightfilter_noneOutput:The map is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50
Recommended Posts:
- set cbegin() and cend() function in C++ STL
- multiset cbegin() and cend() function in C++ STL
- list cbegin() and cend() function in C++ STL
- match_results cbegin() add cend() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- multimap::cbegin() and multimap::cend() in C++ STL
- array::cbegin() and array::cend() in C++ STL
- unordered_multiset cend() function in C++ STL
- unordered_multimap cend() function in C++ STL
- unordered_set cend() function in C++ STL
- unordered_multimap cbegin() function in C++ STL
- unordered_set cbegin() function in C++ STL
- unordered_multiset cbegin() function in C++ STL
- forward_list::cend() in C++ STL with Example
- unordered_map cend in C++ STL
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.



