Sort STL Map Value

Sort STL Map Value

Use STL set to simulate map

#include <vector>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
 
class compare
{
public:
 
   //the error is over here
   bool operator()(pair<int, int> p1, pair <int, int> p2) const
   {
      if (p1.second == p2.second)
         return p1.first <p2.first;
      else
         return p1.second <p2.second;
   }
};
 
typedef set<pair<int, int>, compare> MMap;
 
void main(int argc, char* argv[])
{
	MMap D;
	D.insert(make_pair<int, int>(1, 10));
	D.insert(make_pair<int, int>(2, 2));
	D.insert(make_pair<int, int>(3, 2));
	D.insert(make_pair<int, int>(4, 3));
	D.insert(make_pair<int, int>(5, 6));
	
	MMap::const_iterator iter(D.begin());
	MMap::const_iterator end(D.end());
	for (; iter != end; ++iter)
	cout <<iter->first <<" " <<iter->second <<std::endl;
}

 

Dotblogs 的標籤: ,