对于STL中的依靠比较排序的容器,均提供了一个模板参数来传递比较函数,默认的为std::less<>。

查阅Containers - C++ Reference可以看到典型的使用比较函数的容器有

template <class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> class set;
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;

分别是优先队列、集合、映射,当然multiset和multimap也一样。

这里以优先队列为例,分别给出三种传递方式,将比较函数从默认的less<>(升序)改成降序。

这里先看看优先队列的构造函数源码

protected:
_Container c; // the underlying container
_Pr comp; // the comparator functor
    priority_queue(const _Myt& _Right)
: c(_Right.c), comp(_Right.comp)
{ // construct by copying _Right
} explicit priority_queue(const _Pr& _Pred)
: c(), comp(_Pred)
{ // construct with empty container, specified comparator
}

1、函数指针

bool greaterInt(const int& lhs, const int& rhs)
{
return lhs > rhs;
}
priority_queue<int, vector<int>, bool(*)(const int&, const int&)> q(greaterInt);

典型C风格写法,有种调用C库的qsort的感觉,代码有点长,虽然可以在前面加一句typedef bool(*Compare)(const int&, const int&)来缩短单行代码,但代码总共还是很长

2、函数对象

template <typename T>
struct Greater
{
bool operator()(const T& lhs, const T& rhs) const
{
return lhs > rhs;
}
};
priority_queue<int, vector<int>, Greater<int>> q;

注意,这里的q是采取默认构造。回顾之前的构造函数,字段comp在默认构造函数是直接用默认构造的,所以这里可以不写参数,而对于函数指针则不同,函数指针不是类,没有构造函数,所以必须添上参数。

那么,如果采用显式写法呢?

priority_queue<int, vector<int>, Greater<int>> q(Greater<int>());

编译器会提出警告

 warning C4930: 'std::priority_queue<int,std::vector<int,std::allocator<_Ty>>,Greater<int>> q(Greater<int> (__cdecl *)(void))': 
prototyped function not called (was a variable definition intended?)
> with
> [
> _Ty=int
> ]

这句代码不再是定义一个优先队列,而是声明一个函数指针,类似下面这种

Type q(Greater<int>());

函数重载是个语法糖,重载括号操作符的Greater<int>()(const int&, const int&)就像定义某个函数Greater_Int_XXX(const int&, const int&),Greater<int>()自然就对应了Greater_Int_XXX。

如果继续测试下去(比如q.push(1);)可以发现编译无法通过,提示left of '.push' must have class/struct/union,证明了q在这里不是容器对象,而被当成了函数指针。

所以没必要画蛇添足,直接用默认构造就行了。

3、lambda表达式

    auto comp = [](const int& lhs, const int& rhs) { return lhs > rhs; };
priority_queue<int, vector<int>, decltype(comp)> q(comp);

由于lambda表达式类型要无法手写出来,所以C++ 11提供了decltype关键字来取得类型。至于decltype的用法,本文不多做说明,网上资料很多。

比较

使用函数指针是最快的,因为无需构造对象或者lambda表达式,但是通用性不强,函数对象使用模板类只需改变模板参数就能适应不同类型,对特殊类型还可以做模板特化。而lambda表达式写起来比函数对象更为方便,适用于无需重复利用的比较函数。不过像==、!=、<、>、<=、>=等比较运算的函数对象都已经有现成的(见<functional> - C++ reference中的equal_to、not_equal_to、less、greater、less_equal、greater_equal),似乎lambda表达式一般用不着?

啊,说到这里,要绑定这三种不同的比较函数形式,用通用的function模板即可

  typedef std::function<bool(const int& lhs, const int& rhs)> Compare;
typedef std::priority_queue<int, std::vector<int>, Compare> MyPriorQueue;
// 1. 函数指针
MyPriorQueue q1(greaterInt);
// 2. 函数对象
Greater<int> compFunctor;
MyPriorQueue q2(compFunctor);
// 3. lambda表达式
auto compLambda = [](const int& lhs, const int& rhs) { return lhs > rhs; };
MyPriorQueue q3(compLambda); vector<pair<string, MyPriorQueue>> my_prior_queues = {
make_pair("函数指针", q1),
make_pair("函数对象", q2),
make_pair("lambda表达式", q3)
};
for (auto item : my_prior_queues)
{
auto& method = item.first;
cout << method << "\t";
auto& q = item.second;
q.push(1);
q.push(3);
q.push(2);
q.push(4);
q.push(0);
while (!q.empty())
{
cout << q.top() << " ";
q.pop();
}
cout << endl;
}

STL传递比较函数进容器的三种方式的更多相关文章

  1. 创建 Spring容器的三种方式

    一.src路径下打包完在war包的classes层级下 1.Spring容器创建的三种方式 创建Bean容器之后创建对象: 其中第三种使用的是BeanFactory对象 2.spring通过配置文件用 ...

  2. Docker容器互访三种方式

    我们都知道docker容器之间是互相隔离的,不能互相访问,但如果有些依赖关系的服务要怎么办呢.下面介绍三种方法解决容器互访问题. 方式一.虚拟ip访问 安装docker时,docker会默认创建一个内 ...

  3. 【Spring】Spring之向 IOC 容器注入对象的三种方式

    关于Spring的搭建可参见:浅析Spring框架的搭建.在测试之前还是应该先将环境配置好,将相关Jar包导进来.Spring创建的对象,默认情况下都是单例模式,除非通过scope指定. 向IOC容器 ...

  4. 容器间通信的三种方式 - 每天5分钟玩转 Docker 容器技术(35)

    容器之间可通过 IP,Docker DNS Server 或 joined 容器三种方式通信. IP 通信 从上一节的例子可以得出这样一个结论:两个容器要能通信,必须要有属于同一个网络的网卡. 满足这 ...

  5. 三种方式创建bean对象在springIOC容器中初始化、销毁阶段要调用的自定义方法

    1. 使用@Bean注解定义initMethod和destroyMethod 所谓initMethod和destroyMethod,是指在springIOC容器中,对于bean对象执行到初始化阶段和销 ...

  6. struts2 action 页面与action参数的传递的三种方式

    第一种: 初始页面: <form action="LoginAction.action" method="post"> 用户名:<input ...

  7. 菜鸟学习Spring——SpringIoC容器基于三种配置的对比

    一.概述 对于实现Bean信息定义的目标,它提供了基于XML.基于注解及基于java类这三种选项.下面总结一下三种配置方式的差异. 二.Bean不同配置方式比较. 三.Bean不同配置方式的适用场合. ...

  8. Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式

    Linux就这个范儿 第15章 七种武器  linux 同步IO: sync.fsync与fdatasync   Linux中的内存大页面huge page/large page  David Cut ...

  9. 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】

    一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...

随机推荐

  1. 11g OCM自动打补丁

    1.先替换掉OPatch软件 每个实例都要运行 GRID_HOME和ORACLE_HOME的OPatch目录都去除掉 把OPatch软件p6880880_112000_Linux-x86-64.zip ...

  2. OC-NSString

    ========================== 面向对象编程进阶和字符串 ========================== Δ一.类的设计模式—单例 [单例]程序允许过程中,有且仅有一块内存 ...

  3. L167

  4. L158

    It started out modestly enough: David Hertz, having learned that under the right conditions you real ...

  5. L147 Low Cost Study Has High Impact Results For Premature Babies

    No one knows exactly why some babies are born prematurely(早产), but some of the smallest premature ba ...

  6. c# 实体处理工具类

    using System; using System.Collections; using System.Collections.Generic; using System.ComponentMode ...

  7. socket函数sendto与send的区别

    C:socket相关的sendto()函数简介 http://blog.csdn.net/flytiger_ouc/article/details/19634279 文中提到SOCK_DGRAM, S ...

  8. iOS:Core Data 中的简单ORM

    我们首先在xcdatamodel文件中设计我们的数据库:例如我建立一个Data的实体,里面有一个String类型的属性name以及一个Integer类型的num: 然后选中Data,添加文件,选择NS ...

  9. CXF生成本地ws调用代码测试webservice

    package com.free.webservice.client; import java.util.List; import cn.com.webxml.*; public class Weat ...

  10. Redis学习总结之二——Redis配置文件(Windows版)

    # Redis configuration file example # Note on units: when memory size is needed, it is possible to sp ...