該程序以系統時間單位為單位(而不是以秒為單位)計算延遲時間,避免了在每輪循環中將系統時間轉換為秒。
#include "stdafx.h"
#include <ctime>//describes clock() function, clock_t type
#include <iostream>
using namespace std;
int main(int argc, char* argv[])

{
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();
while(clock()-start<delay)//wait until time elapses
; //note the semicolon
cout<<"done \a\n";
return 0;
}若還想進一步獲得關于ctime的一些知識,可看下面的隨筆:C/C++中的日期和時間——代碼全手敲驗證一遍
關于類型別名:C++為類型建立別名的方式有兩種。一種是使用預處理器:
#define BYTE char 第二種方法是使用C++(和C)的關鍵字typedef來創建別名。例如:
typedef typeName aliasName
typedef char byte;//makes byte an alias for char
typedef char * byte_pointer; //pointer to char type注意,typedef不會創建新類型,而只是為已有的類型建立一個新名稱。如果將word作為int的別名,則cout將把word類型的值視為int類型。


