C++ map pair

Introduction

Every map element is a pair.

The pair's first element (the key) is accessed through a first() member variable, the second element (the value) is accessed through a second member function variable.

To print out our map, we can use:

#include <iostream> 
#include <map> 

int main() /*from  w  w  w . j ava  2s.c  o  m*/
{ 
    std::map<int, char> mymap = { {1, 'a'}, {2, 'b'}, {3,'z'} }; 
    for (auto el : mymap) 
    { 
        std::cout << el.first << ' ' << el.second << '\n'; 
    } 
} 



PreviousNext

Related