概念:
本质:
函数对象(仿函数)是一个类,不是一个函数。
特点:
概念:
operator() 接受一个参数,那么叫做一元谓词。operator() 接受两个参数,那么叫做二元谓词。概念:
分类:
用法:
#include <functional>。功能描述:
negate 是一元运算,其他都是二元运算。仿函数原型:
| 原型 | 说明 |
|---|---|
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> | 取反仿函数 |
示例:
#include <iostream> #include <functional> // 必须包含头文件 void test01() { // negate 一元仿函数 取反 std::negate<int> n; std::cout << n(50) << std::endl; // 输出 -50 // plus 二元仿函数 加法 std::plus<int> p; std::cout << p(10, 20) << std::endl; // 输出 30 } int main() { test01(); return 0; }