Namespaces
Variants

std::chrono::duration<Rep,Period>:: operator+=, -=, *=, /=, %=

From cppreference.net
(1)
duration & operator + = ( const duration & d ) ;
(自 C++11 起)
(自 C++17 起为 constexpr)
(2)
duration & operator - = ( const duration & d ) ;
(自 C++11 起)
(自 C++17 起为 constexpr)
(3)
duration & operator * = ( const rep & rhs ) ;
(自 C++11 起)
(自 C++17 起为 constexpr)
(4)
duration & operator / = ( const rep & rhs ) ;
(自 C++11 起)
(自 C++17 起为 constexpr)
(5)
duration & operator % = ( const rep & rhs ) ;
(自 C++11 起)
(自 C++17 起为 constexpr)
(6)
duration & operator % = ( const duration & rhs ) ;
(自 C++11 起)
(自 C++17 起为 constexpr)

在两个具有相同周期的时长之间,或在时长与滴答计数值之间执行复合赋值操作。

如果 rep_ 是此持续时间对象中保存滴答数的成员变量,

1) 等价于 rep_ + = d. count ( ) ; return * this ;
2) 等价于 rep_ - = d. count ( ) ; return * this ;
3) 等价于 rep_ * = rhs ; return * this ;
4) 等价于 rep_ / = rhs ; return * this ;
5) 等价于 rep_ % = rhs ; return * this ;
6) 等价于 rep_ % = d. count ( ) ; return * this ;

目录

参数

d - 操作符右侧的时长
rhs - 操作符右侧的滴答数

返回值

修改后对此持续时间的引用。

示例

#include <chrono>
#include <iostream>
int main()
{
    std::chrono::minutes m(11);
    m *= 2;
    m += std::chrono::hours(10); // 小时隐式转换为分钟
    std::cout << m.count() << " minutes equals "
              << std::chrono::duration_cast<std::chrono::hours>(m).count() 
              << " hours and ";
    m %= std::chrono::hours(1);
    std::cout << m.count() << " minutes\n";
}

输出:

622 minutes equals 10 hours and 22 minutes

参见

递增或递减滴答计数
(公开成员函数)
实现以时长为参数的算术运算
(函数模板)