标签:引用类型 erro 语句 string 变量 cst 开始 blog chap
6.1
形参:与实参有联系的变量,可以是实参的拷贝、引用等。
实参:传递给被调用函数的参数,让调用函数中的值可以给被调用函数使用
6.4
1 #include <iostream> 2 #include <vector> 3 #include <cctype> 4 #include <iterator> 5 #include <stdexcept> 6 #include <string> 7 #include <cstring> 8 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::vector; 13 using std::string; 14 using std::runtime_error; 15 16 int fact(int n) 17 { 18 if (n < 1) return -1; 19 int ans = 1; 20 for (int i = n; i > 0; i--) { 21 ans *= i; 22 } 23 return ans; 24 } 25 26 int main() 27 { 28 int n; 29 while(cin >> n) { 30 cout << fact(n) << endl; 31 } 32 return 0; 33 }
6.5
1 #include <iostream> 2 #include <vector> 3 #include <cctype> 4 #include <iterator> 5 #include <stdexcept> 6 #include <string> 7 #include <cstring> 8 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::vector; 13 using std::string; 14 using std::runtime_error; 15 16 int i_abs(int n) 17 { 18 return n > 0 ? n : -n; 19 } 20 21 int main() 22 { 23 int a; 24 while(cin >> a) 25 cout << i_abs(a) << endl; 26 return 0; 27 }
6.6
形参:生命周期和函数一样
局部变量:生命周期自其定义语句开始至定义所在的块末尾
局部静态变量:生命周期自其定义语句开始至程序结束
6.7
1 #include <iostream> 2 #include <vector> 3 #include <cctype> 4 #include <iterator> 5 #include <stdexcept> 6 #include <string> 7 #include <cstring> 8 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::vector; 13 using std::string; 14 using std::runtime_error; 15 16 int coco() 17 { 18 static int flag = 0; 19 if (!flag) return flag++; 20 return 1; //flag的值不是0时执行此语句 21 } 22 23 int main() 24 { 25 for (int i = 1; i <= 10; i++) 26 cout << coco() << endl; 27 return 0; 28 }
6.8
1 int fact(int n);
6.9
1 #include <iostream> 2 #include "Chapter6.h" 3 4 int fact(int n) 5 { 6 int ans = 1; 7 for (int i = n; i != 0; i--) 8 ans *= i; 9 return ans; 10 }
1 #include <iostream> 2 #include "fact.cpp" 3 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int n; 11 while(cin >> n) 12 cout << fact(n) << endl; 13 return 0; 14 }
6.10
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 5 using std::cin; 6 using std::cout; 7 using std::endl; 8 using std::vector; 9 using std::string; 10 11 void swap(int *p1, int *p2) 12 { 13 int tmp; 14 tmp = *p1; 15 *p1 = *p2; //改变P1所指对象的值 16 *p2 = tmp; 17 } 18 19 int main() 20 { 21 int a, b; 22 while (cin >> a >> b) { 23 cout << "交换前:a = " << a << ", b = " << b << endl; 24 swap(&a, &b); 25 cout << "交换后:a = " << a << ", b = " << b << endl; 26 } 27 return 0; 28 }
6.12
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 5 using std::cin; 6 using std::cout; 7 using std::endl; 8 using std::vector; 9 using std::string; 10 11 void swap(int &r1, int &r2) 12 { 13 int tmp; 14 tmp = r1; 15 r1 = r2; //改变P1所绑定对象的值 16 r2 = tmp; 17 } 18 19 int main() 20 { 21 int a, b; 22 while (cin >> a >> b) { 23 cout << "交换前:a = " << a << ", b = " << b << endl; 24 swap(a, b); 25 cout << "交换后:a = " << a << ", b = " << b << endl; 26 } 27 return 0; 28 }
6.13
void f(T):传值
void f(&T):穿引用
6.15
因为s字符串是不能被函数所修改的,所以是常量引用。c可能是一个临时变量,所以不需要使用引用类型,也无需函数加以修改其本身。如果令occurs为常量引用,则输出occurs为0(不能加以修改),而s可能会在程序中被修改。
标签:引用类型 erro 语句 string 变量 cst 开始 blog chap
原文地址:http://www.cnblogs.com/xzxl/p/7658353.html