# 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.

## Input

map<char, int> newmap;
newmap[‘a’] = 1;
newmap[‘b’] = 2;
newmap[‘c’] = 3;
newmap.cbegin();

## Output

a = 1

## Example

Live Demo

#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  header file. cend() function is the constant end (). This function returns the constant iterator of the element which is past the last element in the associated 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 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.

## Input

map<char, int> newmap;
newmap[‘a’] = 1;
newmap[‘b’] = 2;
newmap[‘c’] = 3;
newmap.cend();

## Output

error

## Example

Live Demo

#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