开发者

std::map one key, two values

开发者 https://www.devze.com 2023-02-22 14:16 出处:网络
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

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

B开发者_JAVA技巧ut 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.


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.


Normally, I crate a simple mapValue struct/class.

0

精彩评论

暂无评论...
验证码 换一张
取 消