任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。
C++ 针对常用的算术和逻辑运算定义了很多函数对象:
std::negate
template< class T > |
(C++14 前) | |
template< class T = void > |
(C++14 起) |
进行取负的函数对象。等效地在 T
类型实例上调用 operator- 。
标准库提供
|
(C++14 起) |
成员类型
|
(C++20 前) |
operator() |
返回参数的相反数 (公开成员函数) |
T operator()( const T& arg ) const; |
(C++14 前) | |
constexpr T operator()( const T& arg ) const; |
(C++14 起) |
返回 arg
的相反数。
arg | - | 要计算相反数的值 |
-arg 的结果。
(无)
constexpr T operator()(const T &arg) const
{
return -arg;
}
#include
#include
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;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
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)
{
Cell cell = {-cell1.x, -cell1.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 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;
}
int main()
{
std::cout << "std::negate()(50): "
<< std::negate()(50) << std::endl;
std::cout << "std::negate()(1023): "
<< std::negate()(1023) << std::endl;
std::cout << "std::negate()(1023): "
<< std::negate()(1023) << std::endl;
std::cout << "std::negate()(1023): "
<< std::negate()(1023) << std::endl;
std::cout << "std::negate()(1023): "
<< std::negate()(8) << std::endl;
std::cout << "std::negate()(123): "
<< std::negate()(123) << std::endl;
std::cout << "std::negate()(101): "
<< std::negate()(101) << std::endl;
std::cout << "std::negate()(10230): "
<< std::negate()(10230) << std::endl;
std::cout << "std::negate()(1023): "
<< std::negate()(8) << std::endl;
std::cout << "std::negate()(123): "
<< std::negate()(123) << std::endl;
std::cout << "std::negate()(101): "
<< std::negate()(101) << std::endl;
std::cout << "std::negate()(10230): "
<< std::negate()(10230) << std::endl;
std::cout << "std::negate()(3.14): "
<< std::negate()(3.14) << std::endl;
std::cout << "std::negate()(3.14): "
<< std::negate()(3.14) << std::endl;
std::cout << "std::negate()(3): "
<< std::negate()(3) << std::endl;
std::cout << "std::negate()(3.56): "
<< std::negate()(3.56) << std::endl;
std::cout << "std::negate()(3.14): "
<< std::negate()(3.34) << std::endl;
std::cout << "std::negate()(Cell{101, 101}): "
<< std::negate()(Cell{101, 101}) << std::endl;
//编译失败
// std::cout << "std::negate()(\"I am a \", \"handsome programmer\"):"
// << std::negate()("I am a ", "handsome programmer") << std::endl;
return 0;
} | |
std::negate()(50):
std::negate()(1023): -1023
std::negate()(1023): -1023
std::negate()(1023): -1023
std::negate()(1023):
std::negate()(123): 65413
std::negate()(101): 4294967195
std::negate()(10230): 18446744073709541386
std::negate()(1023):
std::negate()(123): -123
std::negate()(101): -101
std::negate()(10230): -10230
std::negate()(3.14): -3.14
std::negate()(3.14): -3.14
std::negate()(3): -3
std::negate()(3.56): -3.56
std::negate()(3.14): -3
std::negate()(Cell{101, 101}): {-101,-101} |