multimap::begin() and multimap::end() in C++ STL

C++Server Side ProgrammingProgramming

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

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates storing 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::begin()?

multimap::begin() function is an inbuilt function in C++ STL, which is defined in <map> header file. begin() is used to access the element which is at the very beginning of the associated multimap container.

This function returns an iterator which points to the first element of the container. When the container has no values in it the iterator can’t be deference

Syntax

multimap_name.begin();

Parameters

The function accepts no parameter.

Return value

This function returns an iterator which is pointing to the first value of the map container.

Input 

std::multimap<int> mymap;
mymap.insert({‘a’, 10});
mymap.insert({‘b’, 20});
mymap.insert({‘c’, 30});
mymap.begin();

Output 

a:10

Example

multimap::begin()

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   multimap<int, int> mul;
   //inserting elements in multimap
   mul.insert({ 1, 10 });
   mul.insert({ 2, 20 });
   mul.insert({ 3, 30 });
   mul.insert({ 4, 40 });
   mul.insert({ 5, 50 });
   //fetching first pair in multimap
   auto i = mul.begin();
   cout<<"First element in multimap is: ";
   cout << "{" << i->first << ", " << i->second << "}\n";
   //displaying multimap elements
   cout << "\nElements in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto it = mul.begin(); it!= mul.end(); ++it){
      cout << it->first << '\t' << it->second << '\n';
   }
   return 0;
}

Output

IF WE RUN THE ABOVE CODE IT WILL GENERATE THE FOLLOWING OUTPUT −

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

What is multimap::end()?

multimap::end() function is an inbuilt function in C++ STL, which is defined in <map> header file. end() is used to access the element which is at after the last element in the multimap container, or the past the last element.

This function returns an iterator which points to the element which is next to the last element of the container. When the container has no values in it the iterator can’t be dereferenced

Usually begin() and end() are used to iterate through the map container by giving them range.

Syntax

multimap_name.end();

Parameters

The function accepts no parameter.

Return value

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

Input

std::multimap<int> mymap;
mymap.insert({‘a’, 10});
mymap.insert({‘b’, 20});
mymap.insert({‘c’, 30});
mymap.end();

Output 

error

Example

multimap::end()

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   multimap<int, int> mul;
   //inserting elements in multimap
   mul.insert({ 1, 10 });
   mul.insert({ 2, 20 });
   mul.insert({ 3, 30 });
   mul.insert({ 4, 40 });
   mul.insert({ 5, 50 });
   //displaying multimap elements
   cout << "\nElemenst in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto it = mul.begin(); it!= mul.end(); ++it){
      cout << it->first << '\t' << it->second << '\n';
   }
   return 0;
}

Output

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

Elements in multimap is :
KEY ELEMENT
1 10
2 20
3 30
4 40
5 50
raja
Published on 22-Apr-2020 11:29:41
Advertisements