Javatpoint Logo
Javatpoint Logo

C++ map cbegin() Function

C++ map cbegin() function is used to return a constant iterator pointing to the first element of the map container.

Syntax

Note: A const_iterator is an iterator that points to constant content.

Parameter

None

Return value

It returns a const_iterator pointing to the first element of the map.

Example 1

Let's see a simple example for cbegin() function.

Output:

a => C++
b => Java
c => SQL

In the above, cbegin() function is used to return a constant iterator pointing to the first element in the mymap map.

Example 2

Let's see a simple example to iterate over the map using for-each loop.

Output:

Room1 = 100
Room2 = 200
Room3 = 300 

In the above example, we are using an STL algorithm std::for-each to iterate over the map. It will iterate on each of the map element and call the callback provided by us.

Example 3

Let's see a simple example to iterate over the map using while loop.

Output:

100: Nikita
200: Deep
300: Priya
400: Suman
500: Aman

In the above example, cbegin() function is used to return a constant iterator pointing to the first element in the mymap map.

Example 4

Let's see another simple example.

Output:

Elements are:
10 + 10 = 20
20 + 20 = 40
30 + 30 = 60
The first element is: {10, 10}

In the above example, cbegin() function is used to return an iterator pointing to the first element in the mymap map.

Next TopicC++ Map



Please Share

facebook twitter google plus pinterest