省去临时对象的构造和析构过程,从而提高效率

如果输入参数以值传递的方式传递对象,则宜改用“const &”方式来 传递,这样可以省去临时对象的构造和析构过程,从而提高效率。

 

 1 #include <iostream>
 2 #include<stdlib.h>
 3 #define MAX 30
 4 //main()的定义
 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 6 using namespace std;
 7 int main(int argc, char** argv) {
 8         char str[MAX],*p;
 9 
10     //从键盘上输入int数
11     cout<<"Please input a int:"<<endl;
12     int n;
13     cin>>n;
14 
15     //将整型数n按十进制转换为字符串并输出
16     p=itoa(n,str,10);
17     cout<<"str="<<str<<endl;
18     cout<<"p="<<p<<endl;
19 
20     //将整型数n按十六进制转换为字符串并输出
21     p=itoa(n,str,16);
22     cout<<"str="<<str<<endl;
23     cout<<"p="<<p<<endl;
24 
25     //从键盘上输入double类型的数据
26     cout<<"Please input a double:"<<endl;
27     double x;
28     cout<<"x=";
29     cin>>x;
30 
31     //将浮点数x转换为字符串后输出
32     p=gcvt(x,10,str);
33     cout<<"str="<<str<<endl;
34     cout<<"p="<<p<<endl;
35     
36     return 0;
37     return 0;
38 }

 

posted @ 2018-08-03 13:04  borter  阅读(124)  评论(0编辑  收藏  举报