multimap::operator= in C++ STL

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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";
}

chevron_right


Output:-

MultiMap 1 data
d 4
e 5
f 6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.