
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
map cbegin() and cend() function in C++ STL
In this article we will be discussing the working, syntax and examples of map::cbegin() and map::cend() functions in C++ STL.
What is a Map in C++ STL?
Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.
What is a map::cbegin()?
map::cbegin() function is an inbuilt function in C++ STL, which is defined in <map> header file. cbegin() is the constant begin function.
This function returns the constant iterator which is pointing to the first element in the map container. The iterator returned is the constant iterator, they can’t be used to modify the content. We can use them to traverse among the elements of a map container by increasing ordecreasing the iterator
Syntax
newmap.cbegin();
Parameters
This function accepts no parameter.
Return value
It returns an iterator pointing to the first element of the associated map container.
Example
Input
map<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; newmap.cbegin();
Output
a = 1
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_Map; TP_Map.insert({3, 50}); TP_Map.insert({2, 30}); TP_Map.insert({1, 10}); TP_Map.insert({4, 70}); //using map::cbegin to fetch first element auto temp = TP_Map.cbegin(); cout <<"First element is: "<<temp->first << " -> " << temp->second; cout<<"\nTP Map is : \n"; cout << "MAP_KEY\tMAP_ELEMENT\n"; for (auto i = TP_Map.cbegin(); i!= TP_Map.cend(); i++) { cout << i->first << "\t" << i->second << endl; } return 0; }
Output
First element is: 1 -> 10 TP Map is: MAP_KEY MAP_ELEMENT 1 10 2 30 3 50 4 70
What is map::cend()?
map::cend() function is an inbuilt function in C++ STL, which is defined in
The iterator returned is the constant iterator, they can’t be used to modify the content, we can use them to traverse among the elements of the map container by increasing or decreasing the iterator.
map::cbegin() and map::cend() are used to traverse through the whole container by giving a start of the range and end of the range.
Syntax
newmap.cend();
Parameters
This function accepts no parameter.
Return value
It returns an iterator pointing to the past the last element of the associated map container.
Example
Input
map<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; newmap.cend();
Output
error
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_Map; TP_Map.insert({3, 50}); TP_Map.insert({2, 30}); TP_Map.insert({1, 10}); TP_Map.insert({4, 70}); cout<<"\nTP Map is : \n"; cout << "MAP_KEY\tMAP_ELEMENT\n"; for (auto i = TP_Map.cbegin(); i!= TP_Map.cend(); i++) { cout << i->first << "\t" << i->second << endl; } return 0; }
Output
TP Map is: MAP_KEY MAP_ELEMENT 1 10 2 30 3 50 4 70
- Related Articles
- Set cbegin() and cend() function in C++ STL
- multiset cbegin() and cend() function in C++ STL
- list cbegin() and cend() functions in C++ STL
- multimap::cbegin() and multimap::cend() in C++ STL
- match_results cbegin() add cend() in C++ STL
- map crbegin() and crend() function in C++ STL
- forward_list cbegin() in C++ STL
- map emplace_hint() function in C++ STL
- map erase() function in C++ STL
- map find() function in C++ STL
- map key_comp() function in C++ STL
- map rbegin() function in C++ STL
- map rend() function in C++ STL
- map lower_bound() function in C++ STL
- map upper_bound() function in C++ STL
