STL之函数对象(function object)

FunctionObject_函数对象 立即下载


1、概论

如果类定义了调用运算符(即圆括号),则该类的对象称作函数对象(function object)。因为可以调用这种对象,所以我们说这些对象的"行为像函数一样"。

函数对象常常作为泛型算法的实参,并代替函数指针使用。以sort()为例,sort(begin,end)接受两个迭代器作为其参数,默认利用元素类型的<运算符来实现排序。sort(begin,end,comp)则以用户定义的comp函数作为元素比较的根据。通常情况下,可以设计自己的比较函数comp,然后以函数指针当做算法的一个参数。但是函数指针不能满足STL对抽象性的要求,同时函数指针无法和STL其他组件搭配,产生更灵活的变化,因此需要使用函数对象。

STL的函数对象,根据操作数的个数划分,可分为一元和二元函数对象。根据功能划分,可分为算术运算、关系运算、逻辑运算三大类,每个类分别定义了一个执行命名操作的调用运算符。

在这里插入图片描述

2、可适配(adaptable)的关键

STL的函数对象可以使算法更灵活,而更加灵活的关键,在于STL的函数对象的可适配性(adaptability)。STL的函数对象应该有能力被函数适配器修饰,彼此像积木一样地串接。为了拥有适配能力,每一个函数对象必须定义自己的相应类型,就像迭代器如果要融入STL,也必须依照规定定义自己的5个相应类型一样,这些相应类型是为了让适配器能够取出,获得函数对象的某些信息。

函数对象的相应类型主要用来表现函数参数类型和返回值类型。标准库在<stl_function.h>中定义了两个类,分别代表一元函数对象和二元函数对象,其中没有任何数据成员或成员函数,只有一些类型定义。

template <typename _Arg, typename _Result>
struct unary_function
{
  /// @c argument_type is the type of the argument
  typedef _Arg argument_type;

  /// @c result_type is the return type
  typedef _Result result_type;
};

template <typename _Arg1, typename _Arg2, typename _Result>
struct binary_function
{
  /// @c first_argument_type is the type of the first argument
  typedef _Arg1 first_argument_type;

  /// @c second_argument_type is the type of the second argument
  typedef _Arg2 second_argument_type;

  /// @c result_type is the return type
  typedef _Result result_type;
};

3、算术类函数对象

算术类函数对象包括:plus<Type>、minus<Type>、multiplies<Type>、divides<Type>、modulus<Type>、negate<Type>。

template <typename _Tp>
struct plus : public binary_function<_Tp, _Tp, _Tp>
{
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x + __y;
  }
};

template <typename _Tp>
struct minus : public binary_function<_Tp, _Tp, _Tp>
{
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x - __y;
  }
};

template <typename _Tp>
struct multiplies : public binary_function<_Tp, _Tp, _Tp>
{
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x * __y;
  }
};

template <typename _Tp>
struct divides : public binary_function<_Tp, _Tp, _Tp>
{
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x / __y;
  }
};

template <typename _Tp>
struct modulus : public binary_function<_Tp, _Tp, _Tp>
{
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x % __y;
  }
};

template <typename _Tp>
struct negate : public unary_function<_Tp, _Tp>
{
  _GLIBCXX14_CONSTEXPR
  _Tp operator()(const _Tp &__x) const
  {
    return -__x;
  }
};

3.1、示例

#include <iostream>
#include <functional>
using namespace std;

int main()
{
    plus<int> intplus;
    minus<int> intminus;
    multiplies<int> intmultiplies;
    divides<int> intdivides;
    modulus<int> intmodulus;
    negate<int> intnegate;

    cout<<intplus(1,2)<<endl;  //3
    cout<<intminus(1,2)<<endl;  //-1
    cout<<intmultiplies(1,2)<<endl;  //2
    cout<<intdivides(1,2)<<endl;  //0
    cout<<intmodulus(1,2)<<endl;  //1
    cout<<intnegate(1)<<endl;  //-1
    return 0;
}

4、关系类函数对象

关系类函数对象包括:equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>

template <typename _Tp>
struct equal_to : public binary_function<_Tp, _Tp, bool>
{
  _GLIBCXX14_CONSTEXPR
  bool
  operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x == __y;
  }
};

template <typename _Tp>
struct not_equal_to : public binary_function<_Tp, _Tp, bool>
{
  _GLIBCXX14_CONSTEXPR
  bool
  operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x != __y;
  }
};

template <typename _Tp>
struct greater : public binary_function<_Tp, _Tp, bool>
{
  _GLIBCXX14_CONSTEXPR
  bool
  operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x > __y;
  }
};

template <typename _Tp>
struct less : public binary_function<_Tp, _Tp, bool>
{
  _GLIBCXX14_CONSTEXPR
  bool
  operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x < __y;
  }
};

template <typename _Tp>
struct greater_equal : public binary_function<_Tp, _Tp, bool>
{
  _GLIBCXX14_CONSTEXPR
  bool
  operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x >= __y;
  }
};

template <typename _Tp>
struct less_equal : public binary_function<_Tp, _Tp, bool>
{
  _GLIBCXX14_CONSTEXPR
  bool
  operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x <= __y;
  }
};

4.1、示例

#include <iostream>
#include <functional>
using namespace std;

int main()
{
    equal_to<int> intequal_to;
    not_equal_to<int> intnotequal_to;
    greater<int> intgreater;
    greater_equal<int> intgreater_equal;
    less<int> intless;
    less_equal<int> intless_equal;

    cout<<intequal_to(1,2)<<endl;  //0
    cout<<intnotequal_to(1,2)<<endl;  //1
    cout<<intgreater(1,2)<<endl;  //0
    cout<<intgreater_equal(1,2)<<endl;  //0
    cout<<intless(1,2)<<endl;  //1
    cout<<intless_equal(1,2)<<endl;  //1
    return 0;
}

5、逻辑函数对象

逻辑函数对象包括:logical_and<Type>、logical_or<Type>、logical_not<Type>

template <typename _Tp>
struct logical_and : public binary_function<_Tp, _Tp, bool>
{
  _GLIBCXX14_CONSTEXPR
  bool
  operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x && __y;
  }
};

template <typename _Tp>
struct logical_or : public binary_function<_Tp, _Tp, bool>
{
  _GLIBCXX14_CONSTEXPR
  bool
  operator()(const _Tp &__x, const _Tp &__y) const
  {
    return __x || __y;
  }
};

template <typename _Tp>
struct logical_not : public unary_function<_Tp, bool>
{
  _GLIBCXX14_CONSTEXPR
  bool
  operator()(const _Tp &__x) const
  {
    return !__x;
  }
};

5.1、示例

#include <iostream>
#include <functional>
using namespace std;

int main()
{
    logical_and<int> int_and;
    logical_or<int> int_or;
    logical_not<int> int_not;

    cout<<int_and(1,0)<<endl;  //0
    cout<<int_or(1,0)<<endl;  //1
    cout<<int_not(1)<<endl;  //0

    return 0;
}

6、其他

template <typename _Tp>
struct _Identity
    : public unary_function<_Tp, _Tp>
{
  _Tp &
  operator()(_Tp &__x) const
  {
    return __x;
  }

  const _Tp &
  operator()(const _Tp &__x) const
  {
    return __x;
  }
};

// Partial specialization, avoids confusing errors in e.g. std::set<const T>.
template <typename _Tp>
struct _Identity<const _Tp> : _Identity<_Tp>
{
};

template <typename _Pair>
struct _Select1st
    : public unary_function<_Pair, typename _Pair::first_type>
{
  typename _Pair::first_type &
  operator()(_Pair &__x) const
  {
    return __x.first;
  }

  const typename _Pair::first_type &
  operator()(const _Pair &__x) const
  {
    return __x.first;
  }

#if __cplusplus >= 201103L
  template <typename _Pair2>
  typename _Pair2::first_type &
  operator()(_Pair2 &__x) const
  {
    return __x.first;
  }

  template <typename _Pair2>
  const typename _Pair2::first_type &
  operator()(const _Pair2 &__x) const
  {
    return __x.first;
  }
#endif
};

template <typename _Pair>
struct _Select2nd
    : public unary_function<_Pair, typename _Pair::second_type>
{
  typename _Pair::second_type &
  operator()(_Pair &__x) const
  {
    return __x.second;
  }

  const typename _Pair::second_type &
  operator()(const _Pair &__x) const
  {
    return __x.second;
  }
};
【全解析 | PTA】浙大版《Python 程序设计》题目集-第一章 Python浙大版PTA题解 阅读详情

相关推荐

手把手教你用Simulink搭建交流异步电机矢量控制模型(附完整仿真文件)

本文提供了一份详细的Simulink仿真指南,手把手教你从零开始搭建交流异步电机矢量控制模型。内容涵盖主电路搭建、坐标变换实现、双闭环PI调节器设计、转子磁链观测器调试等核心步骤,并附有完整的仿真文件,帮助读者跨越理论到实践的鸿沟,掌握高性能电机控制系统的建模与仿真技能。

pz890123的博客 665

函数对象Function Object

Essential C++ -- 函数对象Function Object的原理,使用函数对象的意义,实现一个函数对象Function Object完成容器元素的过滤,find()和find_if()的区别

GodFishhh的博客 986

陆地观测卫星数据服务平台深度体验:高分、中巴、资源系列影像的检索与下载技巧

本文深度解析国产陆地观测卫星数据服务平台的使用技巧,重点介绍高分、中巴、资源系列卫星影像的检索与下载方法。通过权限体系解析、高级检索策略、数据预处理等实战技巧,帮助用户高效获取高质量遥感数据,适用于环境监测、资源调查等多种应用场景。

eee77的博客 980

C++ STL:function包装函数详解

语法:function包装函数,包装普通函数,lambda表达式,仿函数,成员函数

ylh的博客 2413

别再死磕MuJoCo了!用PyBullet+Stable-Baselines3搞定机械臂强化学习(保姆级环境配置)

本文详细介绍了如何使用PyBullet和Stable-Baselines3构建机械臂强化学习系统,绕过昂贵的MuJoCo许可壁垒。通过实战指南,包括环境配置、机械臂Gym环境构建、训练策略与调优技巧,帮助开发者快速掌握机器人强化学习技术,提升智能控制效率。

weixin_42667269的博客 264

深入浅出DNS系列(一)- DNS杂谈

黄丝玛:班达山,你好,我记性不好,你告诉我搜狐、新浪的网站IP地址好吗? 班达山:你问问DNS先生吧,他专门干这买卖的

jiangsd198的博客 4129

C++基础知识(十九)--- 函数对象

STL 中有以下实现“累加”功能的算法(函数模板),该模板的功能是对(first,last)中的每个迭代器 I 执行 val = op(val, *I),返回最终的 val。

woshizuopie的博客 1459

OpenHarmony系统参数权限配置避坑指南:从param get/set失败到SELinux标签实战

本文深入解析OpenHarmony系统参数权限配置的常见问题与解决方案,涵盖DAC和SELinux双重权限控制机制。通过实战案例演示如何配置`param get/set`权限,解决'Permission denied'错误,并提供三方应用访问参数的安全方案,帮助开发者高效完成系统调试与优化。

weixin_27013291的博客 449

function_object

函数对象 function object 函数对象是一个广泛的概念,因为所有具有函数行为的对象都可以称为函数对象。这是一个高级抽象,我们不关心对象到底是什么,只要其具有函数行为。所谓的函数行为是指的是可以使用()调用并传递参数: function(arg1, arg2, ...); //函数调用 这种函数对象实际上是一个类的实例,只不过这个类实现了函数调用符(): class X{ public...

UnknownPlayer的博客 822

ENVI5.6 自动化流程:GF系列卫星影像批处理与质量优化

本文详细介绍了如何利用ENVI5.6构建针对GF-2、GF-6、GF-7系列卫星影像的自动化批处理流水线。通过配置Batch Processing工具,实现从正射校正到图像融合的全流程批量处理,并分享参数设置、质量监控与故障排查的实战经验,旨在显著提升海量遥感影像的处理效率与成果质量。

efc1234567的博客 603

零基础做AI漫剧:Kimi K2.5写本 + Flux生图 + Seedance 2.0动效的保姆级教程

很多零基础小伙伴想做AI漫剧,却被“写剧本、画分镜、做动效”三大难题劝退——不会写剧情、不会画插画、不会做动态,觉得门槛太高。其实不用慌!今天就给大家带来保姆级教程,全程用Kimi K2.5(写剧本)、Flux(生分镜图)、Seedance 2.0(做动效)三个工具,不用专业技能,不用手动绘画,零基础也能在1小时内做出1分钟完整AI漫剧,全程步骤拆解、示例齐全,跟着做就能出片!

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 3036

function object

什么是function object(函数对象)?          函数对象由一个类定义而成,该类重载(), 用于封装一个函数。这是c++非常迷人的一个特性。          我们看一下使用方法。          class function{          public:               function():x(0){              }

hhhbbb的专栏 649

PoE 的三大国际标准:PoE、PoE+、PoE++

很多人知道 PoE 能“一根网线,既供电又传数据”,但鲜少有人能说清楚:PoE 供电到底分几种标准?差别有多大?哪些设备要用 PoE,哪些必须上 PoE+ 或 PoE++?今天,我们就来一次彻底的“拆解”,把 PoE 的三大国际标准——讲得明明白白,顺便也帮你厘清网络工程中的常见“供电疑惑”。

网络技术联盟站 1549

C++ STL Function Object函数对象

概念 指定义了operator()的对象,如: FunctionObjectType fo; ... fo(...); 其中的fo();是调用了fo对象的operator(),而不是调用函数fo() 可以将函数对象视为一般函数,只不过是一种更复杂的撰写手段,比如: class FunctionObject { public: void operator() () { statements...

qq_44800780的博客 476

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C++ STL六大组件-5-Function object(函数对象) 

C++ STL六大组件-简析 ------------------------------------------ C++ STL六大组件-1-Container(容器) C++ STL六大组件-2-Adapter(适配器) C++ STL六大组件-3-Algorithm(算法) C++ STL六大组件-4-Iterator(迭代器) C++ STL六大组件-5-Function object(函数对象) C++ STL六大组件-6...

cyy2learn的博客 1609

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Day 29 C++ STL- 函数对象(Function Object)(仿函数)

我们使用函数对象实例 multiplyByTwo 进行函数调用,以及将函数对象 MultiplyBy(3) 作为参数传递给 std::transform() 算法,对容器 nums 中的元素进行乘法操作。通过使用函数对象 MyCompare(),我们重新对向量 v 进行排序,这次的输出结果为 50 40 30 20 10,与上一次排序的结果相反,符合我们设定的从大到小排序策略。首先,在 test01() 函数中,我们创建一个整型向量 v,并添加一些数字元素。函数对象(仿函数)是一个 重载了。

缺点灵气儿的笔记本 408

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

less 函数_C++ STL六大组件5Function object(函数对象)

C++ STL六大组件-5-Function object(函数对象)C++ STL Function object如果一个类将()运算符重载为成员函数,这个类就称为函数对象类,这个类的对象就是函数对象。函数对象是一个对象,但是使用的形式看起来像函数调用,实际上也执行了函数调用,因而得名。接下来看一个实际例子。#include using namespace std;class CAv...

weixin_39829236的博客 191

C++ 11 - STL - 函数对象(Function Object) (上)

1. 定义 在STL中,可以把函数传递给算法,也可以把函数对象传递给算法。 那么,什么是函数对象呢? 我们来看下它的声明: class X { public: // define function call operator return-value operator() (arguments) const; ... } 你可以这样调用:X fo; .....

weixin_34390105的博客 106

STL for_each 和函数对象(function object) 使用详解

STL for_each函数定义在头文件里面, 其实现很简单, 如下: [cpp] view plaincopy templateclass _InIt, class _Fn1>   _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)   {// perform function for each

Jack Wang 4754

power函数要包含头文件_C++ STL六大组件5Function object(函数对象)

C++ STL六大组件-5-Function object(函数对象)C++ STL Function object如果一个类将()运算符重载为成员函数,这个类就称为函数对象类,这个类的对象就是函数对象。函数对象是一个对象,但是使用的形式看起来像函数调用,实际上也执行了函数调用,因而得名。接下来看一个实际例子。#include using namespace std;class CAv...

weixin_39805539的博客 354

C++ 11 - STL - 函数对象(Function Object) (下)

1. 预定义函数对象 C++标准库内含许多预定义的函数对象,也就是内置的函数对象。 你可以充分利用他们,不必自己费心去写一些自己的函数对象。 要使用他们,你只要包含如下头文件 #include <functional> eg: set<int, less<int>> coll; // sort elements with < set<...

weixin_34123613的博客 274

C++ 11 - STL - 函数对象(Function Object) (中)

我们再来看一个复杂的例子 需求: 我们需要对集合内每个元素加上一个特定的值 代码如下: AddInt.h class AddInt { private: int theValue; // the value to add public: // constructor initializes the value to add AddInt(in...

weixin_34194702的博客 191

【STL十四】函数对象(function object)_仿函数(functor)——lambda表达式

lambda表达式也是特殊的函数对象。

junxuezheng的博客 888
上一篇: STL之关联容器
下一篇: STL之适配器(adapter)
Spring_24
博客等级 码龄12年 46粉丝 41原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Spring_24

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值