std::map vs. vector of vector

前端 未结 3 689
天命终不由人
天命终不由人 2021-01-26 17:33

I need a container to store a value (int) according to two attributes, source (int) and destination (int) i.e. when a source sends something to a destination, I need to store it

3条回答
  •  别那么骄傲
    2021-01-26 17:54

    If you want to keep using a vector but want to add a check for whether the item contains a valid value, look at boost::optional. The type would now be std::vector>>.

    You can also use a map, but the key into the map needs to be both IDs not just one.

    std::map,int>
    

    Edit: std::pair implements a comparison operator operator< that should be sufficient for use in a map, see http://en.cppreference.com/w/cpp/utility/pair/operator_cmp.

提交回复
热议问题