C++ C++;使用运算符的字符串进行比较

C++ C++;使用运算符的字符串进行比较,c++,operators,compare,string-comparison,C++,Operators,Compare,String Comparison,我在写下面的函数,并开始认为可能有更好的方法来实现它;然而,谷歌并没有出现太多,所以任何洞察都将不胜感激。我也有一个非常类似的情况,涉及整数 bool compare_strs (std::string operator_, std::string str_0, std::string str_1) { if (operator_ == ">") { return str_0 > str1; } else if (operator_ =

我在写下面的函数,并开始认为可能有更好的方法来实现它;然而,谷歌并没有出现太多,所以任何洞察都将不胜感激。我也有一个非常类似的情况,涉及整数

bool compare_strs (std::string operator_, std::string str_0, std::string str_1)
{
    if (operator_ == ">")
    {
        return str_0 > str1;
    }
    else if (operator_ == "<")
    {
        return str_0 < str1;
    }
    else if (operator_ == "<=")
    {
        return str_0 <= str1;
    }
    else
    {
        return str_0 >= str1;
    }
}
bool compare\u strs(std::string操作符,std::string str\u 0,std::string str\u 1)
{
如果(运算符=“>”)
{
返回str_0>str1;
}

否则,如果(operator_==”如其他人所述,您应该首先问问自己为什么要这样做——可能有更好的解决方案。不过,我可能会这样做:

template <typename T1, typename T2>
bool mycompare(std::string operator_, const T1 & _lhs, const T2 & _rhs)
{
    if (operator_ == ">")
    {
        return _lhs > _rhs;
    }
    else if (operator_ == "<")
    {
        return _lhs < _rhs;
    } 
    //etc.
    else
    {
        throw new exception("Invalid operator");
    }
}
模板
bool mycompare(std::字符串运算符、常数T1和常数lhs、常数T2和常数rhs)
{
如果(运算符=“>”)
{
返回左侧>右侧;
}

else if(运算符=”您可以使用映射来存储运算符和相关的函子。在C++11中,虽然可能会有一些细微的错误,但沿着这些思路应该可以工作。在C++03中,您必须更改一些内容,包括将
std::function
更改为
boost::function
或函数指针,以及使用
std::make\u pair
以存储贴图值

#include <functional> //for std::function and std::less et al.
#include <map> //for std::map
#include <stdexcept> //for std::invalid_argument
#include <string> //for std::string

struct StringComparer {
    static bool compare( //split up to fit width
        const std::string &oper, 
        const std::string &str0, const std::string &str1
    ) {
        MapType::const_iterator iter = operations.find(oper); 
        if (iter == std::end(operations)) //check if operator is found
            throw std::invalid_argument("No match for provided operator.");

        return iter->second(str0, str1); //call the appropriate functor
    }

private:
    using MapType = std::map< //makes life easier, same as typedef
        std::string, 
        std::function<bool(const std::string &, const std::string &)>
    >;

    static const MapType operations; //a map of operators to functors
};

const StringComparer::MapType StringComparer::operations = { //define the map
    {"<", std::less<std::string>()}, //std::less is a functor version of <
    {"<=", std::less_equal<std::string>()},
    {">", std::greater<std::string>()},
    {">=", std::greater_equal<std::string>()}
};
#包括//for std::function和std::less等。
#include//for std::map
#include//for std::无效的_参数
#include//for std::string
结构字符串比较器{
静态布尔比较(//拆分到合适的宽度
常量标准::字符串和操作,
常量标准::字符串和str0,常量标准::字符串和str1
) {
MapType::const_迭代器iter=operations.find(oper);
if(iter==std::end(operations))//检查是否找到运算符
throw std::无效的_参数(“提供的运算符不匹配”);
返回iter->second(str0,str1);//调用相应的函子
}
私人:
使用MapType=std::map;
static const MapType operations;//运算符到函子的映射
};
常量StringComparer::MapType StringComparer::operations={//定义映射
{“”,std::greater()},
{“>=”,std::greater_equal()}
};

你也可以。这种方法的好处是,很容易包含更多运算符,因为你所要做的就是将它们添加到映射中。

运算符必须是字符串吗?你的变量名会让baby Bjarne Stroustrup哭。此外,如果有人将“lol”传递给运算符,会发生什么?它的行为类似于“>=”。这是专业的bably不是故意的行为。此外,你为什么需要这样做?如果你发布更多的代码会有所帮助。你可以制作一个
std::map
来存储它们。在
@Wug中已经有了预制的运算符函子。你对Bjarne的拼写让他哭了。@chris:这个问题没有标记为C++11,但这仍然是一个可以理解的好建议可以适应任何语法稍有不同的标准。+1,因为我不知道
。这很简单。
std::less
等是否自动转换为整数或执行字符串逻辑运算?(我假设在一个字符串终止之前,这是一个字符级比较)@直截了当地说,slugonamission在类型T上使用运算符<。参见a。因此,是的,对模板化类型执行逻辑运算。仍然整洁:)欢呼。@ SulgOnAsRead,它的全部用途是沿着这条线的东西。考虑<代码> STD::MAP< /COD>本身,它需要被排序。你可以指定<代码> STD::Engult作为构造函数的一个可选参数,使它向后排序,但是它是默认的参数,默认为<代码> STD::优化:您应该存储
操作的结果。查找(oper)
,而不是使用
操作[oper]
再次查看映射。