C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- std::chrono::duration
- std::chrono::ceil(std::chrono::duration)
- std::chrono::round(std::chrono::duration)
- std::chrono::abs(std::chrono::duration)
- operator+,-,*,/,%(std::chrono::duration)
- operator==,!=,<,<=,>,>=,<=>(std::chrono::duration)
- std::chrono::duration_cast
- std::chrono::floor(std::chrono::duration)
- std::common_type(std::chrono::duration)
- std::formatter<std::chrono::duration>
- std::chrono::duration<Rep,Period>::operator+=, -=, *=, /=, %=
- std::chrono::operator<< (std::chrono::duration)
- std::chrono::from_stream (std::chrono::duration)
- std::chrono::duration<Rep,Period>::max
- std::chrono::duration<Rep,Period>::operator+(unary), std::chrono::duration<Rep,Period>::operator-(unary)
- std::chrono::duration<Rep,Period>::operator++, std::chrono::duration<Rep,Period>::operator--
- std::chrono::duration<Rep,Period>::zero
- std::chrono::duration<Rep,Period>::min
- std::chrono::duration<Rep,Period>::duration
- std::chrono::duration<Rep,Period>::operator=
- std::chrono::duration<Rep,Period>::count
- std::chrono::clock_cast
- std::chrono::is_am, std::chrono::is_pm, std::chrono::make12, std::chrono::make24
- std::chrono::system_clock
- std::chrono::steady_clock
- std::chrono::high_resolution_clock
- std::chrono::clock_time_conversion
- std::chrono::time_point
- C 日期和时间工具
- std::chrono::is_clock
- std::chrono::utc_clock
- std::chrono::tai_clock
- std::chrono::gps_clock
- std::chrono::file_clock
- std::chrono::local_t
- std::chrono::day
- std::chrono::month
- std::chrono::year
- std::chrono::weekday
- std::chrono::operator/ (日历)
- std::chrono::year_month_day
- std::chrono::year_month_day_last
- std::chrono::year_month_weekday
- std::chrono::year_month_weekday_last
- std::chrono::weekday_indexed
- std::chrono::weekday_last
- std::chrono::month_day
- std::chrono::month_day_last
- std::chrono::month_weekday
- std::chrono::month_weekday_last
- std::chrono::year_month
- std::chrono::tzdb
- std::chrono::tzdb_list
- std::chrono::get_tzdb_list, std::chrono::get_tzdb, std::chrono::remote_version, std::chrono::reload_tzdb
- std::chrono::time_zone
- std::chrono::locate_zone
- std::chrono::current_zone
- std::chrono::zoned_time
- std::chrono::time_zone_link
- std::chrono::leap_second
- std::chrono::time_of_day
- std::chrono::last_spec
- std::chrono::treat_as_floating_point
- std::literals::chrono_literals::operator""ns
- std::literals::chrono_literals::operator""y
- std::literals::chrono_literals::operator""d
- std::literals::chrono_literals::operator""s
- std::literals::chrono_literals::operator""ms
- std::literals::chrono_literals::operator""us
- std::literals::chrono_literals::operator""h
- std::literals::chrono_literals::operator""min
- std::chrono::duration_values
- std::chrono::nonexistent_local_time
- std::chrono::ambiguous_local_time
- 注释
- 字符串库
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >日期和时间工具 >std::chrono::duration > operator+,-,*,/,%(std::chrono::duration)
operator+,-,*,/,%(std::chrono::duration)
typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type
constexpr operator+( const duration<Rep1,Period1>& lhs,
typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type
constexpr operator-( const duration<Rep1,Period1>& lhs,
duration<typename std::common_type<Rep1,Rep2>::type, Period>
constexpr operator*( const duration<Rep1,Period>& d,
duration<typename std::common_type<Rep1,Rep2>::type, Period>
constexpr operator*( const Rep1& s,
duration<typename std::common_type<Rep1,Rep2>::type, Period>
constexpr operator/( const duration<Rep1,Period>& d,
typename std::common_type<Rep1,Rep2>::type
constexpr operator/( const duration<Rep1,Period1>& lhs,
duration<typename std::common_type<Rep1,Rep2>::type, Period>
constexpr operator%( const duration<Rep1, Period>& d,
typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type
constexpr operator%( const duration<Rep1,Period1>& lhs,
在二个时长或时长和计次数之间进行基本算术运算。
lhs
的计次数减去 rhs
的计次数的时长。d
到其 rep
为 Rep1
与 Rep2
之间的共用类型的时长,并将转换后的计次数乘 s
。这些重载仅若 s
可转换到 typename std::common_type<Rep1, Rep2>::type 才参与重载决议。d
到其 rep
为 Rep1
与 Rep2
之间的共用类型的时长,并将转换后的计次数除以 s
。此重载仅若 s
可转换到 typename std::common_type<Rep1, Rep2>::type 且 Rep2
不是 duration
的特化才参与重载决议。lhs
转换后的计次数除以 rhs
转换后的计次数。注意此运算符的返回值不是时长。d
到其 rep
为 Rep1
与 Rep2
之间的共用类型的时长,并创建计次数为此时长的计次数除以 s
的时长。此重载仅若 s
可转换到 typename std::common_type<Rep1, Rep2>::type 且 Rep2
不是 duration
的特化才参与重载决议。参数
lhs | - | 运算符左侧的 duration |
rhs | - | 运算符右侧的 duration |
d | - | 混合参数运算符的时长参数 |
s | - | 混合参数运算符的计次参数 |
返回值
假设 CD 为函数返回类型且 CR<A, B> = std::common_type<A, B>::type ,则:
示例
#include <chrono> #include <iostream> int main() { // 简单算术 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"; // 时长除以一个数和时长除以另一时长的区别 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"; // 余数运算符在确定此特定时长在时间框架的场合有用, // 例如,拆分它为时、分和秒: 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
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
DR | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 3050 | C++11 | 可转换性制约使用非 const 亡值 | 替而使用 const 左值 |