c/c++ 重载运算符 函数调用运算符

重载运算符 函数调用运算符

把一个类的对象a,当成函数来使用,比如a(),所以需要重载operator()方法。重载了函数调用运算符的类的对象,就是函数对象了。

还有什么是函数对象呢???

  • lambda是函数对象
  • std::bind函数的返回值是函数对象
  • 函数是函数对象
  • 函数指针是函数对象

函数对象是做什么用的呢???

  • 在标准算法中使用,比如std::sort(b, e, 函数对象);

标准库提供了下面的函数对象,它们都是模板形式的,它们放在functional头文件中

算术关系逻辑
plus<Type>equal_to<Type>logical_and<Type>
minus<Type>not_equal_to<Type>logical_or<Type>
multiplies<Type>greater<Type>logical_not<Type>
divides<Type>greater_equal<Type>
modulus<Type>less<Type>
negate<Type>less_equal<Type>

例子:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>class LineStr{
public:LineStr(std::istream& in = std::cin) : is(in){}std::string operator()(){std::string str;std::getline(is, str);return is ? str : std::string();}
private:std::istream& is;
};class Isequ{
public:Isequ(int i = 0) : val(i){}bool operator()(int t){return val == t;}
private:int val;
};class StableSort{
public:bool operator()(const std::string& a, const std::string& b){return a.size() < b.size();}
};class SizeCmp{
public:SizeCmp(std::size_t s) : sz(s){}bool operator()(const std::string& str)const{return str.size() > sz;}
private:std::size_t sz;
};int main(){/*LineStr ls;std::cout << ls() << std::endl;*//*std::vector<int> vi{23,3,5,6,78,3};Isequ iq(3);std::replace_if(vi.begin(), vi.end(), iq, 9);for(int i : vi)std::cout << i << " ";std::cout << std::endl;*//*std::vector<std::string> vs{"1234", "123", "a", "bc"};//stable_sort(vs.begin(), vs.end(), [](const std::string& a,//                   const std::string& b){//        return a.size() < b.size();//          });//std::stable_sort(vs.begin(), vs.end(), StableSort());StableSort ss;std::stable_sort(vs.begin(), vs.end(), ss);std::size_t sz = 2;//auto b = std::find_if(vs.cbegin(), vs.cend(), [sz](const std::string& a){//    return a.size() > sz;//  });SizeCmp sc(3);auto b = std::find_if(vs.cbegin(), vs.cend(), sc);for_each(b, vs.cend(), [](const std::string& s){std::cout << s << " ";});std::cout << std::endl;for(auto s : vs)std::cout << s << " ";std::cout << std::endl;*/using std::placeholders::_1;std::vector<int> iv {12,213,123123,434344,213232};int cnt = std::count_if(iv.cbegin(), iv.cend(),std::bind(std::greater<int>(), _1, 1024));std::cout << cnt << std::endl;std::vector<std::string> sv{"pooh", "pooh", "11","pooh","22"};auto idx = std::find_if(sv.cbegin(), sv.cend(),std::bind(std::not_equal_to<std::string>(),_1, "pooh"));std::cout << *idx << std::endl;std::vector<int> iv2 {12,3,12,4,21};std::transform(iv2.cbegin(), iv2.cend(), iv2.begin(),std::bind(std::multiplies<int>(), _1, 2));for(auto i : iv2)std::cout << i << " ";std::cout << std::endl;}

github

c/c++ 学习互助QQ群:877684253

1414315-20181106214320230-961379709.jpg

本人微信:xiaoshitou5854

转载于:https://www.cnblogs.com/xiaoshiwang/p/10176824.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/395614.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

matlab 万能,matlab 万能实用的线性曲线拟合方法

在科学计算和工程应用中&#xff0c;经常会遇到需要拟合一系列的离散数据&#xff0c;最近找了很多相关的文章方法&#xff0c;在这里进行总结一下其中最完整、几乎能解决所有离散参数线性拟合的方法第一步&#xff1a;得到散点数据根据你的实际问题得到一系列的散点例如&#…

socket websocket

1.websocket客户端 websocket允许通过JavaScript建立与远程服务器的连接&#xff0c;从而实现客户端与服务器间双向的通信。在websocket中有两个方法&#xff1a;      1、send() 向远程服务器发送数据    2、close() 关闭该websocket链接  websocket同时还定义了几…

javascript原型_JavaScript的原型:古怪,但这是它的工作原理

javascript原型by Pranav Jindal通过普拉纳夫金达尔 JavaScript的原型&#xff1a;古怪&#xff0c;但这是它的工作原理 (Prototype in JavaScript: it’s quirky, but here’s how it works) The following four lines are enough to confuse most JavaScript developers:以下…

mysql函数之SUBSTRING_INDEX(str,/,-1)

SUBSTRING_INDEX的用法&#xff1a; •SUBSTRING_INDEX(str,delim,count) 在定界符 delim 以及count 出现前&#xff0c;从字符串str返回自字符串。若count为正值,则返回最终定界符(从左边开始) 若为-1则是从后往前截取 SELECT substring_index(Hn_P00001, P, -1) -- 结果是…

mysql8.0主从配置,MySQL 8.0主从服务器(Master-Slave)配置

一、介绍MySQL 主从复制的方式有多种&#xff0c;本文主要演示基于基于日志(binlog)的主从复制方式。MySQL 主从复制(也称 A/B 复制) 的原理&#xff1a;Master将数据改变记录到二进制日志(binary log)中&#xff0c;也就是配置文件log-bin指定的文件&#xff0c; 这些记录叫做…

第十二章 Shell脚本编写及常见面试题(三)

本章目录&#xff1a;12.21 FTP下载文件#!/bin/bash if [ $# -ne 1 ]; thenecho "Usage: $0 filename" fi dir$(dirname $1) file$(basename $1) ftp -n -v << EOF # -n 自动登录 open 192.168.1.10 user admin adminpass binary # 设置ftp传输模式为二进制…

亚马逊面试有几轮_经过几个月的Google面试准备,我被亚马逊录用

亚马逊面试有几轮by Googley as Heck由Googley饰演Heck 经过几个月的Google面试准备&#xff0c;我被亚马逊录用 (After months of preparing for the Google interview, I got hired by Amazon) As you may know, the last 11 months have been very difficult for me. As a …

省选前的考试记录

日拱一卒功不唐捐 什么沙雕玩意儿 2018.12.24 T1 如果对 \(A\) 数组求出来高度递减的单调栈的话&#xff0c;会发现只有单调栈里的元素是有用的。因为如果有 \(A[i]<A[j] \And i<j\)&#xff0c;那电梯就可以在带 \(j\) 上楼的时候顺便把 \(i\) 带上并不会影响结果。所以…

软件工程课设-----日程管理系统

这学期进行了软件工程课设&#xff0c;题目是&#xff1a;日程管理系统&#xff08;JavaWeb&#xff09;&#xff0c;为期3周。这三周只有前两天是企业老师讲解是企业老师讲解相关的基础知识(老师讲的水平实在是不可恭维。。。。。。)。 多的不多说。直接进行对相关项目的介绍。…

matlab中的神经网络训练,MATLAB中的神经网络训练

我试图向前馈送反向传播&#xff0c;但是在网络训练之后&#xff0c;当模拟和打印模拟输出时&#xff0c;我看不到任何靠近目标的值&#xff0c;但它只是一个数字。代码如下。什么是错&#xff0c;什么是问题&#xff1f;前馈反向传播&#xff1a;>> load(E:/Inputdata.t…

Spring For All 顶级Spring综合社区服务平台

Spring For All 玩最纯粹的技术&#xff01;做最专业的 Spring 民间组织~ 欢迎加入&#xff1a;http://spring4all.com/ image.png

chromium 桌面_如何使用Chromium和PyInstaller将Web应用程序转换为桌面应用程序

chromium 桌面Packaging and distributing your app sounds simple in principle. It’s just software. But in practice, it’s quite challenging.打包和分发应用程序在原理上听起来很简单。 这只是软件。 但是在实践中&#xff0c;这非常具有挑战性。 I’ve been working …

PHP面向对象(三)

一、继承概念 继承性也是面向对象程序设计中的重要特性之一。它是指建立一个新的派生类&#xff0c;从一个先前定义的类中继承数据和函数&#xff0c;而且可以重新定义新的数据类型和函数&#xff0c;从而建立累的层次或等级关系。 格式&#xff1a;     [修饰符] class 子…

python数据结构的应用场景不包括,Python 数据结构学习

Python 数据结构学习列表list.append(x)在列表的末尾添加一个元素。相当于 a[len(a):] [x] 。list.extend(iterable)使用可迭代对象中的所有元素来扩展列表。相当于 a[len(a):] iterable 。list.insert(i, x)在给定的位置插入一个元素。第一个参数是要插入的元素的索引&#…

[Jinkey 原创]震惊!iOS 系统居然自带悬浮窗口调试工具

原文链接 : 震惊&#xff01;iOS 系统居然自带悬浮窗口调试工具 —— Jinkey 原创原文作者 : Jinkey1 背景 英文原文&#xff1a;http://ryanipete.com/blog/ios/swift/objective-c/uidebugginginformationoverlay/ 我写得这个并不是翻译而是用自己的理解重新表述这个功能&…

盲人编程_盲人如何编码

盲人编程About one out of every 200 software developers is blind. We know this because Stack Overflow asked 64,000 developers about this a few months ago.每200名软件开发人员中大约有1名是盲人。 我们之所以知道这一点&#xff0c;是因为几个月前 Stack Overflow 向…

hadoop环境搭建笔记

一、配置Linux &#xff08;1&#xff09;cat /etc/networks &#xff08;2&#xff09;cat /etc/sysconfig/network &#xff08;3&#xff09;vi /etc/udev/rules.d/70-persistent-net.rules eth1 改为eth0 &#xff08;4&#xff09;vi /etc/sysconfig/network-scripts/ifc…

边分治讲解

前言&#xff1a; 边分治和点分治一样属于树分治的一部分&#xff0c;相比于点分治&#xff0c;边分治对于与度数相关的问题有着很大的优势&#xff0c;同时边分治也是解决树上最优化问题的一种重要的算法。 分治过程&#xff1a; 边分治的分治过程与点分治类似&#xff0c;同样…

准确性 敏感性 特异性_如何掌握类型特异性的艺术

准确性 敏感性 特异性Do more specific definitions result in less flexibility?更具体的定义会导致灵活性降低吗&#xff1f; In this post I will try to avoid the debate about strong/static vs. weak/dynamic types (what more could possibly be said?), or even sc…