Map Cbegin Function

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

Syntax

Example

const_iterator cbegin() const noexcept;  //since C++ 11

Note: A const_iterator is an iterator thacpp tutorials to constant content.

Parameter

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.

Example

#include <iostream>
#include <map>
using namespace std;

int main ()
{
  map<char,string> mymap;

  mymap['b'] = "Java";
  mymap['a'] = "C++";
  mymap['c'] = "SQL";

  // show content:
  for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)

    cout <<(*it).first << " => " << (*it).second << '\n';

  return 0;
}

Output:

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.

Example

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
 
using namespace std;
 
int main() {
 
	  map<string, int> m;

  m["Room1"] = 100;
  m["Room2"] = 200;
  m["Room3"] = 300;

 
	// Iterate over a map using std::for_each and Lambda function
	
		for_each(m.cbegin(), m.cend(),
				[](pair<string, int> element){
				    
					// Accessing KEY from element
					string word = element.first;
					// Accessing VALUE from element.
					int count = element.second;
					cout<<word<<" = "<<count<<endl;
		});
 
	return 0;
}

Output:

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.

Example

#include <iostream>
#include <map>
#include <string>
int main()
{
    using namespace std;
 
      map<int,string> mymap = {
                { 100, "Nikita"},
                { 200, "Deep"  },
                { 300, "Priya" },
                { 400, "Suman" },
                { 500, "Aman"  }};

 
    map<int, string>::const_iterator it; // declare an iterator

    it = mymap.cbegin(); // assign it to the start of the vector

    while (it != mymap.cend()) // while it hasn't reach the end
    {
cout << it->first << " = " << it->second << "\n"; 

// print the value of the element icpp tutorials to

++it; // and iterate to the next element
    }
 
    cout << endl;
}

Output:

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.

Example

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  map<int,int> mymap = {
                { 10, 10},
                { 20, 20 },
                { 30, 30 } };
          
                
  cout<<"Elements are:" <<endl;
 
    for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
    cout << it->first 
    << " + " 
    << it->second 
    << " = "
    <<it->first + it->second
    << '\n';

    auto ite = mymap.cbegin();
 
    cout << "The first element is: ";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";

  return 0;
  }

Output:

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.

Input Required

This code uses input(). Please provide values below: