C ++ 11使用std :: equal_range和自定义比较函数

时间:2014-02-10 11:23:55

标签: c++ c++11

考虑这个例子

请注意,这只是我用来说明问题的方法。我很清楚有更有效的方法来解析算术表达式,虽然主题很吸引人,但这与我的实际情况无关问题。如果我可以这么说的话,这只是一个半现实的例子。
我同意解析器可能会使问题看起来更复杂,但我想不出更抽象的例子

假设您想要一个简单的表达式解析器。你将从一个标记器中获取一些字符串,其中一些字符串可能不明确。

例如,字符串“ - ”可以表示一元减号或二进制减号。

假设您希望获得字符串“ - ”的所有可能含义。

你可以这样做:

1)定义一个描述所有可能的运算符的排序数组

// types of operators
enum class opType: char { unary, lasso, rasso, none };

// operator descriptors
struct opDesc {
    string symbol;
    opType type;
    char   priority;

    // partial order comparison
    bool operator< (const opDesc& a) const
    {
        // unary operators first
        if (symbol == a.symbol) return type < a.type;
        return symbol < a.symbol;
    }

    // comparison with strings
    static bool comp_desc_str (const opDesc& a, const string& s)
    {
        return a.symbol < s;
    }
    static bool comp_str_desc (const string& s, const opDesc& a)
    {
        return s < a.symbol;
    }
};

static opDesc op_descriptors[] = {
    { "+" , opType::unary, 8 }, // unary +
    { "-" , opType::unary, 8 }, // unary -
    { "*" , opType::lasso, 6 }, // multiplication
    { "/" , opType::lasso, 6 }, // division
    { "+" , opType::lasso, 5 }, // addition
    { "-" , opType::lasso, 5 }, // substraction
};

2)使用std::equal_range获取给定字符串的所有可能匹配

// sort descriptors by value and type
sort(begin(op_descriptors), end(op_descriptors));

// do some searches
string patterns[] = { "+", "-", ">>", "**" };

for (string s : patterns)
{
    pair<opDesc*, opDesc*> ops;

    ops = equal_range(
        std::begin(op_descriptors), 
        std::end  (op_descriptors), 
        s,
        opDesc::comp_desc_str);

    cout << s <<": "<< ops.first[0] << ops.second[-1] << endl;
}

此代码无法编译,抱怨opDesc::comp_desc_str(它反过来预期参数,即string首先,opDesc下一步。)

如果我尝试将该函数替换为以相反顺序获取其参数的版本:

ops = equal_range(
    std::begin(op_descriptors), 
    std::end  (op_descriptors), 
    s,
    opDesc::comp_str_desc);

它也不会编译,抱怨参数再次以错误的顺序(在算法的某个其他位置)。

但是,此代码可以使用(see a live version here

#include <regex>
#include <iostream>

using namespace std;

// types of operators
enum class opType: char { unary, lasso, rasso, none };

// operator descriptors
struct opDesc {
    string symbol;
    opType type;
    char   priority;

    // partial order comparison
    bool operator< (const opDesc& a) const
    {
        // unary operators first
        if (symbol == a.symbol) return type < a.type;
        return symbol < a.symbol;
    }

    // comparison with strings
    static bool comp_desc_str (const opDesc& a, const string& s)
    {
        return a.symbol < s;
    }
    static bool comp_str_desc (const string& s, const opDesc& a)
    {
        return s < a.symbol;
    }

    // display
    friend ostream& operator<<(ostream& os, const opDesc& op);
};

ostream& operator<<(ostream& os, const opDesc& op)
{
    os << op.symbol << "[" << (int)op.type << ":" << (int)op.priority << "]";
    return os;
}

static opDesc op_descriptors[] = {
    { "+" , opType::unary, 8 }, // unary +
    { "-" , opType::unary, 8 }, // unary -
    { "~" , opType::unary, 8 }, // bitwise not
    { "**", opType::rasso, 7 }, // power
    { "*" , opType::lasso, 6 }, // multiplication
    { "/" , opType::lasso, 6 }, // division
    { "%" , opType::lasso, 6 }, // remainder
    { "+" , opType::lasso, 5 }, // addition
    { "-" , opType::lasso, 5 }, // substraction
    { "<<", opType::lasso, 4 }, // left shift
    { ">>", opType::lasso, 4 }, // right shift
    { "&" , opType::lasso, 3 }, // bitwise and
    { "^" , opType::lasso, 2 }, // bitwise xor
    { "|" , opType::lasso, 1 }, // bitwise or 
    { "(" , opType::none , 0 }, // braces
    { ")" , opType::none , 0 }
};

int main(void)
{
    // sort descriptors by value and type
    sort(begin(op_descriptors), end(op_descriptors));

    // do some searches
    string patterns[] = { "+", "-", ">>", "**" };

    for (string s : patterns)
    {
        pair<opDesc*, opDesc*> ops;
        // this won't work
        /*
        ops = equal_range(
            std::begin(op_descriptors), 
            std::end  (op_descriptors), 
            s,
            opDesc::comp_desc_str or opDesc::comp_str_desc);
        */
        // this works
        ops.first = lower_bound(
            std::begin(op_descriptors), 
            std::end  (op_descriptors), 
            s, opDesc::comp_desc_str);
        ops.second = upper_bound(
            std::begin(op_descriptors), 
            std::end  (op_descriptors), 
            s, opDesc::comp_str_desc);
        cout << s <<": "<< ops.first[0] << ops.second[-1] << endl;
    }
}

输出:

+: +[0:8]+[1:5]     // unary and binary "-" operators found
-: -[0:8]-[1:5]     // same thing for "+"
>>: >>[1:4]>>[1:4]  // first == second when there is only
**: **[2:7]**[2:7]  // one version of the operator

我在VisualC ++ 2013和g ++上尝试了这个代码,结果相同
(只有模板错误消息的混淆变化)。

问题

  • lower_boundupper_bound应该要求两个不同的自定义比较函数有特殊原因吗?
  • 在使用仿函数解决问题时,是否存在MSVC在调试版本中抛出虚假错误的解决方法?
  • 是否有针对此问题的明确解决方法(即按预期使用equal_range而不是两次执行作业使其在Visual C ++ 2013上以调试模式进行编译)?

3 个答案:

答案 0 :(得分:6)

std::lower_bound需要comp(*it, val)std::upper_bound需要comp(val, *it)

因此,您的comp仿函数必须同时提供bool operator () (const opDesc& a, const string& s) constbool operator ()(const string& s, const opDesc& a) const

因此,您可以使用以下comp仿函数:

struct lessOpDescWithString
{
    bool operator () (const opDesc& lhs, const std::string& rhs) const {
       return opDesc::comp_desc_str(lhs, rhs);
    }

    bool operator () (const std::string& lhs, const opDesc& rhs) const {
       return opDesc::comp_str_desc(lhs, rhs);
    }
};

答案 1 :(得分:3)

我认为std::lower_boundstd::upper_bound将在您排序的数组上使用二进制搜索,并且必须能够将obDescstd::string进行比较,反之亦然。我建议制作像

这样的比较器
struct obDescStrCmp {
    bool operator()(const opDesc& lhs, const opDesc& rhs) const {
       // code to compare obDesc to opDesc
    }

    bool operator()(const opDesc& lhs, const std::string& rhs) const {
       // code to compare obDesc to std::string
    }

    bool operator()(const std::string& lhs, const opDesc& rhs) const {
       // code to compare std::string to opDesc
    }

    bool operator()(const std::string& lhs, const std::string& rhs) const {
       // code to compare std::string to std::string
       // I'm not sure if this is really necessary.
    }
};

并将其传递给您选择的std算法,而不是依赖于opDesc结构中定义的运算符。编译器应根据std算法实现中参数的实际顺序选择正确的重载。

修改:将operator<替换为operator()以使结构可调用。

答案 2 :(得分:3)

我自己的答案,只是总结其他贡献,特别是Jarod42的:

为什么要进行两次比较

equal_range算法要求内部(本例中为>)和外部(<)类型之间进行opDescstd::string比较。

由于a<b案例,您无法从!(b<a)推断==,因此您必须提供两种不同的比较器。

从功能上讲,你可以选择比较操作的任何工作组合,例如<><<=,但是std :: guys已经选择了固定<与交换的参数进行比较,这是由函数签名决定的选择:它只需要定义(type, foreign type)(foreign type, type)变体。

lower_bound只需要<(表示为type < foreigh type)而upper_bound只需要>(表示为foreign type < type),因此两者都可以使用单个函数,但equal_range必须能够访问这两个原型。

这样做的方法

实际的解决方案是定义一个函数对象aka functor来完成这项工作:

// operator descriptors
struct opDesc {
    string symbol;
    opType type;
    char   priority;

    // partial order comparison
    bool operator< (const opDesc& a) const
    {
        // unary operators first
        if (symbol == a.symbol) return type < a.type;
        return symbol < a.symbol;
    }

    // functor to compare with strings
    struct comp
    {
        bool operator() (const opDesc& a, const std::string& b) const
        {
           return a.symbol < b;
        }

        bool operator() (const std::string& a, const opDesc& b) const
        {
           return a < b.symbol;
        }
    };

并像这样使用它:

pair<opDesc*, opDesc*> ops;
ops = equal_range(
    std::begin(op_descriptors), 
    std::end  (op_descriptors), 
    s,
    opDesc::comp()); // <- functor to provide two different comparison functions

MSVC错误

此外,由于仅在调试模式下启用了一个模糊的偏执检查,因此无法在MSVC ++ 2013上编译。发布版本编译正常,g ++中的代码也是如此,无论调试级别如何。

从使用的神秘名称判断,似乎模板检查比较是否定义了一个总顺序(由于该API的整个要点是对部分有序的结构起作用,所以不应该这样做。)

我当前(丑陋)的解决方法是禁用一些内部调试标志:

#if (defined _MSC_VER && defined _DEBUG)
#define _ITERATOR_DEBUG_LEVEL 1
#endif // _MSC_VER && _DEBUG
在包含std :: headers

之前

Jarod42建议的另一种可能的解决方法是定义缺少的比较函数。

    // functor to compare with strings
    struct comp
    {
        bool operator() (const opDesc& a, const std::string& b)
        { return a.symbol < b; }
        bool operator() (const std::string& a, const opDesc& b)
        { return a < b.symbol; }

        // just to make Microsoft Visual C++ happy when compiling in debug mode
        bool operator() (const opDesc& a, const opDesc& b)
        { assert(false); return false; }
    };
相关问题