运行时指定的多个谓词 [英] Multiple predicates specified during runtime

查看:130
本文介绍了运行时指定的多个谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在STL中有操作符类,如less,equal_to,greater_equal等。如何轻松地结合使用例如remove_if函数?

There are operator classes in STL like less, equal_to, greater_equal etc. How to easily combine them to use with for example remove_if function?

在大于0且小于3且不等于2的向量元素中删除,则它将是如下:

For example I want to remove in vector elements which are greater than 0 AND less than 3 AND not equal to 2, then it would be something like:

remove_if (v.begin(), v.end(), bind2nd(greater<int>(),0) + bind2nd(less<int>(),3) + not1(bind2nd(equal_to<int>(), 2)));

程序运行期间的用户可以指定过滤选项,例如他可以写:remove if x> 0& ;& x < 3&&& x!= 2,或者他可以写:如果x> 5 ||则删除

User during running of program can specify filtering options for example he can write: remove if x > 0 && x < 3 && x != 2, or he can write: remove if x > 5 || x == 3. Then command is parsed and appropriate operators with their arguments are combined together to one predicate.

推荐答案

这是相当于

您首先需要解析用户给出的语句,并将其转换为AST(抽象语法树)。事实证明,这个AST几乎已经合适了。

You first need to parse the statement the user gave, and turn it into an AST (Abstract Syntax Tree). It turns out that this AST is nearly already suitable.

x > 0 && x < 3 && x != 2

可以表示为树:

      AND
     /   \
    >     AND
   / \   /    \
  x  0   <    !=
        / \   / \
       x   3 x   2

应该继承一个公共基类,并且您应该实现一个 Visitor 来为给定值计算 x 参数。

All nodes should inherit from a common base class, and you should implement a Visitor to evaluate the x parameter for a given value.

这篇关于运行时指定的多个谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆