C++函数对象-运算符函数对象 - 旧式绑定器与适配器 - 从函数指针创建与适配器兼容的函数对象包装器(std::ptr_fun)
任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。
运算符函数对象
C++ 针对常用的算术和逻辑运算定义了很多函数对象:
旧式绑定器与适配器
早期提供功能支持的几个工具在 C++11 中弃用,并于 C++17 中移除(旧否定器于 C++17 中弃用并于 C++20 中移除):
函数适配器
从函数指针创建与适配器兼容的函数对象包装器
std::ptr_fun
| template< class Arg, class Result > std::pointer_to_unary_function<Arg,Result> ptr_fun( Result (*f)(Arg) ); | (1) | (C++11 中弃用) (C++17 中移除) |
| template< class Arg1, class Arg2, class Result > std::pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun( Result (*f)(Arg1, Arg2) ); | (2) | (C++11 中弃用) (C++17 中移除) |
创建函数包装器对象( std::pointer_to_unary_function 或 std::pointer_to_binary_function ),从模板实参推导目标类型。
1) 等效地调用 std::pointer_to_unary_function<Arg,Result>(f) 。
2) 等效地调用 std::pointer_to_binary_function<Arg1,Arg2,Result>(f) 。
此函数与关联类型从 C++11 起被弃用,因为使用更通用的 std::function 和 std::ref 更好,它们都可以从简单函数创建与适配器兼容的函数对象。
参数
| f | - | 为之创建包装的指向函数指针 |
返回值
包装 f 的函数对象。
异常
(无)
调用示例
#include <iostream>
#include <functional>
#include <algorithm>
#include <cmath>
#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;
}Cell CellFunction(const Cell &cell)
{Cell tCell = cell + cell;return tCell;
}int main()
{std::vector<Cell> vector1{{101, 101}, {102, 102}, {103, 103}, {104, 104}, {105, 105}};std::vector<Cell> result1(vector1.size());std::transform(vector1.begin(), vector1.end(), result1.begin(), std::ptr_fun(CellFunction));std::cout << "data: ";std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "result: ";std::copy(result1.begin(), result1.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;auto CellFunction2 = [](const Cell & cell){Cell tCell = cell * Cell{3, 3};return tCell;};std::vector<Cell> vector2{{101, 101}, {102, 102}, {103, 103}, {104, 104}, {105, 105}};std::vector<Cell> result2(vector1.size());std::transform(vector2.begin(), vector2.end(), result2.begin(), std::ptr_fun(*CellFunction2));std::cout << "data: ";std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;std::cout << "result: ";std::copy(result2.begin(), result2.end(), std::ostream_iterator<Cell>(std::cout, " "));std::cout << std::endl;return 0;
}
输出
data: {101,101} {102,102} {103,103} {104,104} {105,105}
result: {202,202} {204,204} {206,206} {208,208} {210,210}data: {101,101} {102,102} {103,103} {104,104} {105,105}
result: {303,303} {306,306} {309,309} {312,312} {315,315}