Multimap Begin Function

The C++ multimap beginfunction is used to return an iterator referring to the first element of the multimap container.

Syntax

Example

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 returns an iterator pointing to the first element of the multimap.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed. Neither the constant nor the non-constant versions modify the container.

Exception Safety

This function never throws exception.

Example 1

Let's see the simple example for begin function:

Example

#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:

Output

a => Java
a => Android
b => C++
b => Python

In the above example, beginfunction is used to return an iterator pointing to the first element in the mymultimap multimap.

Example 2

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

Example

#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:

Output

Room1 = 100
Room1 = 300
Room1 = 100
Room2 = 200

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

Example 3

Let's see a simple example to iterate over the multimap using while loop:

Example

#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:

Output

Elements are: 
100 = Nikita
100 = Aman
200 = Deep
200 = Suman
300 = Priya

In the above, beginfunction is used to return an iterator pointing to the first element in the mymultimap multimap.

Example 4

Let's see a simple example:

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:

Output

The first element of m1 is 0
First element of m1 is now 1

In the above example, begin function is used to return an iterator pointing to the first element in the mymultimap multimap.

Input Required

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