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.

3
  • 10
    What happens when you ... try it? Commented Jan 31, 2012 at 7:37
  • 1
    Why wouldn't it be allowed? How would you write a comparison operator for std::map<KType, VType>? Commented Jan 31, 2012 at 7:39
  • How does your C++ platform document std::pair? Answer is there. Commented Jan 31, 2012 at 9:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Bah, you beat me... Now to delete my suboptimal answer!
It's worth to mention, that it is only defined for lexicographic comparison. So for a pair of ints you would have to define it on your own.
@visitor: found nothing official, but stackoverflow.com/questions/2819245/…
@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?
@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<.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.