multimap::operator= in C++ STL

C++Server Side ProgrammingProgramming

In this article, we will be discussing the working, syntax, and example of multimap equal ‘=’ operator 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 equal to ‘=’ operator?

multimap::operator= is equal to operator. This operator is used to copy the elements from one container to another container, by overwriting the current content of the container.

Syntax

multiMap_name1 = multimap_name2;

Parameter

There is a multimap on the left side of the operator and another multimap on the right side of the container. The contents of the right side are copied to the multimap on the left side.

Return value

There is no return value of an operator.

Input 

multimap<char, int> newmap, themap;
newmap.insert({1, 20});
newmap.insert({2, 30});
themap = newmap;

Output 

themap = 1:20 2:30

Example

 Live Demo

#include<iostream>
#include<map>
using namespace std;
int main(){
   multimap<int,char > mul_1;
   multimap<int,char> mul_2;
   //declaring iterator to traverse the elements
   multimap<int,char>:: iterator i;
   //inserting elements to multimap1
   mul_1.insert(make_pair(0,'a'));
   mul_1.insert(make_pair(1,'b'));
   mul_1.insert(make_pair(2,'c'));
   mul_1.insert(make_pair(3,'d'));
   //inserting elements to multimap2
   mul_2.insert(make_pair(4,'e'));
   mul_2.insert(make_pair(5,'f'));
   mul_2.insert(make_pair(6,'g'));
   //calling = operator
   mul_1= mul_2;
   //elements of multimap1
   cout<<"Elements in multimap1 are: "<<"\n";
   for( i = mul_1.begin(); i!= mul_1.end(); i++) {
      cout<<(*i).first<<" "<< (*i).second << "\n";
   }
   //elements of multimap2
   cout<<"\nElements in multimap2 are: "<<"\n";
   for( i = mul_2.begin(); i!= mul_2.end(); i++) {
      cout<<(*i).first<<" "<< (*i).second << "\n";
   }
}

Output

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

Elements in multimap1 are:
4 e
5 f
6 g
Elements in multimap2 are:
4 e
5 f
6 g
raja
Published on 22-Apr-2020 11:22:10
Advertisements