multimap::cbegin() and multimap::cend() in C++ STL


In this article we will be discussing the working, syntax and examples of multimap::cbegin() and multimap::cend() functions in C++ STL.

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.

What is multimap::cbegin()?

multimap::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 multimap 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.

Syntax

multi.cbegin();

Parameters

This function accepts no parameter.

Return value

It returns an iterator pointing to the first element of the associated map container.

Input

multimap<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(){
   //create the container
   multimap<int, int> mul;
   //insert using emplace
   mul.emplace_hint(mul.begin(), 1, 10);
   mul.emplace_hint(mul.begin(), 2, 20);
   mul.emplace_hint(mul.begin(), 2, 30);
   mul.emplace_hint(mul.begin(), 1, 40);
   mul.emplace_hint(mul.begin(), 1, 50);
   mul.emplace_hint(mul.begin(), 5, 60);
   auto it = mul.cbegin();
   cout << "First element in the multimap is: ";
   cout << "{" << it->first << ", " << it->second << "}\n";
   cout << "\nElements in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto i = mul.cbegin(); i!= mul.cend(); i++){
      cout << i->first << "\t" << i->second << endl;
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

First element in the multimap is: {1, 50}
Elements in multimap is :
KEY    ELEMENT
1      50
1      40
1      10
2      30
2      20
5      60

What is multimap::cend()?

multimap::cend() function is an inbuilt function in C++ STL, which is defined in <map> 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 multimap 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.

multimap::cbegin() and multimap::cend() are used to traverse through the whole container by giving a start of the range and end of the range.

Syntax

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

multimap <char, int> newmap;
newmap(make_pair(‘a’, 1));
newmap(make_pair(‘b’, 2));
newmap(make_pair(‘c’, 3));
newmap.cend();

Output

error

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //create the container
   multimap<int, int> mul;
   //insert using emplace
   mul.emplace_hint(mul.begin(), 1, 10);
   mul.emplace_hint(mul.begin(), 2, 20);
   mul.emplace_hint(mul.begin(), 2, 30);
   mul.emplace_hint(mul.begin(), 1, 40);
   mul.emplace_hint(mul.begin(), 1, 50);
   mul.emplace_hint(mul.begin(), 5, 60);
   cout << "\nElements in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto i = mul.cbegin(); i!= mul.cend(); i++){
      cout << i->first << "\t" << i->second << endl;
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements in multimap is :
KEY ELEMENT
1 50
1 40
1 10
2 30
2 20
5 60

Updated on: 22-Apr-2020

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements