C++STL built-in function object

Built-in function object

4.3.1 Meaning of built-in function objects
Concept:
STL has some built-in function objects

classification:

Arithmetic functors
Relational functors
Logical functors

usage:

The usage of the objects generated by these functors is exactly the same as
that of general functions. To use built-in function objects, you need to introduce the header file #include

Arithmetic functor

Function description:
Realize four arithmetic operations.
Negate is a unary operation, and the others are binary operations.

Function prototype:

template<class T> T plus<T>       //加法仿函数
template<class T> T minus<T>       //减法仿函数
template<class T> T multiplies<T>      //乘法仿函数
template<class T> T divides<T>     //除法仿函数
template<class T> T modulus<T>      //取模仿函数
template<class T> T negate<T>       //取反仿函数

Code example:

#include<iostream>
#include<functional>
using namespace std;
//negate一元仿函数 取反仿函数
void test01()
{
    
    
       negate<int>n;
       cout << n(50) << endl;
}
//plus二元仿函数  加法
void test02()
{
    
    
       plus<int>p;
       cout << p(10, 20) << endl;
}
int main()
{
    
    
       test01();
       test02();
       system("pause");
       return 0;
}

Summary: When using the built-in functor object, you need to include the header file #include

Guess you like

Origin blog.csdn.net/gyqailxj/article/details/115077073