Skip to content
Related Articles

Related Articles

multimap::operator= in C++ STL
  • Last Updated : 24 Jul, 2019

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 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
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :