简体   繁体   English

在条件中委托std :: less或std :: more

[英]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: 我已经实现了一个测试用例,我可以将key作为自定义类,如下所示:

#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. 现在我想根据某些条件调用std :: greater / std :: less / std :: less_equal。 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. 例如,对于一个条件,我使用std :: less声明容器,对于其他条件,我使用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)>作为你在multimapCompare类型,然后存储std::lessstd::greater在创建地图时:

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 关于ideone的代码: http ://ideone.com/cP0OEm


Old compile time version below: 下面的旧编译时间版本:

If you can use c++11 , you could use std::conditional : 如果你可以使用c++11 ,你可以使用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. cond是你的条件,你可以链接std::conditional多条件。

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. 其中MY_VALUE是编译时常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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