用内联取代宏代码

用内联取代宏代码

C++ 语言支持函数内联,其目的是为了提高函数的执行效率(速度)。 在 C 程序中,可以用宏代码提高执行效率。宏代码本身不是函数,但使用起来象函 数。预处理器用复制宏代码的方式代替函数调用,省去了参数压栈、生成汇编语言的 CALL 调用、返回参数、执行 return 等过程,从而提高了速度。使用宏代码最大的缺点是容易 出错,预处理器在复制宏代码时常常产生意想不到的边际效应。

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 int main(int argc, char** argv) {
 6         //定义结构类型
 7     struct human {
 8        char name[10];
 9        int sex;
10        int age;
11     };
12 
13     //声明结构数组和结构指针变量,并初始化
14     human x[]={{"WeiPing",1,30},{"LiHua",1,25},{"LiuMin",0,23}},*p=NULL;
15 
16     //用下标变量的输出结构数组的元素
17     for (int i=0;i<3;i++)
18     {
19         cout<<x[i].name<<'\t';
20         cout<<x[i].sex<<'\t';
21         cout<<x[i].age<<endl;
22     }
23     cout<<"----------------"<<endl;
24 
25     //用结构指针输出结构数组的元素
26     for (p=x;p<=&x[2];p++)
27     {
28         cout<<p->name<<'\t';
29         cout<<p->sex<<'\t';
30         cout<<p->age<<endl;
31     }
32     return 0;
33 }

 

posted @ 2018-08-02 12:51  borter  阅读(214)  评论(0编辑  收藏  举报