unordered_map 拥有三个元素
unordered_map to have three elements
我想在 unordered_map
中包含三个元素。我尝试了以下代码
#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;
}
但是我在打印语句中遇到很多错误,例如
‘std::pair’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
std::cout << x.first << ": "<< x.second << std::endl;
你的打印语句有问题。应该是这样的:
std::cout << x.first << ": "<< x.second.first << ","<< x.second.second << std::endl;
_______________________________^^^^^^^^^^^^^^__________^^^^^^^^^^^^^^^_______________
您不能直接打印 std::pair
。您需要单独打印每个项目。
std::pair
没有 ostream& operator<<
重载,但 int
.
有一个重载
我想在 unordered_map
中包含三个元素。我尝试了以下代码
#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;
}
但是我在打印语句中遇到很多错误,例如
‘std::pair’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’ std::cout << x.first << ": "<< x.second << std::endl;
你的打印语句有问题。应该是这样的:
std::cout << x.first << ": "<< x.second.first << ","<< x.second.second << std::endl;
_______________________________^^^^^^^^^^^^^^__________^^^^^^^^^^^^^^^_______________
您不能直接打印 std::pair
。您需要单独打印每个项目。
std::pair
没有 ostream& operator<<
重载,但 int
.