C++ map create via default constructor

Introduction

We can construct a map through its default constructor and some help from its key subscript operator [].

If the key accessed through a subscript operator does not exist, the entire key-value pair gets inserted into a map.

Example:

#include <iostream> 
#include <map> 

int main() /*from  ww  w. jav a  2 s.  co  m*/
{ 
    std::map<int, char> mymap; 
    mymap[1] = 'a'; 
    mymap[2] = 'b'; 
    mymap[3] = 'z'; 

    for (auto el : mymap) 
    { 
        std::cout << el.first << ' ' << el.second << '\n'; 
    } 
} 



PreviousNext

Related