C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

常用链接

留言簿(8)

搜索

  •  

最新评论

阅读排行榜

评论排行榜


      该程序以系统时间单位为单位(而不是以秒为单位)计算延迟时间,避免了在每轮循环中将系统时间转换为秒。
#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 
      这样,预处理器将在编译程序时用char替换所有的BYTE,从而使BYTE成为char的别名。
      第二种方法是使用C++(和C)的关键字typedef来创建别名。例如:
typedef typeName aliasName
      下面是通用格式:
typedef char byte;//makes byte an alias for char
      换句话说,如果要将aliasName作为某种类型的别名,可以声明aliasName,如同将aliasName声明为这种类型的变量那样,然后在声明的前面加上关键字typedef。例如,要让byte_pointer成为char指针的别名,可将byte_pointer声明为char指针,然后在前面加上typedef:
typedef char * byte_pointer;  //pointer to char type
      与使用#define相比,使用typedef是一种更佳的选择——有时候,这也是唯一的选择。它能够处理更复杂的类型别名。
      注意,typedef不会创建新类型,而只是为已有的类型建立一个新名称。如果将word作为int的别名,则cout将把word类型的值视为int类型。
posted on 2010-02-06 20:29 烟皑 阅读(1160) 评论(0)  编辑 收藏 引用 所属分类: C++ primer plus学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理