任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。
运算符函数对象
C++ 针对常用的算术和逻辑运算定义了很多函数对象:
旧式绑定器与适配器
早期提供功能支持的几个工具在 C++11 中弃用,并于 C++17 中移除(旧否定器于 C++17 中弃用并于 C++20 中移除):
函数适配器
适配器兼容的包装,用于包装一元函数的指针
std::pointer_to_unary_function
template< class Arg, | (C++11 中弃用) (C++17 中移除) |
std::pointer_to_unary_function
是表现为环绕一元函数的包装器的函数对象。
成员函数
(构造函数) | 以提供的函数构造 pointer_to_unary_function 对象(公开成员函数) |
operator() | 调用存储的函数 (公开成员函数) |
std::pointer_to_unary_function::pointer_to_unary_function
explicit pointer_to_unary_function( Result (*f)(Arg) ); |
构造 pointer_to_unary_function
函数对象,存储函数 f
。
参数
f | - | 要存储的指向函数指针 |
std::pointer_to_unary_function::operator()
Result operator()( Arg x ) const; |
调用存储的函数。
参数
x | - | 传递给函数的参数 |
返回值
被调用函数的返回值。
调用示例
#include <iostream>
#include <functional>
#include <algorithm>
#include <cmath>
int main()
{
std::pointer_to_unary_function<double, double> LogObject(std::log);
double numbers[] = {10.0, 20.0, 40.0, 80.0, 160.0};
double logs[5];
std::transform(numbers, numbers + 5, logs, LogObject);
for (int i = 0; i < 5; i++)
{
std::cout << logs[i] << " ";
}
std::cout << std::endl;
return 0;
}
输出
2.30259 2.99573 3.68888 4.38203 5.07517
调用示例
#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::pointer_to_unary_function<const Cell &, Cell> cellObject(&CellFunction);
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(), cellObject);
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::pointer_to_unary_function<const Cell &, Cell> cellObject2(CellFunction2);
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(), cellObject2);
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}