任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。
C++ 针对常用的算术和逻辑运算定义了很多函数对象:
std::not_equal_to
template< class T > |
(C++14 前) | |
template< class T = void > |
(C++14 起) |
进行比较的函数对象。除非特化,调用类型 T
上的 operator!= 。
标准库提供n
|
(C++14 起) |
|
(C++20 前) |
operator() |
检查参数是否不相等 (公开成员函数) |
bool operator()( const T& lhs, const T& rhs ) const; |
(C++14 前) | |
constexpr bool operator()( const T& lhs, const T& rhs ) const; |
(C++14 起) |
检查 lhs
是否不等于 rhs
。
lhs, rhs | - | 要比较的值 |
若 lhs != rhs 则为 true ,否则为 false 。
(无)
constexpr bool operator()(const T &lhs, const T &rhs) const
{
return lhs != rhs;
}
#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, 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;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
int main()
{
std::cout << std::boolalpha;
int *ptr = nullptr;
std::cout << "std::not_equal_to()(1023, 1024): "
<< std::not_equal_to()(ptr, nullptr) << std::endl;
std::cout << "std::not_equal_to()(50, 2): "
<< std::not_equal_to()(50, 2) << std::endl;
std::cout << "std::not_equal_to()('a', 97): "
<< std::not_equal_to()('a', 97) << std::endl;
std::cout << "std::not_equal_to()(1023, 1024): "
<< std::not_equal_to()(1023, 1024) << std::endl;
std::cout << "std::not_equal_to()(1023, 1024): "
<< std::not_equal_to()(1023, 1024) << std::endl;
std::cout << "std::not_equal_to()(1023, 1024): "
<< std::not_equal_to()(1023, 1024) << std::endl;
std::cout << "std::not_equal_to()(1023, 1024): "
<< std::not_equal_to()(8, 32) << std::endl;
std::cout << "std::not_equal_to()(123, 456): "
<< std::not_equal_to()(123, 456) << std::endl;
std::cout << "std::not_equal_to()(101, 202): "
<< std::not_equal_to()(101, 202) << std::endl;
std::cout << "std::not_equal_to()(10230, 10240): "
<< std::not_equal_to()(10230, 10240) << std::endl;
std::cout << "std::not_equal_to()(1023, 1024): "
<< std::not_equal_to()(8, 32) << std::endl;
std::cout << "std::not_equal_to()(123, 456): "
<< std::not_equal_to()(123, 456) << std::endl;
std::cout << "std::not_equal_to()(101, 202): "
<< std::not_equal_to()(101, 202) << std::endl;
std::cout << "std::not_equal_to()(10230, 10240): "
<< std::not_equal_to()(10230, 10240) << std::endl;
std::cout << "std::not_equal_to()(3.14, 3.14): "
<< std::not_equal_to()(3.14, 3.14) << std::endl;
std::cout << "std::not_equal_to()(3.14, 3.14): "
<< std::not_equal_to()(3.14, 3.14) << std::endl;
std::cout << "std::not_equal_to()(3, 3): "
<< std::not_equal_to()(3, 3) << std::endl;
std::cout << "std::not_equal_to()(3.56, 3.14): "
<< std::not_equal_to()(3.56, 3.14) << std::endl;
std::cout << "std::not_equal_to()(3.14, 3.14): "
<< std::not_equal_to()(3.34, 3.34) << std::endl;
std::cout << "std::not_equal_to()(Cell{101, 101}, Cell{202, 202}): "
<< std::not_equal_to()(Cell{101, 101}, Cell{202, 202}) << std::endl;
std::cout << "std::not_equal_to()(\"I am a \", \"handsome programmer\"):"
<< std::not_equal_to()("I am a ", "handsome programmer") << std::endl;
return 0;
}
| |
std::not_equal_to()(1023, 1024): false
std::not_equal_to()(50, 2): true
std::not_equal_to()('a', 97): false
std::not_equal_to()(1023, 1024): true
std::not_equal_to()(1023, 1024): true
std::not_equal_to()(1023, 1024): true
std::not_equal_to()(1023, 1024): true
std::not_equal_to()(123, 456): true
std::not_equal_to()(101, 202): true
std::not_equal_to()(10230, 10240): true
std::not_equal_to()(1023, 1024): true
std::not_equal_to()(123, 456): true
std::not_equal_to()(101, 202): true
std::not_equal_to()(10230, 10240): true
std::not_equal_to()(3.14, 3.14): false
std::not_equal_to()(3.14, 3.14): false
std::not_equal_to()(3, 3): false
std::not_equal_to()(3.56, 3.14): true
std::not_equal_to()(3.14, 3.14): false
std::not_equal_to()(Cell{101, 101}, Cell{202, 202}): true
std::not_equal_to()("I am a ", "handsome programmer"):true |