unordered_map 具有三个元素 [英] unordered_map to have three elements

查看:65
本文介绍了unordered_map 具有三个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 unordered_map 中包含三个元素.我尝试了以下代码

I am trying to have three elements in an 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 many errors in the print statement something like

‘std::pair’不是从‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’派生的std::cout <<x.第一个<<": "<

‘std::pair’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’ std::cout << x.first << ": "<< x.second << std::endl;

推荐答案

打印语句中的问题.应该是这样的:

The problem in your printing statement. It should be like this:

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

你不能直接打印std::pair.您需要单独打印每个项目.

You can not just print std::pair directly. You need to print each item separately.

没有ostream&std::pair 的运算符<< 重载,但 int 有一个重载.

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

这篇关于unordered_map 具有三个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆