std::map 一个键,两个值 [英] std::map one key, two values

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

问题描述

将两个值映射到一个键的最佳方法是什么?

即具有值和布尔值的项目.

尝试使用:

std::map>我的地图2

但这似乎不是正确的解决方案.有没有更优雅的方法来做到这一点?

解决方案

这确实是正确的解决方案.更一般地,考虑使用 std::tuple 而不是 std::pair 作为统一接口,而不管值的数量(如 std::paircode> 显然仅限于两个),或 boost::tuple 如果您的编译器太旧而无法提供 std::std::tr1:: 实现.

What is the best way to map two values to one key?

ie An item with a value and bool.

Tried using:

std::map<std::string, std::pair<std::string, bool> > myMap2

But that doesn't seem like the correct solution. Is there a more elegant way to do this?

解决方案

That is indeed the correct solution. More generally, consider using std::tuple instead of std::pair for a uniform interface regardless of the number of values (as std::pair is obviously restricted to two), or boost::tuple if your compiler is too old to ship with a std:: or std::tr1:: implementation.

这篇关于std::map 一个键,两个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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