intmain() { int m; cout << "Enter the number of diskes: "; cin >> m; cout << "the steps to moving " << m << " diskes:" << endl; hanoi(m, 'A', 'B', 'C'); return0; }
// function to reverse a string voidstringReverse( string stringToReverse, int startSubscript ) { // return when null character is encountered if ( startSubscript == stringToReverse.length() ) return;
// recursively call stringReverse with new substring stringReverse( stringToReverse, startSubscript + 1 ); cout << stringToReverse[ startSubscript ]; // output character } // end function stringReverse
② 递归查询最小元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// function to recursively find minimum array element constint MAXRANGE = 1000; intrecursiveMinimum( constintarray[], int low, int high ) { staticint smallest = MAXRANGE;
// if first element of array is smallest so far // set smallest equal to that element if ( array[ low ] < smallest ) smallest = array[ low ];
// if only one element in array, return smallest // else recursively call recursiveMinimum with new subarray return low == high ? smallest : recursiveMinimum( array, low + 1, high ); }
三、函数的参数传递
在函数被调用时才给形参分配存储单元
实参可以是常量、变量或表达式
实参类型必须和形参类型相符
值传递是传递参数值,形参的改变不会引起实参的改变,即单向传递
引用传递可以实现双向传递
常引用作参数可以保障实参数据的安全
1. 值传递
传递参数值,单向传递
2. 引用传递(需要修改形参值的时候使用)
我们常用的交换函数就需要用到引用传递,实现双向传递,修改传进来的形参值
1 2 3 4
voidswap(int &a, int &b){ int temp = a; a = b; b = temp;
学习 引用传递 需详细掌握以下 引用 的知识
① 变量的引用
1 2 3 4 5 6 7 8 9
int &r = n; 某个变量的引用,等价于这个变量(即同一个人,不管谁发生改变,另一个也会随之改变) int n = 7; int &r = n; //定义引用时一定要将其初始化引用某个*变量* r = 4; cout<<r; //4 cout<<n; //4 n = 5; cout<<r; //5
1 2 3 4 5 6
int a = 4; int b = 5; int &r1 = a; int &r2 = r1; //等价于 int &r2 = a; r2也引用a r2 = 10; cout<<a; //10
一旦一个引用被初始化后,就不能改为指向其他对象
1 2 3 4 5 6
int a = 4; int b = 5; int &r1 = a; //*一个引用是从一而终的*,该语句并不表示r1引用了b,而是b的值赋给了r1 r1 = b; cout<<a; //5
② 引用作为函数的返回值
1 2 3 4 5 6 7 8
int n = 4; int &setValue(){ //返回了n的引用 return n; } intmain(){ setValue() = 40; //函数返回的是n的引用,所以可以作为左值被赋值 cout<<n; //给n的引用赋值40 等价于 给n赋值40 }
③ 常引用
const int &r = n;
不能通过常引用去修改其引用的内容
1 2 3 4
int n = 100; constint &r = n; r = 50; //wrong n = 49; //right n的引用r的值也随之改变
三、内联函数
声明时使用关键字 inline
编译时在调用处用函数进行替换,节省了参数传递、控制转移等开销
注意:内联函数体内不能有循环语句和 switch 语句
内联函数的调用过程:
1 2 3 4 5
inlineintMax(int a, int b){ if(a>b) return a; return b; }
doublepower(double x, int n); //x的n次方 intmain(){ int value = 0; cout<<"please enter 8 bit binary number:"; for(int i = 7; i>=0; i--){ //从高位往低位读入二进制数 char ch; cin>>ch; //以字符形式读取 if(ch == '1') //强转 value += static_cast<int>(power(2,i)); //value += (int)(power(2,i)); } cout<<"decimal value is "<<value<<endl; return0; } doublepower(double x, int n){ double sum = 1.0; while(n--) sum = sum*x; return sum; }
doublearctanx(double x) { double sqr = x * x; //x每次都乘以x2 double e = x; //保存x的次方,分子 double sum = 0; //保存最终结果 int i = 1; //分母 int flag = 1; while (e / i > 1e-15) { double f = flag * e / i; sum += f; e = e * sqr; i += 2; flag = -flag; } return sum; } intmain() { //注意:因为整数相除结果取整,如果参数写1/5,1/239,结果就都是0 double a = 16.0 * arctanx(1 / 5.0); double b = 4.0 * arctanx(1 / 239.0); cout << "PI = " << a - b << endl; return0; }