我试图在一个元素中包含三个元素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”不是从“const std::__cxx11::basic_string<_chart _traits _alloc>”派生的
std::cout
您的打印声明中存在问题。应该是这样的:
std::cout << x.first << ": "<< x.second.first << ","<< x.second.second << std::endl;
_______________________________^^^^^^^^^^^^^^__________^^^^^^^^^^^^^^^_______________
你不能只打印std::pair
直接地。您需要单独打印每个项目。
没有ostream& operator<<
过载std::pair
但有一个是为了int
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)