有些场合用“引用传递”替换“值传 递”可以提高效率

如果函数的返回值是一个对象,有些场合用“引用传递”替换“值传 递”可以提高效率。而有些场合只能用“值传递”而不能用“引用传递”,否则会出 错。

 

 1 #include <iostream>
 2 #include <math.h>
 3 #include <stdlib.h>
 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     double y;
 9     int N;
10     //输入一个大于等于0的数
11     do {
12         cout<<"N=";
13         cin>>N;
14         if (N>=0) break;
15     } while (1);
16 
17     //计算并显示
18     for(int i=0;i<=N;i++){
19         y=pow(2,i);
20         cout<<"pow("<<2<<","<<i<<")="<<y<<endl;
21     }
22     return 0;
23 }

 

posted @ 2018-08-03 13:14  Borter  阅读(173)  评论(0)    收藏  举报