operator==,!=,<,<=,>,>=,<=>(std::reverse_iterator)
(1) | ||
template< class Iterator1, class Iterator2 > bool operator==( const std::reverse_iterator<Iterator1>& lhs, |
(直到 C++17) | |
template< class Iterator1, class Iterator2 > constexpr bool operator==( const std::reverse_iterator<Iterator1>& lhs, |
(自 C++17 起) | |
(2) | ||
template< class Iterator1, class Iterator2 > bool operator!=( const std::reverse_iterator<Iterator1>& lhs, |
(直到 C++17) | |
template< class Iterator1, class Iterator2 > constexpr bool operator!=( const std::reverse_iterator<Iterator1>& lhs, |
(自 C++17 起) | |
(3) | ||
template< class Iterator1, class Iterator2 > bool operator<( const std::reverse_iterator<Iterator1>& lhs, |
(直到 C++17) | |
template< class Iterator1, class Iterator2 > constexpr bool operator<( const std::reverse_iterator<Iterator1>& lhs, |
(自 C++17 起) | |
(4) | ||
template< class Iterator1, class Iterator2 > bool operator<=( const std::reverse_iterator<Iterator1>& lhs, |
(直到 C++17) | |
template< class Iterator1, class Iterator2 > constexpr bool operator<=( const std::reverse_iterator<Iterator1>& lhs, |
(自 C++17 起) | |
(5) | ||
template< class Iterator1, class Iterator2 > bool operator>( const std::reverse_iterator<Iterator1>& lhs, |
(直到 C++17) | |
template< class Iterator1, class Iterator2 > constexpr bool operator>( const std::reverse_iterator<Iterator1>& lhs, |
(自 C++17 起) | |
(6) | ||
template< class Iterator1, class Iterator2 > bool operator>=( const std::reverse_iterator<Iterator1>& lhs, |
(直到 C++17) | |
template< class Iterator1, class Iterator2 > constexpr bool operator>=( const std::reverse_iterator<Iterator1>& lhs, |
(自 C++17 起) | |
template< class Iterator1, std::three_way_comparable_with<Iterator1> Iterator2 > constexpr std::compare_three_way_result_t<Iterator1, Iterator2> |
(7) | (自 C++20 起) |
比较底层迭代器。应用逆向比较以考虑迭代器顺序是相反的。
(1-6) 只有当它们的底层比较表达式(见下文)格式正确且可转换为 bool 时,才参与重载解析。 |
(自 C++20 起) |
内容 |
[编辑] 参数
lhs, rhs | - | 要比较的迭代器适配器 |
[编辑] 返回值
[编辑] 示例
#include <compare> #include <iostream> #include <iterator> int main() { int a[]{0, 1, 2, 3}; // ↑ └───── x, y // └──────── z std::reverse_iterator<int*> x{std::rend(a) - std::size(a)}, y{std::rend(a) - std::size(a)}, z{std::rbegin(a) + 1}; std::cout << std::boolalpha << "*x == " << *x << '\n' // 3 << "*y == " << *y << '\n' // 3 << "*z == " << *z << '\n' // 2 << "x == y ? " << (x == y) << '\n' // true << "x != y ? " << (x != y) << '\n' // false << "x < y ? " << (x < y) << '\n' // false << "x <= y ? " << (x <= y) << '\n' // true << "x == z ? " << (x == z) << '\n' // false << "x != z ? " << (x != z) << '\n' // true << "x < z ? " << (x < z) << '\n' // true << "x <= z ? " << (x <= z) << '\n' // true << "x <=> y == 0 ? " << (x <=> y == 0) << '\n' // true << "x <=> y < 0 ? " << (x <=> y < 0) << '\n' // false << "x <=> y > 0 ? " << (x <=> y > 0) << '\n' // false << "x <=> z == 0 ? " << (x <=> z == 0) << '\n' // false << "x <=> z < 0 ? " << (x <=> z < 0) << '\n' // true << "x <=> z > 0 ? " << (x <=> z > 0) << '\n' // false ; }
输出
*x == 3 *y == 3 *z == 2 x == y ? true x != y ? false x < y ? false x <= y ? false x == z ? false x != z ? true x < z ? true x <= z ? true x <=> y == 0 ? true x <=> y < 0 ? false x <=> y > 0 ? false x <=> z == 0 ? false x <=> z < 0 ? true x <=> z > 0 ? false
缺陷报告
以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 发布的行为 | 正确的行为 |
---|---|---|---|
LWG 280 | C++98 | 一个 std::reverse_iterator 只能与另一个具有相同底层迭代器类型的std::reverse_iterator 进行比较 |
允许 std::reverse_iterator 的比较具有不同的底层迭代器类型 |