当前位置:网站首页>STL -- function object
STL -- function object
2022-06-12 16:52:00 【KevinJune】
Catalog
3.1 Built in function object meaning
1 Function object
1.1 Function object concept
Concept :
○ Overloaded function call operator class , Its object often becomes a function object
○ Function objects use overloaded () when , It behaves like a function call , It's also called a parafunction
The essence :
Function object ( functor ) Is a class , Not a function
1.2 Function objects use
characteristic :
○ Function objects are used , It can be called just like a normal function , There can be parameters , You can have a return value
○ Function objects go beyond the concept of ordinary functions , Function objects can have their own states
○ Function objects can be passed as arguments
#include<iostream>
using namespace std;
#include<string>
// Function object ( functor )
/*
○ Function objects are used , It can be called just like a normal function , There can be parameters , You can have a return value
○ Function objects go beyond the concept of ordinary functions , Function objects can have their own states
○ Function objects can be passed as arguments
*/
class MyAdd
{
public:
int operator()(int v1,int v2)
{
return v1 + v2;
}
};
//1、 Function objects are used , It can be called just like a normal function , There can be parameters , You can have a return value
void test01()
{
MyAdd myAdd;
cout << myAdd(10, 10) << endl;
}
//2、 Function objects go beyond the concept of ordinary functions , Function objects can have their own states
class MyPrint
{
public:
MyPrint()
{
this->count = 0;
}
void operator()(string test)
{
cout << test << endl;
this->count++;
}
int count; // Internal self state
};
void test02()
{
MyPrint myPrint;
myPrint("hello world");
myPrint("hello world");
myPrint("hello world");
myPrint("hello world");
myPrint("hello world");
cout << "the times of calling myPrint is : " << myPrint.count << endl;
}
//3、 Function objects can be passed as arguments
void doPrint(MyPrint & mp,string test)
{
mp(test);
}
void test03()
{
MyPrint MyPrint;
doPrint(MyPrint, "Hello c++");
}
int main(){
test03();
system("pause");
return 0;
}summary :
○ It's very flexible to write like functions , It can be passed as a parameter
2 The predicate
2.1 Predicate concept
Concept :
○ return bool A functor of type becomes a predicate
○ If operator() Take a parameter , So it's called unary predicate
○ If operator() Take two parameters , So it's called a binary predicate
2.2 Unary predicate
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
// functor The return value type is bool data type , Become predicate
// Unary predicate
class GreaterFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
// Find container Is there anything greater than 5 The number of
//GreaterFive() Anonymous function object
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if(it == v.end())
{
cout << "not found " << endl;
}
else
{
cout << "find the value is : " << *it << endl;
}
}
int main(){
test01();
system("pause");
return 0;
}summary :
Predicate with only one parameter , It's called unary predicate
2.3 two-place predicate
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
// two-place predicate
class MyCompare
{
public:
bool operator()(int val1,int val2)
{
return val1 > val2;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(20);
v.push_back(30);
v.push_back(50);
sort(v.begin(), v.end());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// Using function objects , Change the algorithm strategy , Change the sorting rule from large to small
sort(v.begin(), v.end(), MyCompare());
cout << "`````````" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main(){
test01();
system("pause");
return 0;
}summary : Predicate with only two arguments , Become a binary predicate
3 Built in function objects
3.1 Built in function object meaning
Concept :
○STL Built in some function objects
classification :
○ Arithmetic functor
○ Relational functor
○ Logic functor
usage :
○ The objects generated by these functors , The usage is exactly the same as the general function
○ Using built-in function objects , Need to introduce header file <functional>
3.2 Arithmetic functor
Function description :
○ Realize four operations
○ among negate It's a unary operation , Everything else is binary
The prototype of functor :
○template<class T> T plus<T>; // Additive affine function
○template<class T> T minus<T>; // Subtraction functor
○template<class T> T multiplies<T>; // Multiplication functor
○template<class T> T divides<T>; // Division functions
○template<class T> T modulus<T>; // Take the mimic function
○template<class T> T negate<T>; // Take the inverse affine function
#include<iostream>
using namespace std;
#include<functional>
// Built in function objects Arithmetic functor
//negate Unary affine function Take the inverse affine function
void test01()
{
negate<int> n;
cout << n(50) << endl;
}
//plus Bivariate affine function Add
void test02()
{
plus<int> p;
cout << p(10, 20) << endl;
}
int main(){
test01();
system("pause");
return 0;
}summary :
When using built-in function objects , Need to introduce header file <functional>
3.3 Relational functor
Function description :
○ Realize the relationship comparison
The prototype of functor :
○template<class T> bool equal_to<T> // be equal to
○template<class T> bool not_equal_to<T> // It's not equal to
○template<class T> bool greater<T> // Greater than
○template<class T> bool greater_equal<T> // Greater than or equal to
○template<class T> bool less<T> // Less than
○template<class T> bool less_equal<T> // Less than or equal to
#include<iostream>
using namespace std;
#include<functional>
#include<vector>
#include<algorithm>
// Built in function objects Relational functor
// Greater than greater
class MyCompare
{
public:
bool operator()(int val1,int val2)
{
return val1 > val2;
}
};
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(50);
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// Descending
// sort(v.begin(), v.end(), MyCompare());
//greater<int>() Built in function objects
sort(v.begin(), v.end(), greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main(){
test01();
system("pause");
return 0;
}summary :
The most common use of relational affine functions is greater<> Greater than
3.4 Logic functor
Function description :
○ Realizing logic operations
The function prototype :
○template<class T> bool logical_and<T> // Logic and
○template<class T> bool logical_or<T> // Logic or
○template<class T> bool logical_not<T> // Logic is not
#include<iostream>
using namespace std;
#include<functional>
#include<vector>
#include<algorithm>
// Built in function objects Logic functor
// Logic is not logical_not
void test01()
{
vector<bool> v;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
for (vector<bool>::iterator it = v.begin(); it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
// Using logical non The container v Carry to Containers v2 in , And perform the reverse operation
vector<bool> v2;
v2.resize(v.size());
transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
for (vector<bool>::iterator it = v2.begin(); it != v2.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main(){
test01();
system("pause");
return 0;
}summary :
Logic affine function is seldom used in practice , Understanding can
边栏推荐
- 有哪些特容易考上的院校?
- [MySQL] Cartesian product - multi table query (detailed explanation)
- Double write consistency problem
- canvas 处理图像(上)
- Overview of webrtc's audio network Countermeasures
- uabntu的sudo
- How to base on CCS_ V11 new tms320f28035 project
- 【研究】英文论文阅读——英语poor的研究人员的福利
- pbootcms的if判断失效直接显示标签怎么回事?
- SwinTransformer网络架构
猜你喜欢

IDEA在控制台显示出services,统一管理所有的jetty服务,

Demande de doctorat | xinchao Wang, Université nationale de Singapour

Leetcode 2194. Excel 錶中某個範圍內的單元格(可以,已解决)

MongoDB 学习整理(用户,数据库,集合,文档 的基础命令学习)

Joint recruitment notice of ganfei research group of Wuhan University and xuzhenjiang research group of Nanchang University

Cookies and sessions

2080虚拟机登录命令

How to base on CCS_ V11 new tms320f28035 project

WebRTC 的音频网络对抗概述

叶子分享站PHP源码下载
随机推荐
[DSP video tutorial] DSP video tutorial Issue 8: performance comparison of DSP library trigonometric function, C library trigonometric function and hardware trigonometric function, and accuracy compar
Project training of Shandong University rendering engine system (VI)
idea如何设置导包不带*号
Latex table online generation
Qt开发高级进阶:初探qt + opengl
Comprendre le go des modules go. MOD et go. SUM
PAT甲级 1142 最大团
generate pivot data 0
std::set compare
[raspberry pie]: (IV) camera advanced
JS monitors whether the user opens the screen focus
添加静态路由
The C programming language (version 2) notes / 8 UNIX system interface / 8.4 random access (lseek)
Project training of Shandong University rendering engine system (V)
VIM from dislike to dependence (16) -- macro
Project training of Shandong University rendering engine system (VII)
What is compound interest financial product?
Mongodb learning and sorting (basic command learning of users, databases, collections and documents)
MySQL statement
890. 查找和替换模式 / 剑指 Offer II 080. 含有 k 个元素的组合