The C++ multimap cbegin method is employed to retrieve a constant iterator that points to the initial element within the multimap data structure.
Syntax
const_iterator cbegin() const noexcept; //since C++ 11
A const_iterator is an iterator that points to read-only data in C++ tutorials.
Parameter
Return value
It yields a const_iterator indicating the initial element of the multimap.
Complexity
Constant
Iterator Validity
No changes.
Data Races
The container is accessed.
Exception Safety
This member function never throws exception.
Example 1
Let's explore a basic illustration demonstrating the implementation of the cbegin function:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,string> mymultimap;
mymultimap = {
{'a',"Java"},
{'b', "C++"},
{'b', "Python"},
{'a', "Android"}
};
// show content:
for (auto it = mymultimap.cbegin(); it != mymultimap.cend(); ++it)
cout <<(*it).first << " => " << (*it).second << '\n';
return 0;
}
Output:
a => Java
a => Android
b => C++
b => Python
In the provided illustration, the cbegin method is employed to retrieve a const_iterator that points to the initial element in the mymultimap multimap.
Example 2
Let's examine a basic illustration of iterating through the multimap using a for-each loop:
#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
int main() {
multimap<string, int> m;
m= {
{"Room1", 100},
{"Room2", 200},
{"Room1", 300},
{"Room1", 100}
};
// Iterate over a multimap 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:
Room1 = 100
Room1 = 300
Room1 = 100
Room2 = 200
In the previously mentioned scenario, we are employing an STL algorithm called std::for-each to traverse through the multimap. This algorithm will cycle through each element within the multimap and execute the specified callback function.
Example 3
Let's explore a straightforward illustration of iterating through a multimap using a while loop:
#include <iostream>
#include <map>
#include <string>
int main()
{
using namespace std;
multimap<int,string> mymultimap = {
{ 100, "Nikita"},
{ 200, "Deep" },
{ 300, "Priya" },
{ 200, "Suman" },
{ 400, "Aman" }};
multimap<int, string>::const_iterator it; // declare an iterator
it = mymultimap.cbegin(); // assign it to the start of the multimap
while (it != mymultimap.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:
100 = Nikita
200 = Deep
200 = Suman
300 = Priya
400 = Aman
In the provided scenario, the cbegin method is employed to retrieve a const_iterator that points to the initial element within the mymultimap multimap.
Example 4
Let's see another simple example:
#include <map>
#include <iostream>
int main()
{
using namespace std;
multimap <int, int> m1;
multimap <int, int> :: iterator m1_Iter;
multimap <int, int> :: const_iterator m1_cIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 0, 0 ) );
m1.insert ( Int_Pair ( 1, 1 ) );
m1.insert ( Int_Pair ( 2, 4 ) );
m1_cIter = m1.cbegin ();
cout << "The first element of m1 is " << m1_cIter -> first << endl;
m1_Iter = m1.begin ();
m1.erase ( m1_Iter );
// The following 2 lines would err as the iterator is const
// m1_cIter = m1.begin ();
// m1.erase ( m1_cIter );
m1_cIter = m1.cbegin();
cout << "First element of m1 is now " << m1_cIter -> first << endl;
}
Output:
The first element of m1 is 0
First element of m1 is now 1
In the previous example, the cbegin method is employed to retrieve a const_iterator indicating the initial element in the mymultimap multimap.