【问题标题】:Is there a way to replace string ">" with > in an 'if' condition?有没有办法在“if”条件下用 > 替换字符串“>”?
【发布时间】:2016-06-17 22:02:56
【问题描述】:

我遇到了以下用例,但找不到合适的解决方案。 有没有办法在if 条件中用条件<> 替换字符串“”?

例子:

  string condition = "<";
  if (10 condition 8)   // Here I want to replace condition with <
  {
      // Some code
  }

我不想这样做:

if ("<" == condition)
{
   if (10 < 8)
   {
   }
}
else if (">" == condition)
{
   if (10 > 10)
   {
   }
}

而且我的情况会在运行期间发生变化。我只是在寻找一种简单的方法,如果存在于上面之外。

用例:用户会给出如下查询:

input: 10 > 9   =>  output: true
input: 10 < 7   =>  output: false

基本上我需要解析这个查询,因为我有这 3 个单词(10、>、9)作为字符串,并且我想以某种方式将字符串 ">" 或 "> 或 @987654328 @。

【问题讨论】:

  • 没有。另请注意,“字符串”不是 C 类型。
  • 你可以先写#define lessThan &lt;,然后写if (10 lessThan 8)。这可能没用,但您也没有提到实际用例。
  • @melpomene 您的建议无关紧要,因为lessthen 只是他用于操作数的标识符名称...#define 将导致预编译时间并且不会在运行之间更改。
  • @TomerW 运行之间的更改不是要求的一部分。此外,如果它可以表示除&lt;之外的其他操作,为什么要称为lessThan
  • 您是否希望能够处理标准运算符的其他字符串表示形式?

标签: c++


【解决方案1】:

您可以通过std::mapstd::unordered_map 将字符串映射到标准库比较器函子,例如std::less

【讨论】:

  • 是的,但这涉及使用预处理器,因此您不能像提问者的示例中那样将字符串的值用作动态设置的运算符。不过,这是一个值得考虑的有趣策略,因为它可以让事情看起来更容易阅读。
  • 不需要为此使用预处理器。
  • @RobertColumbia:抱歉,我不明白您是如何得出结论的。不涉及预处理器。
【解决方案2】:

您不能在 C++ 中创建新运算符 (Can I create a new operator in C++ and how?)。我可以看到你的想法来自哪里,但语言不支持。但是,您可以创建一个接受两个操作数和一个字符串“参数”并返回适当值的函数。

bool CustomCompare(int operand1, int operand2, string op)
{
    if (op == "<")
    {
        return operand1<operand2;
    }
    if (op == ">")
    {
        return operand1>operand2;
    }
    if (op == "_")
    {
        return DoTheHokeyPokeyAndTurnTheOperandsAround(operand1, operand2);
    }
}

【讨论】:

  • 正如我所说,我已经考虑过它,但看看是否有另一种方法。就像标记粘贴操作符
【解决方案3】:
std::function<bool(int,int)> comparator = std::less;
if(comparator(10, 8))
{
    //some code
}

另见:

【讨论】:

    【解决方案4】:
    #include <functional>
    #include <map>
    #include <string>
    
    int main()
    {
      using operation = std::function<bool(int,int)>;
      std::map<std::string, operation> mp = 
        {{"<", std::less<int>()}, 
         {">", std::greater<int>()}};
    
      int x = 5; 
      int y = 10;
      std::string op = "<";
    
      bool answer = mp[op](x, y);
    }
    

    【讨论】:

      【解决方案5】:

      如果您是 C++ 忍者,并且非常固执地要让它按照您希望的方式工作,那么有一种方法,但它既高级又复杂。

      我主要为VxWorks 编写POSIX 代码,但我想它可以为任何其他代码完成。

      假设你有:string myExpression = "mySize &gt; minSize";

      1. “将字符串重建为 C 代码”(保存到文件,使用 ccppcgccgpp,无论您拥有什么工具链。)
      2. 您需要将它与您的代码链接,至少要为mySizeminSize 获得ELF 重定位(如果您自定义ld 命令,我认为可以使用app-load 来完成。
      3. 使用ld加载代码
      4. 跳转到您加载代码的新地址。

      说了这么多,我不建议你这样做:

      1. 非常复杂。
      2. 不是最稳定的,而且很容易出错/出错。
      3. 可能导致重大漏洞的“黑客”风格, 并且需要适当的卫生设施。

      我能看到的唯一优点是,这种方法支持 C 开箱即用的所有功能! BitWise, +-/*^!, 甚至函数 pow() 等等。

      更好一点的是:

      将函数编译为:

      `"bool comparer_AB(int a, int b) { return a " + operator + "}"`
      

      然后拨打comparer_AB();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-27
        • 1970-01-01
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        • 2019-08-22
        • 2022-06-12
        相关资源
        最近更新 更多