operators (std::chrono::duration)
运算符+,-,%2A,%28 std::timeo::工期%29
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type | (1) | |
---|---|---|
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type constexpr operator-( const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs | (2) | |
template< class Rep1, class Period, class Rep2 > duration<typename std::common_type<Rep1,Rep2>::type, Period> constexpr operator*( const duration<Rep1,Period>& d, const Rep2& s | (3) | |
template< class Rep1, class Rep2, class Period > duration<typename std::common_type<Rep1,Rep2>::type, Period> constexpr operator*( const Rep1& s, const duration<Rep2,Period>& d | (4) | |
template< class Rep1, class Period, class Rep2 > duration<typename std::common_type<Rep1,Rep2>::type, Period> constexpr operator/( const duration<Rep1,Period>& d, const Rep2& s | (5) | |
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<Rep1,Rep2>::type constexpr operator/( const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs | (6) | |
template< class Rep1, class Period, class Rep2 > duration<typename std::common_type<Rep1,Rep2>::type, Period> constexpr operator%( const duration<Rep1, Period>& d, const Rep2& s | (7) | |
template< class Rep1, class Period1, class Rep2, class Period2 > typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type constexpr operator%( const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs | (8) | |
在两个持续时间之间或持续时间与滴答计数之间执行基本算术操作。
1%29将两个持续时间转换为它们的公共类型,并创建一个持续时间,其滴答计数是转换后的滴答计数之和。
2%29将两个持续时间转换为它们的公共类型,并创建一个期限,其滴答计数为rhs
从lhs
转换后的蜱数。
3-4%29转换持续时间d
给他的人rep
是Rep1
和Rep2
,并乘以转换后的蜱数。s
...
5%29转换持续时间d
给他的人rep
是Rep1
和Rep2
,并将转换后的蜱数除以s
6%29将这两个持续时间转换为它们的公共类型,并将lhs
的滴答计数转换后rhs
在转换之后。请注意,此运算符的返回值不是持续时间。
7%29转换持续时间d
给他的人rep
是Rep1
和Rep2
,并创建一个持续时间,其刻度计数是刻度计数除法的剩余部分,在转换后,s
...
8%29将这两个持续时间转换为它们的公共类型,并创建一个持续时间,其刻度计数是转换后的滴答计数的其余部分。
参数
lhs | - | duration on the left-hand side of the operator |
---|---|---|
rhs | - | duration on the right-hand side of the operator |
d | - | the duration argument for mixed-argument operators |
s | - | tick count argument for mixed-argument operators |
返回值
假设CD是函数返回类型和CR<A, B>=std::common_type<A, B>::type,然后:
1%29CD(CD(lhs).count() + CD(rhs).count())
2%29CD(CD(lhs).count() - CD(rhs).count())
3-4%29CD(CD(d).count() * s)
5%29CD(CD(d).count() / s).
6%29CD(lhs).count() / CD(rhs).count()
%28此运算符的返回类型不是持续时间%29
7%29CD(CD(d).count() % s)
8%29CD(CD(lhs).count() % CD(rhs).count())
例
二次
#include <chrono>
#include <iostream>
int main()
{
// simple arithmetic
std::chrono::seconds s = std::chrono::hours(1)
+ 2*std::chrono::minutes(10)
+ std::chrono::seconds(70)/10;
std::cout << "1 hour + 2*10 min + 70/10 sec = " << s.count() << " seconds\n";
// difference between dividing a duration by a number
// and dividing a duration by another duration
std::cout << "Dividing that by 2 minutes gives "
<< s / std::chrono::minutes(2) << '\n';
std::cout << "Dividing that by 2 gives "
<< (s / 2).count() << " seconds\n";
// the remainder operator is useful in determining where in a time
// frame is this particular duration, e.g. to break it down into hours,
// minutes, and seconds:
std::cout << s.count() << " seconds is "
<< std::chrono::duration_cast<std::chrono::hours>(
s
).count() << " hours, "
<< std::chrono::duration_cast<std::chrono::minutes>(
s % std::chrono::hours(1)
).count() << " minutes, "
<< std::chrono::duration_cast<std::chrono::seconds>(
s % std::chrono::minutes(1)
).count() << " seconds\n";
}
二次
产出:
二次
1 hour + 2*10 min + 70/10 sec = 4807 seconds
Dividing that by 2 minutes gives 40
Dividing that by 2 gives 2403 seconds
4807 seconds is 1 hours, 20 minutes, 7 seconds
二次
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。