operator==,!=,<,<=,>,>=(std::chrono::duration)

template <class Rep1, class Period1, class Rep2, class Period2>

constexpr bool operator==(const duration<Rep1, Period1>& lhs,

                          const duration<Rep2, Period2>& rhs);
(1)
template <class Rep1, class Period1, class Rep2, class Period2>

constexpr bool operator!=(const duration<Rep1, Period1>& lhs,

                          const duration<Rep2, Period2>& rhs);
(2)
template <class Rep1, class Period1, class Rep2, class Period2>

constexpr bool operator<(const duration<Rep1, Period1>& lhs,

                         const duration<Rep2, Period2>& rhs);
(3)
template <class Rep1, class Period1, class Rep2, class Period2>

constexpr bool operator<=(const duration<Rep1, Period1>& lhs,

                          const duration<Rep2, Period2>& rhs);
(4)
template <class Rep1, class Period1, class Rep2, class Period2>

constexpr bool operator>(const duration<Rep1, Period1>& lhs,

                         const duration<Rep2, Period2>& rhs);
(5)
template <class Rep1, class Period1, class Rep2, class Period2>

constexpr bool operator>=(const duration<Rep1, Period1>& lhs,

                          const duration<Rep2, Period2>& rhs);
(6)

比较二个时长。

1-2) 检查 lhsrhs 是否相等,即以两个 duration 的共用类型表示的计次数是否相等。

3-6) 比较 lhsrhs ,即比较两个 duration 的共用类型表示的计次数。

参数

lhs - 运算符左侧的 duration
rhs - 运算符右侧的 duration

返回值

假设 CT =
std::common_type<std::chrono::duration<Rep1, Period1>,
                 std::chrono::duration<Rep2, Period2>>::type
,则为:

1) CT(lhs).count() == CT(rhs).count()

2) !(lhs == rhs)

3) CT(lhs).count() < CT(rhs).count()

4) !(rhs < lhs)

5) rhs < lhs

6) !(lhs < rhs)

示例

#include <chrono>
#include <iostream>
int main()
{
    if(std::chrono::seconds(2) == std::chrono::milliseconds(2000))
        std::cout <<  "2 s == 2000 ms\n";
    else
        std::cout <<  "2 s != 2000 ms\n";
 
    if(std::chrono::seconds(61) > std::chrono::minutes(1))
        std::cout <<  "61 s > 1 min\n";
    else
        std::cout <<  "61 s <= 1 min\n";
 
}

输出:

2 s == 2000 ms
61 s > 1 min

版本历史

  • (当前 | 先前 2017年10月10日 (二) 10:39Fruderica讨论 | 贡献. . (2,621字节) (-449). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 16:42P12bot讨论 | 贡献 . . (3,070字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 07:21P12bot讨论 | 贡献 . . (3,070字节) (-116). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:19P12bot讨论 | 贡献 . . (3,186字节) (+321). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 09:50P12讨论 | 贡献 . . (2,865字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (2,865字节) (-31). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 13:14P12讨论 | 贡献 . . (2,896字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (2,896字节) (+2,896). . (Translated from the English version using Google Translate)