IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 【C++标准库】10.STL函数对象(仿函数)及Lambda -> 正文阅读

[C++知识库]【C++标准库】10.STL函数对象(仿函数)及Lambda

0.大纲

注意:函数对象和仿函数指的是一个东西.

1.Function Object(函数对象-仿函数)的概念

所谓function object(或者说functor),是一个定义了operator()的类的一个对象.
FunctionObjectType fo;
...
fo(...);?

!!!!注意:表达式fo()是调用函数对象fo的operator(),而非调用函数fo().?

函数对象的一般写法:

class FunctionObjectType 
{
   public:
      void operator() () 
      {
        statements
      }
};

1.1.1 仿函数相对于一般函数的优点

仿函数的优点:
1.仿函数是对象,可以拥有成员函数和成员变量,即仿函数拥有状态;
2.每个仿函数都有自己的类型;
3.仿函数通常比一般函数快(很多信息编译期确定).

1.1.2 以仿函数为排序准则?


#include <iostream>
#include <string>
#include <deque>
#include <set>
#include <algorithm>
using namespace std;

/*Person类*/
class Person
{
private:
    string fn; // first name
    string ln; // last name
public:
    Person() {}
    Person(const string &f, const string &n) : fn(f), ln(n) {}
    string firstname() const;
    string lastname() const;
};

inline string Person::firstname() const { return fn; }
inline string Person::lastname() const  { return ln; }

ostream &operator<<(ostream &s, const Person &p)
{
    s << "[" << p.firstname() << " " << p.lastname() << "]";
    return s;
}

/* class for function predicate
 * - operator () returns whether a person is less than another person
 */
class PersonSortCriterion
{
public:
    bool operator()(const Person &p1, const Person &p2) const
    {
        /* a person is less than another person
         * - if the last name is less
         * - if the last name is equal and the first name is less
         */
        return p1.lastname() < p2.lastname() || 
               ( p1.lastname() == p2.lastname() 
                 && p1.firstname() < p2.firstname());
    }
};

int main()
{
    Person p1("nicolai", "josuttis");
    Person p2("ulli", "josuttis");
    Person p3("anica", "josuttis");
    Person p4("lucas", "josuttis");
    Person p5("lucas", "otto");
    Person p6("lucas", "arm");
    Person p7("anica", "holle");

    // declare set type with special sorting criterion
    typedef set<Person, PersonSortCriterion> PersonSet;

    // create such a collection
    PersonSet coll;
    coll.insert(p1);
    coll.insert(p2);
    coll.insert(p3);
    coll.insert(p4);
    coll.insert(p5);
    coll.insert(p6);
    coll.insert(p7);

    // do something with the elements
    // - in this case: output them
    cout << "set:" << endl;
    PersonSet::iterator pos;
    for (pos = coll.begin(); pos != coll.end(); ++pos)
    {
        cout << *pos << endl;
    }
}

/*
注释:
这里的set coll使用特殊排序准则 PersonSortCriterion,而它正是一个function object
class. PersonSortCriterion这么定义operator():先比较两人的姓,如果相等再比较其名.
coll构造函数会自动产生class PersonSortCriterion的一个实例(instance),所有元素都
将以此为排序准则进行排序.

注意,排序准则PersonSortCriterion是个class,所以你可以把它当作set的template实参.
如果以寻常函数担任排序准则,就无法做到这一点.

"以上述类型作为排序准则"的所有set,都拥有属于自己的独一无二的类型. 无法将这个set拿
来和"拥有不同排序准则"的其他set合并或赋值. 虽然无论做什么都无法回避set的自动排序特
性,但可以设计出"表现不同的排序准则却有着相同类型"的仿函数.

*/

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-04 07:24:57  更:2022-05-04 07:25:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 22:22:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码