0

I want to define a std::map with key as std::pair<std::string, std::string> something like follow

typedef std::map< std::pair<std::string, std::string>, std::string> my_map

Is this allowed, and how do i write comparasion operator for such map.

Avinash
  • 12,851
  • 32
  • 116
  • 186

1 Answers1

6

Yes, it is allowed.

std::pair already has an operator< which compares the two values in order, so you may not need to do anything special for a comparator at all.

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
  • Bah, you beat me... Now to delete my suboptimal answer! – Arafangion Jan 31 '12 at 07:40
  • It's worth to mention, that it is only defined for lexicographic comparison. So for a pair of `int`s you would have to define it on your own. – ezdazuzena Jan 31 '12 at 08:02
  • @visitor: found nothing official, but http://stackoverflow.com/questions/2819245/is-stdpairint-stdstring-ordering-well-defined – ezdazuzena Jan 31 '12 at 08:58
  • @ezdazuzena Why would that mean that you need to define your own comparison for pairs of ints? Isn't that the behavior you'd want for pairs of ints in the majority of cases? – sepp2k Jan 31 '12 at 12:40
  • @sepp2k: I could really miss something here, put in lexicographic comparison the following is true: 101 < 11 – ezdazuzena Jan 31 '12 at 13:04
  • 3
    @ezdazuzena It doesn't compare the individual items lexicographically (how could you even implement that generically?). All that's meant by lexicographically here is that it compares by the first element and if the first element is equal by the second. For the comparison of the individual elements it uses the elements' `operator<`. – sepp2k Jan 31 '12 at 13:30
  • @sepp2k: hmm.. can't argue further against that. So, I'll believe you and I guess I learned something new. Thanks then.. – ezdazuzena Jan 31 '12 at 13:34