谓词(predicate)与 lambda 表达式的使用

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

//谓词(predicate)结构
struct Greater_than { int val; Greater_than(int v) :val{ v } {} bool operator()(const pair<string, int>& r) { return r.second > val; } }; void f(map<string, int>& m) { auto p = find_if(m.begin(), m.end(), Greater_than{ 42 }); //谓词使用 cout << p->first << ' ' << p->second << endl; } int main() { map<string, int> m; m.insert(make_pair("hello",25)); m.insert(make_pair("world", 35)); m.insert(make_pair("smart", 49)); m.insert(make_pair("boy", 43)); f(m); int cxx = count_if(m.begin(), m.end(), [](const pair<string, int>& r) {return r.second > 42; }); //lambda表达式的使用 cout << cxx << endl; return 0; }

猜你喜欢

转载自www.cnblogs.com/lhb666aboluo/p/12664580.html
今日推荐