std::map one key, two values

2020-07-02 22:33发布

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?

标签: c++ stl
3条回答
对你真心纯属浪费
2楼-- · 2020-07-02 22:44

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.

查看更多
手持菜刀,她持情操
3楼-- · 2020-07-02 22:55

Normally, I crate a simple mapValue struct/class.

查看更多
做个烂人
4楼-- · 2020-07-02 22:57

Either use std::pair<> as you did, or make a custom struct containing the values you want to store. I'd do the latter in most cases, as the values then have names more descriptive than first and second.

查看更多
登录 后发表回答