std::map 一个键,两个值 [英] std::map one key, two values
问题描述
将两个值映射到一个键的最佳方法是什么?
即具有值和布尔值的项目.
尝试使用:
std::map>我的地图2
但这似乎不是正确的解决方案.有没有更优雅的方法来做到这一点?
这确实是正确的解决方案.更一般地,考虑使用 std::tuple
而不是 std::pair
作为统一接口,而不管值的数量(如 std::pair
code> 显然仅限于两个),或 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屋!