一元函数 unary_function
.有返回值.
.只有一个参数.
template <class Arg, class Result>
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
}; unary_function可以作为一个一元函数对象的基类,它只定义了参数和返回值的类型,本身并不重载()操作符,这个任务应该交由派生类去完成。 二元函数 binary_function
.有返回值.
.两个参数. binary_function可以作为一个二元函数对象的基类,它只定义了参数和返回值的类型,本身并不重载()操作符,这个任务应该交由派生类去完成。 template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
/* 仿函数 */
#include <iostream>
#include <map>
#include <vector>
#include <algorithm> //std::sort
#include <functional> //binary_function类和unary_function类 using namespace std; struct STComp:public binary_function<int,int,bool>
{
inline bool operator()(int x, int y)
{
return x > y;
}
}; struct STPrint :public unary_function<int, bool>
{
inline void operator()(int x)
{
cout << x << endl;
}
}; void test()
{
vector<int> v1 = { ,,,,,,, };
//仿函数的应用
sort(v1.begin(), v1.end(), STComp());
for_each(v1.begin(), v1.end(), STPrint());
} int main()
{
test();
getchar();
return ;
}
为什么函数对象的性能优于函数指针
在调用带有函数指针的函数时,编译器产生一个间接函数调用——通过指针调用。 大部分编译器不会试图去内联通过函数指针调用的函数
(甚至函数已经声明为inline而且这个优化看起来很直接)。但是函数对象的成员函数可以声明为内联方法

Sword STL仿函数示例的更多相关文章

  1. STL——仿函数(函数对象)

    一.仿函数(也叫函数对象)概观 仿函数的作用主要在哪里?从第6章可以看出,STL所提供的各种算法,往往有两个版本,其中一个版本表现出最常用(或最直观)的某种运算,第二个版本则表现出最泛化的演算流程,允 ...

  2. Sword STL之仿函数概念介绍

    --介绍 函数和类似函数的对象(仿函数)遍布STL.关联容器使用它们来使元素保持有序:find_if这样的算法使用它们来控制它们的行为: 如果缺少它们,那么比如for_each和transform这样 ...

  3. (六)STL仿函数functor

    1.仿函数为算法服务,特点是重载操作符() 2.一共分为3大类,包括算术类,逻辑运算类,相对关系(比较大小):都继承了binary_function 3.仿函数的一些调用示例,其中右边的仿函数没有继承 ...

  4. [GeekBand] STL 仿函数入门详解

    本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Primer 11 中文版(第五版) page 37 :网络资料: 叶卡同学的部落格  http://www.leavesite. ...

  5. STL仿函数functor

    一:仿函数functor介绍 尽管函数指针被广泛用于实现函数回调,但C++还提供了一个重要的实现回调函数的方法,那就是函数对象. functor,翻译成函数对象,伪函数,算符,是重载了“()”操作符的 ...

  6. Sword STL容器分类介绍

    标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器slist和rope.slist是一个单 ...

  7. stl仿函数和适配器

    所谓的适配器就是底层利用仿函数,然后修改仿函数的接口,达到自己的目的: 例如:template<class operation> class binder1st的适配器,其本质是一个类,它 ...

  8. STL 仿函数(函数对象)

    ##定义 仿函数(functor):一种具有函数性质的对象. 仿函数在C++中的新名称为函数对象(function object). 仿函数类对象像函数一样被调用,调用仿函数类对象时,实际调用的是仿函 ...

  9. Sword STL之map效率问题

    #include <iostream> #include <map> #include <vector> using namespace std; /* STL容器 ...

随机推荐

  1. shiro 自定义过滤器,拦截过期session的请求,并且以ajax形式返回

    自定义过滤器: public class CustomFormAuthenticationFilter extends FormAuthenticationFilter { @Override pro ...

  2. 菜鸟学Java(三)——JSTL标签之核心标签

    JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个实现 Web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断.数据管理格式化.XML 操作以及数 ...

  3. Adding support for distinct operation for table API on DataStream

    https://github.com/apache/flink/pull/6521/files/66c3bd5d52a5e4af1f83406035b95774e8b6f636#diff-680b30 ...

  4. himall微信支付

    支付目录:

  5. 每日英语:15 places to find inspiration

    If you’re a writer or artist, you understand the power of location when it comes to creativity and f ...

  6. grpc-golang实现账号and密码认证

    // I would recommend to use interceptors: // client grpc.Dial(target, grpc.WithPerRPCCredentials(&am ...

  7. maven生成jar,运行却提示没有“没有主清单属性”

    转自:http://www.jianshu.com/p/fd5bd8657852 修改pom.xml <project xmlns="http://maven.apache.org/P ...

  8. MyBatis 问题列表

    问题表现:The content of elements must consist of well-formed character data or markup 解决办法:1.配置的动态SQL语句里 ...

  9. Asp.Net采集网页方法大全(5种)

    /// <summary>方法一:比较推荐 /// 用HttpWebRequest取得网页源码 /// 对于带BOM的网页很有效,不管是什么编码都能正确识别 /// </summar ...

  10. kafka消费者如何才能从头开始消费某个topic的全量数据

    消费者要从头开始消费某个topic的全量数据,需要满足2个条件(spring-kafka): (1)使用一个全新的"group.id"(就是之前没有被任何消费者使用过); (2)指 ...