C++ Standard Library C++ STL Library

C++ map - map() Function



The C++ map::map function is used to construct a map object, initializing its contents depending on the version of constructor used:

Syntax

//default version - construct an empty 
//container with no elements
explicit map (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  map (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of x into the container	
map (const map& x);
//default version - construct an empty 
//container with no elements
explicit map (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc);

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  map (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());

//copy version - copies all elements
//of x into the container	
map (const map& x);
map (const map& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
map (map&& x);
map (map&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container	
map (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());
//default version - construct an empty 
//container with no elements
map();
explicit map (const key_compare& comp,
              const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc);

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  map (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());
template <class InputIterator>
  map (InputIterator first, InputIterator last,
       const allocator_type& = allocator_type());

//copy version - copies all elements
//of x into the container	
map (const map& x);
map (const map& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
map (map&& x);
map (map&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container		
map (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());
map (initializer_list<value_type> il,
     const allocator_type& alloc = allocator_type());

Parameters

alloc Specify the Allocator object. The container keeps and uses an internal copy of this allocator.
comp A binary predicate that takes two keys of map as arguments and returns a bool. Keys are sorted by using this function.
first Specify initial position of the input iterator of the range. The range used is [first,last).
last Specify final position of the input iterator of the range. The range used is [first,last).
x Specify a map object of same type.
il Specify an initializer_list object.

Return Value

Constructor never returns value.

Time Complexity

Constant i.e, Θ(1), for default version and move version.
For all the other cases, Linear i.e, Θ(n).

Example: using default version

In the example below, the map::map function is used to construct a map object.

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  //default version - construct an empty map
  map<int, string> MyMap;
  map<int, string>::iterator it;

  cout<<"MyMap contains:\n";
  for(it = MyMap.begin(); it != MyMap.end(); ++it)
    cout<<it->first<<"  "<<it->second<<"\n";

  //populating map
  MyMap[101] = "John";
  MyMap[102] = "Marry";

  cout<<"\nMyMap contains:\n";
  for(it = MyMap.begin(); it != MyMap.end(); ++it)
    cout<<it->first<<"  "<<it->second<<"\n";

  return 0;
}

The output of the above code will be:

MyMap contains:

MyMap contains:
101  John
102  Marry

Example: using range and copy version

A map can also be constructed using range or copy version. Consider the following example:

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  map<int, string> map1;
  map<int, string>::iterator it;

  //populating map
  map1[101] = "John";
  map1[102] = "Marry";
  map1[103] = "Sam";

  //range version - construct map2 using range
  map<int, string> map2(map1.begin(), map1.end());

  //copy version - construct map3 from map1
  map<int, string> map3(map1); 

  cout<<"map2 contains:\n";
  for(it = map2.begin(); it != map2.end(); ++it)
    cout<<it->first<<"  "<<it->second<<"\n";

  cout<<"\nmap3 contains:\n";
  for(it = map3.begin(); it != map3.end(); ++it)
    cout<<it->first<<"  "<<it->second<<"\n";

  return 0;
}

The output of the above code will be:

map2 contains:
101  John
102  Marry
103  Sam

map3 contains:
101  John
102  Marry
103  Sam

Example: using move version

Using the move version of map, the content of one map can be moved to another map. Consider the following example:

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  map<int, string> map1;
  map<int, string>::iterator it;

  //populating map
  map1[101] = "John";
  map1[102] = "Marry";
  map1[103] = "Sam";

  cout<<"map1 contains:\n";
  for(it = map1.begin(); it != map1.end(); ++it)
    cout<<it->first<<"  "<<it->second<<"\n";

  //moving all content of map1 into map2
  map<int, string> map2(move(map1));

  cout<<"\nmap1 contains:\n";
  for(it = map1.begin(); it != map1.end(); ++it)
    cout<<it->first<<"  "<<it->second<<"\n";

  cout<<"\nmap2 contains:\n";
  for(it = map2.begin(); it != map2.end(); ++it)
    cout<<it->first<<"  "<<it->second<<"\n";

  return 0;
}

The output of the above code will be:

map1 contains:
101  John
102  Marry
103  Sam

map1 contains:

map2 contains:
101  John
102  Marry
103  Sam

Example: using initializer list version

The initializer list can also be used to assign values into a map container. Consider the example below:

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  //creating initializer list
  initializer_list<pair<const int, string>> ilist = 
     {{101, "John"}, {102, "Marry"}};

  //initializer list version - copies all 
  //elements of ilist into the container	
  map<int, string> MyMap(ilist);
  map<int, string>::iterator it;

  cout<<"MyMap contains:\n";
  for(it = MyMap.begin(); it != MyMap.end(); ++it)
    cout<<it->first<<"  "<<it->second<<"\n";

  return 0;
}

The output of the above code will be:

MyMap contains:
101  John
102  Marry

❮ C++ <map> Library