Unordered_map have three elements

I am trying to have three elements in unordered_map. I tried the following code

#include <iostream>
#include <string>
#include <algorithm>
#include <boost/unordered_map.hpp>

typedef boost::unordered_map<int, std::pair<int, int>> mymap;
mymap m;

int main(){
//std::unordered_map<int, std::pair<int, int> > m;
m.insert({3, std::make_pair(1,1)});
m.insert({4, std::make_pair(5,1)});
for (auto& x: m)
    std::cout << x.first << ": "<< x.second << std::endl;

}

but I get a lot of errors in the print statement, something like

'std :: the pair is not inferred from' const std :: __ cxx11 :: basic_string <_CharT, _Traits, _Alloc> std :: cout <x.first <":" <x.second <std :: cps;

+4
source share
1 answer

The problem is with the print application. It should be like this:

std::cout << x.first << ": "<< x.second.first << ","<< x.second.second  << std::endl;
_______________________________^^^^^^^^^^^^^^__________^^^^^^^^^^^^^^^_______________

You cannot just type std::pairdirectly. You need to print each item separately.

There is no ostream& operator<<overload for std::pair, but there is one for int.

+6
source

Source: https://habr.com/ru/post/1666902/


All Articles