博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C++标准库】STL函数对象及Lambda
阅读量:5020 次
发布时间:2019-06-12

本文共 7195 字,大约阅读时间需要 23 分钟。

函数对象function object,又称仿函数functors,是定义了operator()的对象。

class FunctionObjectType{    public:        void operator() ()            {                //statement            }}

1 /* The following code example is taken from the book 2 * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 * 5 * (C) Copyright Nicolai M. Josuttis 2012. 6 * Permission to copy, use, modify, sell and distribute this software 7 * is granted provided this copyright notice appears in all copies. 8 * This software is provided "as is" without express or implied 9 * warranty, and with no claim as to its suitability for any purpose.10 */11 #include 
12 #include
13 #include
14 #include
15 #include
16 using namespace std;17 18 19 /* class Person20 */21 class Person {22 private:23 string fn; // first name24 string ln; // last name25 public:26 Person() {27 }28 Person(const string& f, const string& n)29 : fn(f), ln(n) {30 }31 string firstname() const;32 string lastname() const;33 // ...34 };35 36 inline string Person::firstname() const {37 return fn;38 }39 40 inline string Person::lastname() const {41 return ln;42 }43 44 ostream& operator<< (ostream& s, const Person& p)45 {46 s << "[" << p.firstname() << " " << p.lastname() << "]";47 return s;48 }49 50 51 /* class for function predicate52 * - operator () returns whether a person is less than another person53 */54 class PersonSortCriterion {55 public:56 bool operator() (const Person& p1, const Person& p2) const {57 /* a person is less than another person58 * - if the last name is less59 * - if the last name is equal and the first name is less60 */61 return p1.lastname()
PersonSet;80 81 // create such a collection82 PersonSet coll;83 coll.insert(p1);84 coll.insert(p2);85 coll.insert(p3);86 coll.insert(p4);87 coll.insert(p5);88 coll.insert(p6);89 coll.insert(p7);90 91 // do something with the elements92 // - in this case: output them93 cout << "set:" << endl;94 PersonSet::iterator pos;95 for (pos = coll.begin(); pos != coll.end(); ++pos) {96 cout << *pos << endl;97 }98 }
View Code

for_each获取function object的状态

#include 
#include
#include
using namespace std;class MeanValue{private: long num; //num of elements long sum; //sum of valuespublic: MeanValue():num(0),sum(0) { } void operator() (int elem) { ++num; sum += elem; } double value() { return static_cast
(sum) / static_cast
(num); }};int main(){ vector
coll = { 1,2,3,4,5,6,7,8 }; MeanValue mv = for_each(coll.begin(), coll.end(), MeanValue()); cout << "mean value:" << mv.value() << endl; return 0;}
View Code

 预定义的function object

C++标准库提供了许多预定义的function object和binder,后者允许你合成更多精巧的function object。使用时需包含头文件<functional>

bind()函数对象适配器

1 #include 
2 #include
3 using namespace std; 4 using namespace std::placeholders; //定义占位符 5 6 int main() 7 { 8 auto plus10 = bind(plus
(), _1, 10); //以占位符_1表示第一个参数,以10为第二个参数 9 cout << "+10: " << plus10(7) << endl; //1710 11 auto plus10times2 = bind(multiplies
(),12 bind(plus
(),13 _1, 10),14 2);15 cout << "+10*2: " << plus10times2(7) << endl; //3416 17 auto pow3 = bind(multiplies
(),bind(multiplies
(),_1, _1),_1);18 cout << "x*x*x: " << pow3(7) << endl;19 20 auto inversDivide = bind(divides
(), _2, _1);21 cout << "invdiv: " << inversDivide(49, 7) << endl; // 1/722 return 0;23 }
View Code

 运用Lambda

Lambda提供了一种直观、易读的方式,可将独特的行为传递给算法和容器的成员函数。

Lambda表达式完整声明格式如下:

[capture list] (params list) mutable exception-> return type { function body }

其中:

  1. capture list:捕获外部变量列表
  2. params list:形参列表
  3. mutable指示符:用来说用是否可以修改捕获的变量
  4. exception:异常设定
  5. return type:返回类型
  6. function body:函数体

Lambda调用全局函数

1 /* The following code example is taken from the book 2 * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 * 5 * (C) Copyright Nicolai M. Josuttis 2012. 6 * Permission to copy, use, modify, sell and distribute this software 7 * is granted provided this copyright notice appears in all copies. 8 * This software is provided "as is" without express or implied 9 * warranty, and with no claim as to its suitability for any purpose.10 */11 #include 
12 #include
13 #include
14 #include
15 using namespace std;16 17 char myToupper(char c)18 {19 std::locale loc;20 return std::use_facet
>(loc).toupper(c);21 }22 23 int main()24 {25 string s("Internationalization");26 string sub("Nation");27 28 // search substring case insensitive29 string::iterator pos;30 pos = search(s.begin(), s.end(), // string to search in31 sub.begin(), sub.end(), // substring to search32 [](char c1, char c2) { // compar. criterion33 return myToupper(c1) == myToupper(c2);34 });35 if (pos != s.end()) {36 cout << "\"" << sub << "\" is part of \"" << s << "\""37 << endl;38 }39 }
View Code

Lambda调用成员函数

1 /* The following code example is taken from the book 2 * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 * 5 * (C) Copyright Nicolai M. Josuttis 2012. 6 * Permission to copy, use, modify, sell and distribute this software 7 * is granted provided this copyright notice appears in all copies. 8 * This software is provided "as is" without express or implied 9 * warranty, and with no claim as to its suitability for any purpose.10 */11 #include 
12 #include
13 #include
14 #include
15 #include
16 using namespace std;17 using namespace std::placeholders;18 19 class Person {20 private:21 string name;22 public:23 Person(const string& n) : name(n) {24 }25 void print() const {26 cout << name << endl;27 }28 void print2(const string& prefix) const {29 cout << prefix << name << endl;30 }31 //...32 };33 34 int main()35 {36 vector
coll37 = { Person("Tick"), Person("Trick"), Person("Track") };38 39 // call member function print() for each person40 for_each(coll.begin(), coll.end(),41 [](const Person& p) {42 p.print();43 });44 cout << endl;45 46 // call member function print2() with additional argument for each person47 for_each(coll.begin(), coll.end(),48 [](const Person& p) {49 p.print2("Person: ");50 });51 }
View Code

 

转载于:https://www.cnblogs.com/larry-xia/p/9496343.html

你可能感兴趣的文章
发布开源库到JCenter所遇到的一些问题记录
查看>>
第七周作业
查看>>
函数式编程与参数
查看>>
flush caches
查看>>
SSAS使用MDX生成脱机的多维数据集CUB文件
查看>>
ACM_hdu1102最小生成树练习
查看>>
MyBatis源码分析(一)--SqlSessionFactory的生成
查看>>
android中ListView点击和里边按钮或ImageView点击不能同时生效问题解决
查看>>
CTF常用工具之汇总
查看>>
java的面向对象 (2013-09-30-163写的日志迁移
查看>>
HDU 2191 【多重背包】
查看>>
51nod 1433 0和5【数论/九余定理】
查看>>
【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】
查看>>
less 分页显示文件内容
查看>>
如何对数据按某列进行分层处理
查看>>
[Qt] this application failed to start because it could not find or load the Qt platform plugin
查看>>
Git Submodule管理项目子模块
查看>>
学会和同事相处的30原则
查看>>
NOJ——1568走走走走走啊走(超级入门DP)
查看>>
文件操作
查看>>