首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::chrono::duration

Defined in header <chrono>

?

?

template< class Rep, class Period = std::ratio<1> > class duration;

?

(since C++11)

类模板std::chrono::duration表示时间间隔。

它由一组类型的滴答组成。Rep还有一个刻度周期,其中刻度周期是一个编译时的Rational常数,表示从一个滴答到下一个滴答的秒数。

中存储的唯一数据。duration是类型的滴答数。Rep.如果Rep是浮点,则duration可以代表蜱的分数。Period作为持续时间%27s类型的一部分,并且仅在不同持续时间之间进行转换时使用。

成员类型

Member type

Definition

rep

Rep, an arithmetic type representing the number of ticks

period

Period (until C++17)typename Period::type (since C++17), a std::ratio representing the tick period (i.e. the number of seconds per tick)

成员函数

(constructor)

constructs new duration (public member function)

operator=

assigns the contents (public member function)

count

returns the count of ticks (public member function)

zero static

returns the special duration value zero (public static member function)

min static

returns the special duration value min (public static member function)

max static

returns the special duration value max (public static member function)

operator+operator-

implements unary + and unary - (public member function)

operator++operator++(int)operator--operator--(int)

increments or decrements the tick count (public member function)

operator+=operator-=operator*=operator/=operator%=

implements compound assignment between two durations (public member function)

非会员职能

std::common_type<std::chrono::duration>

specializes the std::common_type trait (class template specialization)

operator+operator-operator*operator/operator%

implements arithmetic operations with durations as arguments (function template)

operator==operator!=operator<operator<=operator>operator>=

compares two durations (function template)

duration_cast

converts a duration to another, with a different tick interval (function template)

floor(std::chrono::duration) (C++17)

converts a duration to another, rounding down (function template)

ceil(std::chrono::duration) (C++17)

converts a duration to another, rounding up (function template)

round(std::chrono::duration) (C++17)

converts a duration to another, rounding to nearest, ties to even (function template)

abs(std::chrono::duration) (C++17)

obtains the absolute value of the duration (function template)

帮助者类型

Type

Definition

std::chrono::nanoseconds

duration</*signed integer type of at least 64 bits*/, std::nano>

std::chrono::microseconds

duration</*signed integer type of at least 55 bits*/, std::micro>

std::chrono::milliseconds

duration</*signed integer type of at least 45 bits*/, std::milli>

std::chrono::seconds

duration</*signed integer type of at least 35 bits*/>

std::chrono::minutes

duration</*signed integer type of at least 29 bits*/, std::ratio<60>>

std::chrono::hours

duration</*signed integer type of at least 23 bits*/, std::ratio<3600>>

注:每种预定义的持续时间类型至少涵盖±292年。

帮助者类

treat_as_floating_point

indicates that a duration is convertible to duration with different tick period (class template)

duration_values

constructs zero, min, and max values of a tick count of given type (class template)

文字

定义在内联命名空间std::文本::chrono中[医]文字

*。

操作符“h%28C++14%29

操作符“min”%28C++14%29

运算符“s%28C++14%29

操作符ms%28C++14%29

操作符“”US%28C++14%29

运算符ns%28C++14%29

此示例演示如何定义几种自定义持续时间类型并在类型之间进行转换:

二次

代码语言:javascript
复制
#include <iostream>
#include <chrono>
 
int main()
{
    using shakes = std::chrono::duration<int, std::ratio<1, 100000000>>;
    using jiffies = std::chrono::duration<int, std::centi>;
    using microfortnights = std::chrono::duration<float, std::ratio<12096,10000>>;
    using nanocenturies = std::chrono::duration<float, std::ratio<3155,1000>>;
 
    std::chrono::seconds sec(1);
 
    std::cout << "1 second is:\n";
 
    // integer scale conversion with no precision loss: no cast
    std::cout << std::chrono::microseconds(sec).count() << " microseconds\n"
              << shakes(sec).count() << " shakes\n"
              << jiffies(sec).count() << " jiffies\n";
 
    // integer scale conversion with precision loss: requires a cast
    std::cout << std::chrono::duration_cast<std::chrono::minutes>(sec).count()
              << " minutes\n";
 
    // floating-point scale conversion: no cast
    std::cout << microfortnights(sec).count() << " microfortnights\n"
              << nanocenturies(sec).count() << " nanocenturies\n";
}

二次

产出:

二次

代码语言:javascript
复制
1 second is:
1000000 microseconds
100000000 shakes
100 jiffies
0 minutes
0.82672 microfortnights
0.316957 nanocenturies

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com