I’ve been trying to do a custom sort (or any sort) of a vector of pairs. The code below works fine when compiled as C++, but when run from the root interpreter, it gives the error:
when it comes to the “sort” command.
Any advice would be appreciated.
Version 5.34/07, Mac OS 10.6.8
Thanks,
Eric
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
struct mySort : public binary_function<pair<int,int> &,pair<int,int> &,bool> {
bool operator()(const pair<int, int> &p1, const pair<int, int> &p2) {
return(p1.second>p2.second);
}
};
int main() {
vector<pair<int,int> > mvec;
mvec.push_back(pair<int, int>(2200,5));
mvec.push_back(pair<int, int>(13, 17));
mvec.push_back(pair<int, int>(16, 1));
for(int i = 0; i< mvec.size() ; i++) cout << mvec[i].first<<" : "<< mvec[i].second<<endl;
sort(mvec.begin(),mvec.end(),mySort());
for(int i = 0; i< mvec.size() ; i++) cout << mvec[i].first<<" : "<< mvec[i].second<<endl;
return(0);
}