c++ - 使用 Boost 的耗时(以秒为单位)

标签 c++ boost

我通常会在以下时间播种我的 RNG,通过调用 time.h 获得

#include <iostream>
#include <time.h>

using namespace std;



int main()
{
    cout << static_cast<unsigned int>(time(0)) << endl;
    return 0;
}

是否有相当于 static_cast<unsigned int>(time(0)) 的值在 Boost 库中?

最佳答案

您可以使用boost::posix_time获取时间。看这个SO question 。例如:

boost::posix_time::time_duration diff = tick - now;
diff.total_milliseconds();

您还可以使用C++11 chrono ,如果你可以使用C++11。例如:

int elapsed_milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();

通过这两种方法,您可以获取从一天开始算起的毫秒数,然后将其分配给您的种子。

关于c++ - 使用 Boost 的耗时(以秒为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15998243/

相关文章:

c++ - 连接 boost::mpl::string

c++ - QT5 QML-C++绑定(bind)与交互 : what am i doing wrong?

c++ - 结构在单独的头文件中导致 C++ 中的问题

c++ - Boost GraphML 格式

c++ - 如何确定 Boost header 是否可以用作仅 header ?

c++ - 在 Boost Spirit 中解析嵌套键值对

c++ - 如何使用boost weibull分布库进行随机数生成?

c# - 使用 C#/C++ 包装器寻找可能的内存泄漏

c++ - 作为重载运算符的函数包装器

c++ - STL 容器与结构