【C++标准库】STL函数对象及Lambda

函数对象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 <iostream>
12 #include <string>
13 #include <deque>
14 #include <set>
15 #include <algorithm>
16 using namespace std;
17 
18 
19 /* class Person
20 */
21 class Person {
22 private:
23     string fn;    // first name
24     string ln;    // last name
25 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 predicate
52 * - operator () returns whether a person is less than another person
53 */
54 class PersonSortCriterion {
55 public:
56     bool operator() (const Person& p1, const Person& p2) const {
57         /* a person is less than another person
58         * - if the last name is less
59         * - if the last name is equal and the first name is less
60         */
61         return p1.lastname()<p2.lastname() ||
62             (p1.lastname() == p2.lastname() &&
63                 p1.firstname()<p2.firstname());
64     }
65 };
66 
67 
68 int main()
69 {
70     Person p1("nicolai", "josuttis");
71     Person p2("ulli", "josuttis");
72     Person p3("anica", "josuttis");
73     Person p4("lucas", "josuttis");
74     Person p5("lucas", "otto");
75     Person p6("lucas", "arm");
76     Person p7("anica", "holle");
77 
78     // declare set type with special sorting criterion
79     typedef set<Person, PersonSortCriterion> PersonSet;
80 
81     // create such a collection
82     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 elements
92     // - in this case: output them
93     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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class MeanValue
{
private:
    long num; //num of elements
    long sum; //sum of values
public:
    MeanValue():num(0),sum(0)
    {
    }
    void operator() (int elem)
    {
        ++num;
        sum += elem;
    }
    double value()
    {
        return static_cast<double>(sum) / static_cast<double>(num);
    }
};

int main()
{
    vector<int> 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

猜你喜欢

转载自www.cnblogs.com/larry-xia/p/9496343.html
今日推荐