The begin method in C++ for multimap is employed to retrieve an iterator pointing to the initial element within the multimap container.
Syntax
iterator begin(); //until C++ 11
const_iterator begin() const; //until C++ 11
iterator begin() noexcept; //since C++ 11
const_iterator begin() const noexcept; //since C++ 11
Parameter
Return value
It yields an iterator that points to the initial element of the multimap.
Complexity
Constant.
Iterator validity
No changes.
Data Races
The container is accessed without any modifications made by either the constant or non-constant versions.
Exception Safety
This function never throws exception.
Example 1
Let's explore a basic illustration demonstrating the functionality of the begin method:
#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 (multimap<char,string>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
cout << it->first << " => " << it->second << '\n';
return 0;
}
Output:
a => Java
a => Android
b => C++
b => Python
The function begin is employed to retrieve an iterator that points to the initial element within the multimap named mymultimap.
Example 2
Let's explore a basic illustration of iterating through a 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}
};
// Create a multimap iterator and point to beginning of multimap
multimap<string, int>::iterator it = m.begin();
// Iterate over a multimap using std::for_each and Lambda function
for_each(m.begin(), m.end(),
[](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 previous example, we utilized an STL algorithm for-each to loop through the multimap. This process involves iterating over each element within the multimap and executing the specified callback function that we have defined.
Example 3
Let's explore a basic illustration on how to iterate 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" },
{ 100, "Aman" }};
cout<<"Elements are: "<<endl;
multimap<int, string>::const_iterator it; // declare an iterator
it = mymultimap.begin(); // assign it to the start of the vector
while (it != mymultimap.end()) // 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:
Elements are:
100 = Nikita
100 = Aman
200 = Deep
200 = Suman
300 = Priya
In the aforementioned case, the begin method is employed to retrieve an iterator that points to the initial element within the mymultimap multimap.
Example 4
Let's see a simple example:
#include <map>
#include <iostream>
int main()
{
using namespace std;
multimap <int, int> m1;
multimap <int, int> :: iterator m1_Iter;
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_Iter = m1.begin ();
cout << "The first element of m1 is " << m1_Iter -> first << endl;
m1_Iter = m1.begin ();
m1.erase ( m1_Iter );
m1_Iter = m1.begin();
cout << "First element of m1 is now " << m1_Iter -> first << endl;
}
Output:
The first element of m1 is 0
First element of m1 is now 1
In the previous example, the begin method is employed to retrieve an iterator that points to the initial element within the mymultimap multimap.