C++;相当于在python中将lambda函数作为参数传递

C++;相当于在python中将lambda函数作为参数传递,python,c++,function,lambda,conditional-statements,Python,C++,Function,Lambda,Conditional Statements,在python中,您可以将lambda函数作为参数传递,如下所示: class Thing(object): def __init__(self, a1, a2): self.attr1 = a1 self.attr2 = a2 class ThingList(object): def __init__(self): self.things = [Thing(1,2), Thing(3,4), Thing(1,4)] d

在python中,您可以将lambda函数作为参数传递,如下所示:

class Thing(object):
    def __init__(self, a1, a2):
        self.attr1 = a1
        self.attr2 = a2

class ThingList(object):
    def __init__(self):
        self.things = [Thing(1,2), Thing(3,4), Thing(1,4)]

    def getThingsByCondition(self, condition):
        thingsFound = []
        for thing in self.things:
            if condition(thing):
                thingsFound.append(thing)
        return thingsFound

things = tl.getThingsByCondition(lambda thing: thing.attr1==1)
print things
有没有办法在C++中做类似的事情?我之所以需要这样做,是因为我想在对象的
向量中搜索满足特定条件的对象


好吧,我试着这样解决它: 我应该提到,我在向量中管理的“东西”是员工,我希望找到满足某些条件的员工

employee_vector getEmployeeByCondition(function<bool(const Employee&)> condition) {
    employee_vector foundEmployees;
    for (int i = 0; i < employees.size(); i++) {
        Employee e = employees.at(i);
        if (condition(e)) {
            foundEmployees.push_back(e);
        }
    }
    return foundEmployees;
}

employee_vector getEmployeeByBirthday(Date bday) {
    employee_vector found;
    found = getEmployeeByCondition([](Employee e) {return e.birthday == bday;});
    return found;
}
employee\u向量getEmployeeByCondition(函数条件){
雇员(雇员);;
对于(int i=0;i

现在的问题是,我显然不能在
getEmployeeByBirthday
中的lambda函数中使用
bday
,因为它是一个局部变量。

是的,可以将lambda传递给函数

您可以使用以下模板:

template <typename Func>
void foo (Func f) {
    f();
}

foo([]{ std::cout << "Hello"; });
如果要在
std::vector
中搜索满足某些条件的对象,可以使用
std::find\u If

std::find_if(my_vec.begin(), my_vec.end(), 
             [](auto& el) { return /*does this fulfil criteria?*/; });
至于
getThingsByCondition
,它可能看起来像这样:

template <typename T, typename...Ts, typename Func>
std::vector<std::reference_wrapper<T>>
filter(std::vector<T,Ts...>& container, const Func& func) {
    std::vector<std::reference_wrapper<T>> ret;

    for (auto& el : container) {
        if (func(el)) ret.push_back(std::ref(el));   
    }

    return ret;
}
模板
向量
过滤器(标准::向量和容器、常量函数和函数){
std::载体ret;
用于(自动和el:容器){
if(func(el))返回推送(std::ref(el));
}
返回ret;
}

这当然可以改进,以适用于不同的容器,等等。它几乎肯定不适用于
std::vector

您可以像这样设计
getThingsByCondition

using things_vec = std::vector<std::reference_wrapper<Thing>>;

things_vec getThingsByCondition(std::function<bool(const Thing&)> condition);

看起来很有趣,谢谢。我问的原因也是我需要通过比较各种属性来查找对象。现在我不想写一个每次循环检查向量的函数,因为我必须写6个完全相同的函数,我认为这不是正确的方法。这就是为什么我希望保留条件变量并将其传递给函数的原因。请注意,在python中,您可以简单地将getThingsByCondition方法实现为:def getThingsByCondition(self,condition):return filter(condition,self.things)
getThingsByCondition
最好返回类似于
std::vector
的内容,以避免复制并允许您修改找到的对象。看起来很有趣,但在尝试第二个时,Visual Studio告诉我“函数不是模式”。我这样声明函数
vector getEmployeeByCondition(函数条件){…}
您使用的是什么版本的MSVC?你是否包含了?不,我没有,这就是问题所在。
using things_vec = std::vector<std::reference_wrapper<Thing>>;

things_vec getThingsByCondition(std::function<bool(const Thing&)> condition);
auto things = getThingsByCondition([](const Thing& thing) { return thing.attr1 == 1; })