6.4
#include<iostream> using namespace std; int fact(int val) { int ret = 1; while (val>=1) { ret *= val--; } return ret; } int main() { int j ; cout << "请输入要求阶乘的那个整数:" << endl; cin >> j; cout <<j<<"的阶乘是:"<< fact(j)<< endl; return 1; }
6.5
#include<iostream> using namespace std; double abss(double val) { if (val >= 0) return val; else return -val; } int main() { double j ; cout << "请输入要求绝对值的那个数:" << endl; cin >> j; cout <<j<<"的绝对值是:"<< abss(j)<< endl; return 1; }
#include<iostream> using namespace std; double abss(double val) { return val > 0 ? val : -val; } int main() { double j ; cout << "请输入要求绝对值的那个数:" << endl; cin >> j; cout <<j<<"的绝对值是:"<< abss(j)<< endl; return 1; }
6.7
#include<iostream> using namespace std; int cnt_call() { static int flag = 0; if (!flag) return flag++; else return 1; } int main() { for (int i = 0; i != 4;i++) cout <<cnt_call()<< endl; return 1; }
局部静态变量的值等于上次调用函数退出时它的值。局部静态变量的生命周期从程序第一次执行开始,知道程序终止时结束。
6.10
#include <iostream> #include <string> #include <vector> using namespace std; void sw(int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; cout << *a << *b << endl; } int main() { int i = 4, j = 9; sw(&i, &j); cout << "i: " << i << endl; cout << "j: " << j << endl; return 1; }
#include <iostream> #include <string> #include <vector> using namespace std; void sw(int &a, int &b) { int tmp; tmp = a; a = b; b = tmp; cout << a << b << endl; } int main() { int i = 4, j = 9; sw(i, j); cout << "i: " << i << endl; cout << "j: " << j << endl; return 1; }
引用形参也会改变实参的值
6.17
#include <iostream> #include <string> #include <vector> using namespace std; bool isCapital(const string s) { for (auto it = s.begin(); it != s.end(); ++it) { if (isupper(*it)) { return true; } } return false; } void fun(string &s) { for (auto &i : s) { i = isupper(i) ? tolower(i) : i; } } int main() { string str = "ABCdehijklMNopqrstuvwxyz"; cout << isCapital(str) << endl; fun(str); cout << str << endl; return 1; }
不需要改变实参的值就用常量引用,否则不使用常量引用。尽量使用常量引用,防止编译出错。见p192
6.21
#include <iostream> #include <string> #include <vector> using namespace std; int fun(const int i, const int *p) { return i > *p ? i : *p; } void main() { int a = 5; int b[] = { 0, 1, 2, 3, 4, 5, 6 }; int *c = b; c+=2; cout << fun(a, c) << endl; }
6.22 交换指针而非指针所指对象(交换cd,而不是ab)
#include <iostream> #include <string> #include <vector> using namespace std; void fun( int *&i, int *&j) { int *tmp=NULL; tmp = i; i = j; j = tmp; } void main() { int a = 5; int b = 6; int *c = &a; int *d = &b; fun(c,d); cout << a<<" "<<b<< endl; cout << *c << " " << *d<< endl; }
6.25 使用argv中的实参时,一定要记得可选参数从argv[1]开始,argv[0]保存的是程序的名字。
#include <iostream> #include <string> #include <vector> using namespace std; int main(int argc, char **argv) { string ss; for (int i = 1; i != argc; ++i) { ss += argv[i]; ss += " "; } cout << ss << endl; return 0; }
6.27
#include <iostream> #include <vector> #include <string> using namespace std; int fun(initializer_list<int> il) { int sum = 0; for (auto &i : il) sum += i; return sum; } int main(int argc, char *argv[]) //形参argv是一个数组,它的元素是指向C风格字符串的指针 { cout << fun({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }) << endl; return 0; }
6.33
#include <iostream> #include <string> #include <vector> using namespace std; vector<int> v1 = { 1, 2, 3, 4, 5 }; void fun(vector<int>::iterator it) { if (it != v1.end()) { cout << *it++ << endl; fun(it); } return; } int main() { fun(v1.begin()); return 0; }
6.42
#include <iostream> #include <string> using namespace std; string make_plural(size_t ctr, const string &word, const string &ending="s") { return (ctr > 1) ? word + ending : word; } int main() { string str = "failure"; cout<< make_plural(1, str)<<endl; cout << make_plural(2, str) << endl; }
6.55
#include <iostream> #include<string> #include<vector> using namespace std; int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a*b; } int divi(int a, int b) { return decltype(1.0) (a) / b; } int main(int argc, char** argv) { typedef int(*p)(int a, int b); //声明函数指针,使用typedef的声明语句定义的 //不再是变量, 而是类型别名 vector<p> v1{ add, sub, mul, divi }; for (auto f : v1) cout << f(3, 2) << endl; return 0; }