I have to admit I've never made a proper study of the standard library, but this caught me by surprise
#include <iostream>
#include <map>
int main (int argc, char ** argv)
{
std::multimap<int, int> mm1;
mm1.insert( std::pair<int, int>(0, 0) );
mm1.insert( std::pair<int, int>(0, 1) );
std::multimap<int, int> mm2;
mm2.insert( std::pair<int, int>(0, 1) );
mm2.insert( std::pair<int, int>(0, 0) );
std::cout << "The maps are "
<< ( (mm1 == mm2) ? "equal" : "not equal" )
<< std::endl;
}
And trying it we get ...
$ g++ --std=c++11 -o multimaporder multimaporder.cc
$ ./multimaporder
The maps are not equal
So, the order you put things into the map matters. That would not have been my first expectation for an associative container, but fine.
With a random access container my next plan would usually be to sort them and then compare ... but adding
#include <algorithm>
//...
std::sort( mm1.begin(), mm1.end() );
std::sort( mm2.begin(), mm2.end() );
std::cout << "The *sorted* maps are "
<< ( (mm1 == mm2) ? "equal" : "not equal" )
<< std::endl;
just results is a lot of complaints about not finding an acceptable set of iterators to hand to sort
.
So what is the right way to find out if two multimap
s have the same contents?
For this case, you need to compare the sets like below
bool comp ()
{
using Map = std::multimap<int, int>;
Map mm1;
mm1.insert( std::pair<int, int>(0, 0) );
mm1.insert( std::pair<int, int>(0, 1) );
Map mm2;
mm2.insert( std::pair<int, int>(0, 1) );
mm2.insert( std::pair<int, int>(0, 0) );
return std::set<Map::value_type>(mm1.begin(), mm1.end()) == std::set<Map::value_type>(mm2.begin(), mm2.end());
}