Hi there,
Let's say we have the following:
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<string, int> Data;
class DataCompare
{
public:
bool operator()(const Data& el1, const Data& el2) const
{
return lessK(el1.first, el2.first);
}
bool operator() (const Data& el1, const Data::first_type& k) const
{
return lessK(el1.first, k);
}
bool operator ()(const Data::first_type& k, const Data& el2) const
{
return lessK(k, el2.first);
}
private:
bool lessK(const Data::first_type& k1, const Data::first_type& k2) const
{
return k1 < k2;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<Data> dataVector;
// .. do some insertion
sort(dataVector.begin(), dataVector.end(),DataCompare());
// ... do something again
return 0;
}
This class works fine and does what needs to be done as long as same pair def in use.
If I would like to use different pair (<int, string>) what would be the beast aproach?
Should there be differnt typedef? Is there better aproach other then creation of new class 'CompareData'?
Any suggestion for reading are more then welcome.
Regards