任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。
运算符函数对象
C++ 针对常用的算术和逻辑运算定义了很多函数对象:
旧式绑定器与适配器
早期提供功能支持的几个工具在 C++11 中弃用,并于 C++17 中移除(旧否定器于 C++17 中弃用并于 C++20 中移除):
基类
与适配器兼容的一元函数基类
std::unary_function
template <typename ArgumentType, typename ResultType> | (C++11 中弃用) (C++17 中移除) |
unary_function
是用于创建拥有一个参数的函数的基类。
unary_function
不定义 operator() ;它期待导出类定义此运算符。 unary_function
只提供二个类型—— argument_type
和 result_type
——为模板形参所定义。
一些标准库函数适配器,如 std::not1 ,要求它们适配的函数对象已定义某些类型; std::not1 要求要适配的函数对象拥有名为 argument_type
的类型。从 unary_function
导出函数对象是令它们与那些适配器兼容的简易方式。
unary_function
在 C++11 中被弃用。
成员类型
类型 | 定义 |
argument_type | ArgumentType |
result_type | ResultType |
调用示例
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include <iterator>struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell(const Cell &cell){x = cell.x;y = cell.y;}Cell &operator+(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator+=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator*=(int n){x *= n;y *= n;return *this;}Cell &operator++(){x += 1;y += 1;return *this;}friend Cell operator +(const Cell &cell1, const Cell &cell2){Cell cell = cell1;cell += cell2;return cell;}friend Cell operator *(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x * cell2.x, cell1.y * cell2.y};return cell;}friend Cell operator /(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x / cell2.x, cell1.y / cell2.y};return cell;}friend Cell operator %(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x % cell2.x, cell1.y % cell2.y};return cell;}friend bool operator ==(const Cell &cell1, const Cell &cell2){return cell1.x == cell2.x && cell1.y == cell2.y;}friend bool operator !=(const Cell &cell1, const Cell &cell2){return cell1.x != cell2.x && cell1.y != cell2.y;}friend bool operator <(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y < cell2.y;}else{return cell1.x < cell2.x;}}friend bool operator >(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y > cell2.y;}else{return cell1.x > cell2.x;}}friend bool operator &&(const Cell &cell1, const Cell &cell2){return cell1.x && cell2.x && cell1.y && cell2.y;}friend bool operator ||(const Cell &cell1, const Cell &cell2){return cell1.x || cell2.x || cell1.y || cell2.y;}friend bool operator !(const Cell &cell){return !(cell.x && cell.x);}friend Cell operator &(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x & cell2.x, cell1.y & cell2.y};return cell;}friend Cell operator |(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x | cell2.x, cell1.y | cell2.y};return cell;}friend Cell operator ^(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x ^ cell2.x, cell1.y ^ cell2.y};return cell;}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}struct less_than_1024 : std::unary_function<Cell, bool>
{bool operator()(const Cell &cell) const{return cell < Cell{1024, 1024};}
};int main()
{std::vector<Cell> vector;for (int index = 1024 - 3; index < 1024 + 3; ++index){vector.push_back(Cell{index, index});}std::copy(vector.begin(), vector.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "std::not1(less_than_1024()): ";std::cout << std::count_if(vector.begin(), vector.end(), std::not1(less_than_1024())) << std::endl;/* C++11 解法:// 用某方法转型到 std::function<bool (Cell)> ——即使以 lambdastd::cout << std::count_if(vector.begin(), vector.end(),std::not1(std::function<bool (Cell)>([](Cell cell){ return cell < Cell{1024, 1024}; })));*/return 0;
}
输出
{1021,1021} {1022,1022} {1023,1023} {1024,1024} {1025,1025} {1026,1026}
std::not1(less_than_1024()): 3