std::map one key, two values

前端 未结 3 1729
谎友^
谎友^ 2021-02-19 16:03

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

ie An item with a value and bool.

Tried using:

std::map

        
相关标签:
3条回答
  • 2021-02-19 16:34

    Normally, I crate a simple mapValue struct/class.

    0 讨论(0)
  • 2021-02-19 16:35

    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.

    0 讨论(0)
  • 2021-02-19 16:50

    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.

    0 讨论(0)
提交回复
热议问题