< ctime>的替代方法 [英] Alternatives to <ctime>

查看:179
本文介绍了< ctime>的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找< ctime> 库的C ++ 11版本。这是否包含在C ++ 11中?

I'm looking for a C++11 version of the <ctime> library. Were any like this included with C++11?

编辑:
任何具有更多功能的东西将是完美的!

Anything with more functionality would be perfect!

编辑2:
我想使用这个游戏,我可以跟踪总播放时间。

EDIT 2: I'm looking to use this with a game I'm making so that I can keep track of total time played. Anything that would help me with that is what I'm looking for.

推荐答案

C ++ 11包括< chrono> 头,它提供不同类型的时钟(我将使用高分辨率时钟),具有现在 。您可以从 now()中减去这些时间中的两个,以获得< unit> s我将使用秒):

C++11 includes the <chrono> header, which provides different types of clocks (I'll use the high resolution one), that have a now function. You can subtract two of these times received from now() to get the total number of <unit>s (I'll use seconds) between them:

using clock = std::chrono::high_resolution_clock;
using unit = std::chrono::seconds;
std::chrono::time_point<clock> startTime = clock::now(); //get starting time

... //do whatever stuff you have to do

std::chrono::time_point<clock> thisTime = clock::now();
long long secondsElapsed = std::chrono::duration_cast<unit>(thisTime - startTime).count();

//now use secondsElapsed however you wish
//you can also use other units, such as milliseconds or nanoseconds

但请注意, secondsElapsed 不能保证为正数,除非 is_steady 成员 true ,因为成员 true 意味着随后调用 now()将给出比以前调用 now()更大的数字。

Do note, however, that secondsElapsed is not guaranteed to be positive unless the is_steady member of the clock is true because that member being true means that a subsequent call to now() will give a larger number than a former call to now().

这篇关于&lt; ctime&gt;的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆