猿问

while (clock() - start < delay ) 这里的clock()

#include <iostream>

#include <ctime> // describes clock() function, clock_t type

int main()

{

using namespace std;

cout << "Enter the delay time, in seconds: ";

float secs;

cin >> secs;

clock_t delay = secs * CLOCKS_PER_SEC;  // convert to clock ticks

cout << "starting\a\n";

clock_t start = clock();                这里和下一句没看懂,什么意思?clock()赋给start 了,怎么再减啊?

while (clock() - start < delay )        // wait until time elapses

;                                   // note the semicolon

cout << "done \a\n";

return 0;
}

xungeer29
浏览 2017回答 1
1回答

望远

//clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下: clock_t clock(void) ; //这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型。clock_t delay = secs * CLOCKS_PER_SEC;//这是将延迟时间秒-->CPU时钟计时单元while (clock()-start < delay); //通过这个循环来每次取出新的CPU时钟计时单元,减去一开始的start单元时间,获得时间差,来延时
随时随地看视频慕课网APP
我要回答