views:

59

answers:

1

Is it possible for a multimap to contain within it pairs? IE, rather then being defined as multimap<char,int> for instance, it would be defined as multimap<pair, pair>?

How would this multimap then be sorted? Also, how would one access the individual contents of each pair?

+6  A: 

Is it possible for a multimap to contain within it pairs?

Yes its possible.

How would this multimap then be sorted?

By the key/first pair (ie, first by the first element of the first pair, then by the second element of the first pair).

Also, how would one access the individual contents of each pair?

multimap<pair <T1, T2>, pair<T3, T4> >::iterator it = mymultimap.begin();
it->first.first;
it->first.second;
it->second.first;
it->second.second;

In other words, a multimap of pairs works exactly as expected!

Update: Also, I'd like to add that I discourage any use of pairs of pairs, it makes the code very hard to read, use structs with real variable names instead.

Viktor Sehr
Small point, you need to define operator `<` for comparison or provide object that compares these pairs in definition of multimap
Artyom
You shouldn't define operator< for `std::pair`; it's not your type. In fact, it already is defined.
MSalters
@Artyom: operator< for std::pair is defined as I said, it compares first::operator<, and if they are equal, it compares by second::operator<
Viktor Sehr
+1 for concise and straightforward answer. And yes, pair already implements operator<.