C++ Map Library - operator= Function



Description

The C++ function std::multimap::operator= copy elements from initializer list to multimap.

Declaration

Following is the declaration for std::multimap::operator= function form std::map header.

C++11

multimap& operator= (initializer_list<value_type> il);

Parameters

il − Initialize list.

Return value

Returns this pointer

Exceptions

No effect on container if exception is thrown.

Time complexity

Linear i.e. O(n)

Example

The following example shows the usage of std::multimap::operator= function.

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   multimap<char, int> m;

   m = {
         {'a', 1},
         {'a', 2},
         {'b', 3},
         {'c', 4},
         {'d', 5}
       };

   cout << "Multimap contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Multimap contains following elements
a = 1
a = 2
b = 3
c = 4
d = 5
map.htm
Advertisements