简体   繁体   中英

delegate std::less or std::greater in condition

I have already implemented a test case where I can have key as custom class as follows:

#include <iostream>
#include <map>

using namespace std;

class CoordinateValue
{
public:

    short int x_;
    short int y_;

    CoordinateValue(short int x = 0, short int y = 0)
    : x_(x),y_(y)
    {

    }

    bool operator<=(const CoordinateValue &right) const
    {
      return ((this->x_ <= right.x_) && (this->y_ <= right.y_));
    }

    bool operator<(const CoordinateValue &right) const
    {
      return ((this->x_ < right.x_) && (this->y_ < right.y_));
    }

    bool operator>=(const CoordinateValue &right) const
    {
      return ((this->x_ >= right.x_) && (this->y_ >= right.y_));
    }

    bool operator>(const CoordinateValue &right) const
    {
      return ((this->x_ > right.x_) && (this->y_ > right.y_));
    }


    friend ostream &operator<<(ostream &out, const CoordinateValue &val)
    {
      out << "[ " << val.x_ << "," << val.y_ << " ]" << std::endl;

      return out;
    }
} ;

int main()
{

    std::multimap<CoordinateValue,int,std::less_equal<CoordinateValue>> intersectionIn;


    intersectionIn.insert(std::pair<CoordinateValue,int>(CoordinateValue(100,200),12));
    intersectionIn.insert(std::pair<CoordinateValue,int>(CoordinateValue(1000,7),135));
    intersectionIn.insert(std::pair<CoordinateValue,int>(CoordinateValue(0,2),112));
    intersectionIn.insert(std::pair<CoordinateValue,int>(CoordinateValue(-10,-200),12));
    intersectionIn.insert(std::pair<CoordinateValue,int>(CoordinateValue(-100,-200),12));
    intersectionIn.insert(std::pair<CoordinateValue,int>(CoordinateValue(1000,2000),12));

    std::multimap<CoordinateValue,int,std::greater<CoordinateValue>>::const_iterator iter = intersectionIn.begin();

    while(iter != intersectionIn.end())
    {
        std::cout << iter->first;
        ++iter;
    }

    return 0;
 }

Now I want call std::greater / std::less / std::less_equal based on some condition. How do I delegate ? For example for one condition I declare the container with std::less and for other condition I declare the container with std::greater.

Some hint or reference would be great to have.

Thanks

It looks like you want a runtime version (from your comment), you can use the type std::function<bool(int, int)> as your Compare type in multimap and then store either std::less or std::greater when creating the map:

std::function<bool(int, int)> cmp;
if (...) {
    cmp = std::less<int>();
}
else {
    cmp = std::greater<int>();
}
std::multimap<int, int, decltype(cmp)> map(cmp);

Code on ideone: http://ideone.com/cP0OEm


Old compile time version below:

If you can use c++11 , you could use std::conditional :

template <typename T>
using cmp = typename std::conditional<cond, std::greater<T>, std::less<T>>::type;

Where cond is your condition, and you can chain std::conditional for multiple conditions.

And then:

std::multimap<int, int, cmp<int>> map;

Since you did not say what your conditions were, I cannot add more information.

After your comment, this could look like this:

template <int value, typename T>
using cmp = typename std::conditional<(value > 0), std::less<T>, std::greater<T>>::type;

std::multimap<int, int, cmp<MY_VALUE, int>> map;

Where MY_VALUE is a compile time constant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM