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

Recommended Answers

All 3 Replies

// use a template class
template <typename T, typename T2> class DataCompare {
public: // you can probably do this better, I just directly modified your example
// It probably won't suit your needs, etc etc, might have typos
  bool operator()(const T& el1, const T& el2) const {
    return lessK(el1.first, el2.first);
  }
  bool operator() (const T2& el1, const T& k) const {
    return lessK(el1.first, k);
  }
  bool operator ()(const T& k, const Data& T2) const {
    return lessK(k, el2.first);
  }
private: // I was really lazy about this part because it's an example
  bool lessK(const T& k1, const T2& k2) const {
    return k1 < k2;
  }
  bool lessK(const T2& k1, const T& k2) const {
    return k1 < k2;
  }
  bool lessK(const T2& k1, const T2& k2) const {
    return k1 < k2;
  }
  bool lessK(const T& k1, const T& k2) const {
    return k1 < k2;
  }
};

Just a push in the right direction, good luck!

Edit: After looking it over, you can probably just do this

template <typename T, typename T2> class DataCompare {
public:
  bool operator()(const T& el1, const T& el2) const {
    return el1.first < el2.first;
  }
  bool operator() (const T& el1, const T2& k) const {
    return el1.first < k;
  }
  bool operator ()(const T2& k, const T2& k2) const {
    return k < el2.first;
  }
};

As long as the < operator is overloaded.

Hm, very nice, I was stuck and looking at 'pair',
Thanks a lot

After second thought,

bool operator() (const T& el1, const T2& k) const 
      {
        return el1.first < k;
      }
      bool operator ()(const T2& k, const T2& el2) const 
      {
        return k < el2.first;
      }

Since in the reality, you try to re-define same operator, your suggestion will work fine with assumption that entire pair will be passed as argument, this won’t cover the cases when we want to pass just ‘pair.first’, still could work for me but it won’t work for ‘pair’. (const T& el1, const T2& k - k is not el2.first, it just another T2 and not T2::first_type).
Regards, and thanks again for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.