【发布时间】:2020-06-12 10:55:54
【问题描述】:
如果我有 lambda 作为表达式,如何编写“查找”函数定义:
int main() {
vector<Student> v = { Student("Pero", "Peric", "3882"),
Student("Ivo", "Ivic", "10991"),
Student("Mara", "Maric", "40911"),
Student("Ivan", "Juric", "93877"),
Student("Ivo", "Kalic", "20991")
};
vector<Student*> r = find(v, [](Student* s) { return s->name() == "Ivo"; });
【问题讨论】:
-
惯用的方式是使用模板函数。类似
template <typename F> auto find(const std::vector<Student>& vec, F find) { ... }。 -
显示完整代码,但首先请尝试搜索。
-
不相关,但我会称之为 find_all_if 之类的,find 有点暗示它会得到一个元素而不是所有元素(至少标准库 find 是这样做的)
-
如果
find是std::find_if你应该把Student* s改成const Student& s
标签: c++