草庐IT

c++ chrono意外行为

std::chrono::system_clock::time_pointstart;//1secondpassesstd::cout上面的代码,1秒后,在VisualStudio2012中给我10000000但是在gcc4.8.2上给了我100000000.将最后一行更改为std::chrono::duration_cast(std::chrono::high_resolution_clock::now()-start).count();按预期工作,并在两个编译器上给我相同的结果。这怎么可能? 最佳答案 根据http://en.c

c++ - boost::posix_time::milliseconds 和 boost::chrono::milliseconds 之间的区别

我正在尝试使用condition_variable_any::timed_wait()当我将boost::chrono::millisecond传递给函数时,它无法编译:error:nomatchfor‘operator+’in‘boost::get_system_time()+wait_duration’但是,如果我将boost::posix_time::milliseconds传递给它编译的函数。问题是我不明白两者之间的区别。他们都声称是持续时间。但据我了解posix时间,它代表自纪元以来的时间,对我来说这意味着boost::posix_time::millisecondsp(10

c++ - 如何修复 C++11 中 std::chrono 比较的编译错误?

我正在按照示例ASIOserverwithtimeout,此处显示的函数行已从deadline_timer::traits_type::now()修改为std::chrono::steady_clock::now()因为我想使用不带boost的独立ASIO。ASIO可以独立使用C++11。voidcheck_deadline(deadline_timer*deadline){if(stopped())return;//Checkwhetherthedeadlinehaspassed.comparethedeadlineagainst//thecurrenttime//Imodified

C++ Chrono 判断一天是否是周末?

我有一个年(int)、月(int)和日(int)形式的日期,例如,2018、10、12表示2018年10月12日。有没有一种方法可以使用带有这些整数的C++Chrono库来确定我的“日期”是否是周末?如果没有,实现这一目标的最简单替代方法是什么? 最佳答案 在C++20中,您将能够这样做:#includeconstexprboolis_weekend(std::chrono::sys_dayst){usingnamespacestd::chrono;constweekdaywd{t};returnwd==Saturday||wd==

c++ - 从变量转换时出现 std::chrono::time_point 编译器错误

我有一个longlong类型的变量,它表示以纳秒为单位的时间点。我正在尝试使用std::chrono::time_point包装它,但编译器(VS2017)给我带来了麻烦。这是编译的代码:std::chrono::time_pointtpStart(std::chrono::nanoseconds(10ll));std::chrono::time_pointtpEnd=std::chrono::steady_clock::now();doubled=std::chrono::duration(tpEnd-tpStart).count();现在,如果我用变量切换值10ll,计算持续时间的

c++ - 使用 chrono 获取当前日期

C++11提供了返回当前时间的函数。但是,我找不到返回当前日期的函数。我使用boost来这样做。boost::gregorian::dateTODAY=boost::gregorian::day_clock::local_day();有什么方法可以使用chrono实现相同的结果吗?编辑:我想要的是使用time_point来表示当天。即时分秒为零2013-07-3100:00:00 最佳答案 Chronos不会延期到日期问题;这真的不是它的目的。日期问题和“时间”问题的分界线是天。而且Chrono没有定义日期类型。但您可以将时间除以2

C++ 如何将日期从输入(字符串年、字符串月、字符串日)转换为时间点

我的老师说我必须用这样的行来转换文件:JanKowalski199743给有游泳池的人上课,比如:stringname,surname;std::chrono::system_clock::time_pointdateofbirth;如何从3个整数创建时间点?我想这不是存储此类数据的最简单方法。"Areferencetoaspecificpointintime,likeone'sbirthday,today'sdawn,orwhenthenexttrainpasses.Inthislibrary,objectsofthetime_pointclasstemplateexpressthi

c++ - C++向chrono::system_clock::time_point添加月份

如何将chrono::system_clock::time_point值加数月?谢谢! 最佳答案 概述这是一个非常有趣的问题,有几个答案。“正确”的答案是您必须针对特定应用程序决定的。使用月份,您可以选择按时间顺序进行计算或进行日历计算。按时间顺序的计算处理时间点和持续时间的常规单位,例如小时,分钟和秒。日历计算处理不规则的日历,该日历主要用来给日子起令人难忘的名字。年表计算如果问题是关于future几个月的物理过程,那么物理学并不关心不同的月份有不同的长度,因此按时间顺序计算就足够了:婴儿要在9个月内到期。从现在开始的6个月后,这

c++ - 如何将 std::chrono::high_resolution_clock::now() 转换为毫秒、微秒...?

我从Howtogetduration,asintmilli'sandfloatsecondsfrom?得到了这段代码#include#includeintmain(intargc,char*argv[]){autot0=std::chrono::high_resolution_clock::now();autot1=std::chrono::high_resolution_clock::now();std::chrono::durationfs=t1-t0;std::chrono::millisecondsd=std::chrono::duration_cast(fs);std::co

c++ - 控制帧率

我正在创建一堆需要在帧周期内工作的线程。我想控制一秒钟完成多少帧。我简化了我的代码,这样我就可以向你展示我写的内容//setuptheframetimerstd::chrono::time_pointstart=std::chrono::system_clock::now();std::chrono::time_pointend=std::chrono::system_clock::now();while(running==true){//updatetimerstart=std::chrono::system_clock::now();std::chrono::durationela