【问题标题】:delegate std::less or std::greater in condition在条件下委托 std::less 或 std::greater
【发布时间】:2016-04-19 09:03:30
【问题描述】:

我已经实现了一个测试用例,我可以将键作为自定义类,如下所示:

#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;
 }

现在我想根据某些条件调用 std::greater / std::less / std::less_equal 。我该如何委托?例如,对于一种情况,我用 std::less 声明容器,而对于另一种情况,我用 std::greater 声明容器。

如果有一些提示或参考会很棒。

谢谢

【问题讨论】:

  • 关于编译时条件?还是运行时一?
  • 我想在运行时使用它
  • 您正在使用 std::map 中的 const_iteratorstd::greater 来迭代 std::less,这是有意的吗?
  • 如果你只想在更大和更少之间切换,你可以使用反向迭代器。无法在运行时更改模板参数(比较)。
  • 我很抱歉最后的评论。我想在编译时使用它。并且迭代器仅测试比较器 -less/greater 并且它是一个错误。它应该是 std::less_equal 。

标签: c++


【解决方案1】:

看起来您想要一个运行时版本(来自您的评论),您可以使用类型 std::function&lt;bool(int, int)&gt; 作为您的 Compare 类型在 multimap 中,然后在创建地图时存储 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);

ideone 上的代码:http://ideone.com/cP0OEm


以下旧编译时版本:

如果你可以使用c++11,你可以使用std::conditional

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

cond 是您的条件,您可以将std::conditional 链接到多个条件。

然后:

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

由于您没有说明您的条件是什么,我无法添加更多信息。

在您发表评论后,可能如下所示:

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;

其中MY_VALUE 是编译时间常数。

【讨论】:

  • 如果某个特定值大于0,则容器的内容按升序排列,否则在插入容器时按降序排列。
  • @user1355185 这个特定的值在编译时是已知的?
  • 我强烈猜测它是运行时的。让我再分解一下。我有一条线段的端点。我通过从起点减去终点来找出线段的方向。如果方向大于零,则按升序存储坐标值,否则按降序存储。
  • @user1355185 我怀疑这是编译时间,或者你有一个非常奇怪的程序......我已经更新了我的答案以提供运行时版本。
  • 为什么第一个代码块中的变量cmp不能用三元赋值?当我尝试这样做时,我得到一个编译时错误:Incompatible operand types。但是如果我将三元的两个条件都设置为std::greater,那么它有效吗?
猜你喜欢
  • 2013-01-27
  • 1970-01-01
  • 2012-10-10
  • 2021-01-07
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
相关资源
最近更新 更多