Open In App

multimap::operator= in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

multimap::operator= is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents. 

Syntax:

multimap1 = (multimap2)

Parameters :
Another container of the same type.

Result :
Assign the contents of the container passed as 
parameter to the container written on left 
side of the operator.

Examples:

Input  :  multimap1 = { ('a', 1), ('b', 2), ('c', 3)}
          multimap2 = { ('d', 4), ('e', 5), ('f', 6)}
          multimap1 = multimap2;
Output :  multimap1 
d 4
e 5
f 6

Input  :  multimap1 = { ('abc', 1), ('bca', 2), ('cab', 3)}
          multimap2 = { ('def', 4), ('efd', 5), ('fde', 6)}
          multimap1 = multimap2;
Output :  multimap1 
def 4
efd 5
fde 6

Errors and Exceptions 

1. If the containers are of different types, an error is thrown. 
2. It has a basic no exception throw guarantee otherwise. 

CPP




// CPP Program to illustrate working of
// multimap::operator=
#include <iostream>
#include <map>
using namespace std;
 
int main()
{
    // initialise multimap
    multimap<char, int> m1;
    multimap<char, int> m2;
 
    // iterator for iterate all element of multimap
    multimap<char, int>::iterator iter;
 
    // multimap1 data
    m1.insert(make_pair('a', 1));
    m1.insert(make_pair('b', 2));
    m1.insert(make_pair('c', 3));
 
    // multimap2 data
    m2.insert(make_pair('d', 4));
    m2.insert(make_pair('e', 5));
    m2.insert(make_pair('f', 6));
 
    // operator=
    m1 = m2;
 
    // multimap1 data
    cout << "MultiMap 1 data" << "\n";
    for (iter = m1.begin(); iter != m1.end(); iter++)
        cout << (*iter).first << " " << (*iter).second << "\n";
}


Output:-

MultiMap 1 data
d 4
e 5
f 6

Time Complexity : O(N)


Last Updated : 12 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads