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

把一个类的对象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

本人微信:xiaoshitou5854

c/c++ 重载运算符 函数调用运算符的更多相关文章

  1. C++ Pirmer : 第十四章 : 重载运算符与类型转换之函数调用运算符与标准库的定义的函数对象

    函数调用运算符 struct test { int operator()(int val) const { return (i > 0 ? i : -i); } }; 所谓的函数调用就是一个类重 ...

  2. C++赋值运算符、函数调用运算符、下标运算符(“=”、“()”、“[]”)重载

    #include <iostream>#include <assert.h>#include <string.h> using namespace std; cla ...

  3. [C++] 重载运算符与类型转换(2)——函数调用运算符和类型转换运算符

    1.这两个应该是C++中比较高级的用法了. 一.函数调用运算符   1.重载函数调用运算符(),必须是成员函数,一个类可以定义多个不同版本的调用运算符,相互之间应该在参数数量或者类型上有所区别.   ...

  4. C++ //函数调用运算符重载 (仿函数)

    1 //函数调用运算符重载 2 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 7 //函 ...

  5. 函数调用运算符"()"

    14.8函数调用运算符"()"1.函数调用运算符必须是成员函数,一个类可以定义多个不同版本的调用运算符,但是他们相互之间应该在参数数量或返回类型上有所区别.定义了调用运算符的类的对 ...

  6. C++重载流插入运算符和流提取运算符【转】

    C++的流插入运算符“<<”和流提取运算符“>>”是C++在类库中提供的,所有C++编译系统都在类库中提供输入流类istream和输出流类ostream.cin和cout分别是 ...

  7. c/c++ 重载运算符 类型转换运算符

    重载运算符 类型转换运算符 问题:能不能把一个类型A的对象a,转换成另一个类型B的对象b呢?? 是可以的.这就必须要用类型A的类型转换运算符(conversion operator) 下面的opera ...

  8. C++运算符重载——输入/输出运算符

    为了与IO标准库一致,重载输入输出运算符函数的第一个行参应该是流的引用,第二个行参是对象的引用. 如果重载为类的成员函数,第一个行参应该是对象的引用,第二个行参是流的引用. 使用方式是 ClassOb ...

  9. operator 重载内置运算符

    operator 关键字来重载内置运算符,或提供类或结构声明中的用户定义转换.它可以定义不同类型之间采用何种转化方式和转化的结果. operator用于定义类型转化时可采用2种方式,隐式转换(impl ...

随机推荐

  1. mybatis generator自动生成代码时 只生成了insert 而没有其他的

    mybatis框架提供了非常好用的逆向工程插件,但是在使用过程中会有很多问题. 我在使用中就遇到了只生成insert和insertSeletive方法,而不生成其他根据primary key查询更新删 ...

  2. Python - 命令式编程与符号编程

    原文链接:https://zh.d2l.ai/chapter_computational-performance/hybridize.html本文是对原文内容的摘取和扩展. 命令式编程(imperat ...

  3. MySQL5.7.25(解压版)Windows下详细的安装过程

    大家好,我是浅墨竹染,以下是MySQL5.7.25(解压版)Windows下详细的安装过程 1.首先下载MySQL 推荐去官网上下载MySQL,如果不想找,那么下面就是: Windows32位地址:点 ...

  4. CKEditor上传视频(java)

    CKEditor上传视频 CKEditor批量上传图片flvplayer.swf播放器CKEditor整合包(v4.6.1) ------------------------------------ ...

  5. 边缘计算 VS 云计算,谁才是未来?

    计算是互联网中一个永恒的话题,设备的所有运行都可以看成是 0 和 1 的运算.在计算中近些年有两个越来越响亮的技术:云计算和边缘计算.现如今是云计算方兴未艾,边缘计算已经有了燎原之势,本文将对这两种技 ...

  6. C# 8中的可空引用类型

    原文:Nullable Reference Types In C# 8 作者:.NET Core Tutorials 译者:Lamond Lu 现状 可空引用类型? 自从我开始学习.NET, 引用类型 ...

  7. WebSocket刨根问底(一)

    年初的时候,写过两篇博客介绍在Spring Boot中如何使用WebSocket发送消息[在Spring Boot框架下使用WebSocket实现消息推送][在Spring Boot框架下使用WebS ...

  8. Kafka Producer源码简述

    接着上文kafka的简述,这一章我们一探kafka生产者是如何发送消息到消息服务器的. 代码的入口还是从 kafkaTemplate.send开始 最终我们就会到 org.springframewor ...

  9. Chapter 5 Blood Type——21

    "Bella." Edward's voice was right beside me, relieved now. "Can you hear me?" “B ...

  10. leetcode — palindrome-partitioning-ii

    import java.util.Arrays; /** * * Source : https://oj.leetcode.com/problems/palindrome-partitioning-i ...