C++中lambda表达式的使用
lambda基本规则
基本写法如下:
auto lambda = []() option -> retType(option){
//statements...
};
解释:
- 利用auto获取lambda对象,之后可以直接调用此对象
- []内部捕获变量,把要使用的变量写进[],如[x,&y];x传值,y传引用
- ()和函数的相同,传入变量
- option可以是mutable或throw
- retType返回值类型可选
- 如果要直接调用lambda,形式是在表达式后面加(),如
auto lambda = []() option -> retType(option){
//statements...
}();
lambda用法举例
lambda用法一:替代Binder
lambda用起来比Binder更方便
#include <iostream>
int main(void)
{
auto plus10 = std::bind(std::plus<int>(), std::placeholders::_1, 10);
std::cout << "+10 " << plus10(7) << std::endl;
auto plus10lambda = [](int i) {
return i + 10;
};
std::cout << "+10 " << plus10lambda(7) << std::endl;
return 0;
}
lambda用法二:替换函数对象
之前用MeanValue类的函数对象实现了求平均值,现在改用lambda
#include <iostream>
#include <vector>
#include <algorithm>
int main(void)
{
std::vector<int> coll = {1, 2, 3, 4, 6, 78, 34, 12, 56, 8};
long sum = 0;
std::for_each(coll.begin(), coll.end(), [&sum](int elem) {
sum += elem;
});
double meanvalue = sum / coll.size();
std::cout << "mean value = " << meanvalue << std::endl;
return 0;
}
使用lambda移除元素
注意:
- remove_if()算法会拷贝lambda,导致重复行为,所以要以引用方式传入count;
- ++count == 3和count++ == 3行为是不同的
#include <iostream>
#include <list>
#include <algorithm>
int main(void)
{
std::list<int> coll = {1, 2, 3, 4, 6, 78, 34, 12, 56, 8};
int count = 0;
auto pos = std::remove_if(coll.begin(), coll.end(), [&count](int) {
return ++count == 3;
});
coll.erase(pos, coll.end());
for (auto i : coll) {
std::cout << i << std::endl;
}
return 0;
}
lambda用法三:调用全局函数
#include <iostream>
#include <algorithm>
#include <string>
#include <locale>
char myToupper(char c)
{
std::locale loc;
return std::use_facet<std::ctype<char>> (loc).toupper(c);
}
int main(void)
{
std::string s("Internationalization");
std::string sub("Nation");
auto pos = std::search(s.begin(), s.end(), sub.begin(), sub.end(), [](char c1, char c2) {
return myToupper(c1) == myToupper(c2);
});
if (pos != s.end()) {
std::cout << "\"" << sub << "\" is part of \"" << s << "\"" << std::endl;
}
return 0;
}
lambda用法四:调用成员函数
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
class Person
{
private:
std::string name;
public:
Person(const std::string& n) : name(n) {}
void print() const
{
std::cout << name << std::endl;
}
void printPrefix(const std::string& prefix) const
{
std::cout << prefix << name << std::endl;
}
};
int main(void)
{
std::vector<Person> coll = {Person("Tick"), Person("Trick"), Person("Track")};
std::for_each(coll.begin(), coll.end(), [](const Person & p) {
p.print();
});
std::cout << std::endl;
std::for_each(coll.begin(), coll.end(), [](const Person & p) {
p.printPrefix("Preson: ");
});
std::cout << std::endl;
return 0;
}
lambda用法五:lambda作为Hash函数、排序准则或相等准则
注意:
要将lambda的类型传给unordered_set,而使用函数对象则不用这么做。
#include <iostream>
#include <string>
#include<unordered_set>
class Person
{
private:
std::string name;
public:
Person(const std::string& n) : name(n) {}
void print() const
{
std::cout << name << std::endl;
}
void printPrefix(const std::string& prefix) const
{
std::cout << prefix << name << std::endl;
}
};
int main(void)
{
auto hash = [](const Person & p) {
};
auto eq = [](const Person & p1, const Person & p2) {
};
std::unordered_set<Person,decltype(hash),decltype(eq)> pset(10,hash,eq);
return 0;
}