├── .DS_Store ├── README.md ├── chapt1 ├── .DS_Store ├── 1_01.cpp ├── 1_02.cpp ├── 1_03.cpp ├── 1_04.cpp ├── 1_05.cpp ├── 1_06.cpp ├── 1_07.cpp ├── 1_08.cpp ├── C++编程基础.txt ├── exe1.6.cpp ├── exe1.7.cpp ├── string.txt └── string_sort.txt ├── chapt2 ├── 2_01.cpp ├── 2_02.cpp ├── 2_03.cpp ├── 2_04.cpp ├── 2_05.cpp ├── 2_06.cpp ├── 2_07.cpp ├── 2_08.cpp ├── 2_09.cpp └── 面向过程的编码风格.txt ├── chapt3 ├── 3_01.cpp ├── 3_02.cpp ├── 3_03.cpp ├── 3_04.cpp ├── 3_05.cpp ├── 3_06.cpp ├── 3_07.cpp ├── 3_08.cpp ├── input.txt ├── output.txt └── 范型编程风格.txt ├── chapt4 ├── 4_01 │ ├── Stack.cpp │ ├── Stack.hpp │ └── main.cpp ├── 4_02 │ ├── Triangular.hpp │ └── main.cpp ├── 4_03 │ ├── Matrix.hpp │ └── main.cpp ├── 4_04 │ ├── LessThan.hpp │ └── main.cpp └── 基于对象的编码风格.txt ├── chapt5 ├── 5_01 │ ├── LibMat.hpp │ └── main.cpp ├── 5_02 │ ├── fibonacci.hpp │ ├── main.cpp │ └── num_sequence.hpp ├── 5_03 │ ├── fibonacci.hpp │ ├── main.cpp │ └── num_sequence.hpp ├── 5_04 │ ├── fibonacci.hpp │ ├── main.cpp │ └── num_sequence.hpp └── 面向对象编码风格.txt ├── chapt6 ├── 6_01.cpp └── 以template进行编程.txt └── chapt7 └── 异常处理Exception Handling.txt /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndyHsu-cn/Essential_CPP/2f74e74ec8df9cac49a3ba49e90379c9e6ff130c/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Essential_CPP 2 | Essential C++第三版代码 3 | -------------------------------------------------------------------------------- /chapt1/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AndyHsu-cn/Essential_CPP/2f74e74ec8df9cac49a3ba49e90379c9e6ff130c/chapt1/.DS_Store -------------------------------------------------------------------------------- /chapt1/1_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string user_name; 8 | 9 | cout << "Please enter your first name: "; 10 | cin >> user_name; 11 | cout << "\nHello, " << "Hello, " << user_name << " ... and goodbye!\n"; 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /chapt1/1_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(void) 6 | { 7 | string user_name; 8 | int user_guess; 9 | int num_tries; // 总的猜测数 10 | int num_right; // 总的正确数 11 | double user_score = 0.0; 12 | char user_rsp; 13 | char try_again; 14 | bool next_seq = true; // 显示下一组数列 15 | bool go_for_it = true; // 用户想继续猜一次 16 | bool got_it = false; // 用户是否猜对 17 | const int max_tries = 3; 18 | 19 | const int seq_size = 18; 20 | const int max_seq = 6; 21 | int elem_seq[seq_size] = { 22 | 1, 2, 3, // Fibonacci 23 | 3, 4, 7, // Lucas 24 | 2, 5, 12, // Pell 25 | 3, 6, 10, // Triangular 26 | 4, 9, 16, // Square 27 | 5, 12, 22 // Pentagonal 28 | }; 29 | string seq_names[max_seq] = { 30 | "Fibonacci", 31 | "Lucas", 32 | "Pell", 33 | "Triangular", 34 | "Square", 35 | "Pentagonal" 36 | }; 37 | int cur_index = 0; 38 | 39 | 40 | // 用户想要继续猜某个数列 41 | while (next_seq == true && cur_index < seq_size) 42 | { 43 | // 显示数列的前两个数字 44 | std::cout << "The first 2 elements of the sequence are: " 45 | << elem_seq[cur_index] << ", " << elem_seq[cur_index+1] << endl; 46 | std::cout << "What is the next element?"; 47 | int tries_cnt = 0; 48 | next_seq = true; 49 | go_for_it = true; 50 | got_it = false; 51 | // 用户所猜不正确 && 用户想要再猜一次 52 | while(!got_it && go_for_it && (++tries_cnt <= max_tries)) 53 | { 54 | std::cin >> user_guess; 55 | ++num_tries; 56 | // 如果答案正确 57 | if(user_guess == elem_seq[cur_index+2]) 58 | { 59 | ++num_right; 60 | std::cout << "Very good, yes, " << elem_seq[cur_index+2] 61 | << " is the next element in the " 62 | << seq_names[cur_index/3] << " sequence." << endl; 63 | got_it = true; 64 | }else{ 65 | // 如果答案错误 66 | switch (tries_cnt) 67 | { 68 | case 1: 69 | std::cout << "Oops, Nice guess but not quite it." << endl; 70 | break; 71 | case 2: 72 | std::cout << "Sorry, Wrong a second time!" << endl; 73 | break; 74 | case 3: 75 | std::cout << "Ah, this is harder than it looks, isn't it?" << endl; 76 | break; 77 | default: 78 | std::cout << "It must be getting pretty frustrating by now!!!" << endl; 79 | break; 80 | } 81 | 82 | std::cout << "do you want to continue?(y/n):" << endl; 83 | std::cin >> user_rsp; 84 | if(user_rsp == 'N' || user_rsp == 'n') 85 | go_for_it = false; 86 | } 87 | } // 内层while结束 88 | std::cout << "Want to try another sequence? (y/n)?" << endl; 89 | std::cin >> try_again; 90 | if(try_again == 'N' || try_again == 'n') 91 | next_seq = false; 92 | else 93 | cur_index += 3; 94 | } // 外层while结束 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /chapt1/1_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | const int seq_size = 18; 8 | vector pell_seq(seq_size); 9 | pell_seq[0] = 1; 10 | pell_seq[1] = 2; 11 | for (int index = 2; index < seq_size; index++) 12 | pell_seq[index] = 2 * pell_seq[index-1] + pell_seq[index-2]; 13 | 14 | cout << "The first " << pell_seq.size() << " elements of the Pell series:" << endl; 15 | for (int index = 0; index < seq_size; index++) 16 | cout << pell_seq[index] << " "; 17 | cout << endl; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /chapt1/1_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main(void) 9 | { 10 | string user_name; 11 | int user_guess; 12 | int num_tries; // 总的猜测数 13 | int num_right; // 总的正确数 14 | double user_score = 0.0; 15 | char user_rsp; 16 | char try_again; 17 | bool next_seq = true; // 显示下一组数列 18 | bool go_for_it = true; // 用户想继续猜一次 19 | bool got_it = false; // 用户是否猜对 20 | const int max_tries = 3; 21 | 22 | const int seq_size = 18; 23 | int elem_seq[seq_size] = { 24 | 1, 2, 3, // Fibonacci 25 | 3, 4, 7, // Lucas 26 | 2, 5, 12, // Pell 27 | 3, 6, 10, // Triangular 28 | 4, 9, 16, // Square 29 | 5, 12, 22 // Pentagonal 30 | }; 31 | vector fibonacci(elem_seq, elem_seq+3); 32 | vector Lucas(elem_seq+3, elem_seq+6); 33 | vector Pell(elem_seq+6, elem_seq+9); 34 | vector Triangular(elem_seq+9, elem_seq+12); 35 | vector Square(elem_seq+12, elem_seq+15); 36 | vector Pentagonal(elem_seq+15, elem_seq+18); 37 | 38 | const int max_seq = 6; 39 | string seq_names[max_seq] = { 40 | "Fibonacci", 41 | "Lucas", 42 | "Pell", 43 | "Triangular", 44 | "Square", 45 | "Pentagonal" 46 | }; 47 | vector * seq_addrs[max_seq] = { 48 | &fibonacci, &Lucas, &Pell, 49 | &Triangular, &Square, &Pentagonal 50 | }; 51 | 52 | vector *current_vec = 0; 53 | int seq_index; 54 | srand(time(NULL)); 55 | 56 | // 用户想要继续猜某个数列 57 | while (next_seq == true) 58 | { 59 | seq_index = rand() % max_seq; 60 | current_vec = seq_addrs[seq_index]; 61 | std::cout << "The first 2 elements of the sequence are: " 62 | << (*current_vec)[0] << ", " << (*current_vec)[1] << endl; 63 | std::cout << "What is the next element?"; 64 | int tries_cnt = 0; 65 | next_seq = true; 66 | go_for_it = true; 67 | got_it = false; 68 | // 用户所猜不正确 && 用户想要再猜一次 69 | while(!got_it && go_for_it && (++tries_cnt <= max_tries)) 70 | { 71 | std::cin >> user_guess; 72 | ++num_tries; 73 | // 如果答案正确 74 | if(user_guess == (*current_vec)[2]) 75 | { 76 | ++num_right; 77 | std::cout << "Very good, yes, " << (*current_vec)[2] 78 | << " is the next element in the " 79 | << seq_names[seq_index] << " sequence." << endl; 80 | got_it = true; 81 | }else{ 82 | // 如果答案错误 83 | switch (tries_cnt) 84 | { 85 | case 1: 86 | std::cout << "Oops, Nice guess but not quite it." << endl; 87 | break; 88 | case 2: 89 | std::cout << "Sorry, Wrong a second time!" << endl; 90 | break; 91 | case 3: 92 | std::cout << "Ah, this is harder than it looks, isn't it?" << endl; 93 | break; 94 | default: 95 | std::cout << "It must be getting pretty frustrating by now!!!" << endl; 96 | break; 97 | } 98 | 99 | std::cout << "do you want to continue?(y/n):" << endl; 100 | std::cin >> user_rsp; 101 | if(user_rsp == 'N' || user_rsp == 'n') 102 | go_for_it = false; 103 | } 104 | } // 内层while结束 105 | std::cout << "Want to try another sequence? (y/n)?" << endl; 106 | std::cin >> try_again; 107 | if(try_again == 'N' || try_again == 'n') 108 | next_seq = false; 109 | } // 外层while结束 110 | 111 | return 0; 112 | } -------------------------------------------------------------------------------- /chapt1/1_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string name; 8 | int num_tries; 9 | int num_rights; 10 | ofstream outfile("1_05.txt", ios_base::app); 11 | 12 | cout << "Enter your name: "; 13 | cin >> name; 14 | cout << "Enter your num_tries, num_rights: "; 15 | cin >> num_tries >>num_rights; 16 | if (!outfile) 17 | { 18 | cerr << "Oops, unable to save session data!" << endl; 19 | }else 20 | { 21 | outfile << name << ' ' 22 | << num_tries << ' ' 23 | << num_rights << ' ' << endl; 24 | } 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /chapt1/1_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string name; 8 | int num_tries; 9 | int num_rights; 10 | ifstream infile("1_05.txt"); 11 | 12 | if (!infile) 13 | { 14 | cerr << "Oops, unable to open file!" << endl; 15 | }else 16 | { 17 | infile >> name >> num_tries >> num_rights; 18 | cout << "name: " << name << endl; 19 | cout << "tot: " << num_tries << endl; 20 | cout << "right: " << num_rights << endl; 21 | } 22 | 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /chapt1/1_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string name; 8 | int num_tries; 9 | int num_rights; 10 | 11 | fstream iofile("data.txt", ios_base::in | ios_base::app); 12 | if(!iofile) 13 | { 14 | cerr << "Oops, unable to open file!" << endl; 15 | 16 | }else 17 | { 18 | iofile << "andy " << 10 << ' ' << 7 << endl; 19 | cout << "______________________" << endl; 20 | string usr_name; 21 | int num_tries = 0; 22 | int num_rights = 0; 23 | 24 | // 由于ios_base::app的原因,开始读取之前,请将文件重新定位到起始处 25 | iofile.seekg(0); 26 | iofile >> usr_name >> num_tries >> num_rights; 27 | cout << "name: " << usr_name << endl; 28 | cout << "tot: " << num_tries << endl; 29 | cout << "right: " << num_rights << endl; 30 | } 31 | 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /chapt1/1_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main(void) 10 | { 11 | string user_name; 12 | int user_guess; 13 | int num_tries; // 总的猜测数 14 | int num_right; // 总的正确数 15 | double user_score = 0.0; 16 | char user_rsp; 17 | char try_again; 18 | bool next_seq = true; // 显示下一组数列 19 | bool go_for_it = true; // 用户想继续猜一次 20 | bool got_it = false; // 用户是否猜对 21 | const int max_tries = 3; 22 | 23 | const int seq_size = 18; 24 | int elem_seq[seq_size] = { 25 | 1, 2, 3, // Fibonacci 26 | 3, 4, 7, // Lucas 27 | 2, 5, 12, // Pell 28 | 3, 6, 10, // Triangular 29 | 4, 9, 16, // Square 30 | 5, 12, 22 // Pentagonal 31 | }; 32 | vector fibonacci(elem_seq, elem_seq+3); 33 | vector Lucas(elem_seq+3, elem_seq+6); 34 | vector Pell(elem_seq+6, elem_seq+9); 35 | vector Triangular(elem_seq+9, elem_seq+12); 36 | vector Square(elem_seq+12, elem_seq+15); 37 | vector Pentagonal(elem_seq+15, elem_seq+18); 38 | 39 | const int max_seq = 6; 40 | string seq_names[max_seq] = { 41 | "Fibonacci", 42 | "Lucas", 43 | "Pell", 44 | "Triangular", 45 | "Square", 46 | "Pentagonal" 47 | }; 48 | vector * seq_addrs[max_seq] = { 49 | &fibonacci, &Lucas, &Pell, 50 | &Triangular, &Square, &Pentagonal 51 | }; 52 | 53 | vector *current_vec = 0; 54 | int seq_index; 55 | 56 | srand(time(NULL)); 57 | cout << "Enter your name: "; 58 | cin >> user_name; 59 | ifstream infile("seq_data.txt"); 60 | if(!infile) 61 | cerr << "Oops, unable to open file!" << endl; 62 | else 63 | { 64 | string name; 65 | int nt; // 猜的总次数 66 | int nc; // 猜对的次数 67 | int find = 0; 68 | while (infile >> name) 69 | { 70 | infile >> nt >> nc; 71 | if(name == user_name) 72 | { 73 | find = 1; 74 | num_tries = nt; 75 | num_right = nc; 76 | } 77 | } 78 | if(find == 1){ 79 | cout << "Welcome back, " << user_name 80 | << "TOT[" << nt << "]" 81 | << "RIGHT[" << nc << "]" 82 | << "\nGood, Luck!" << endl; 83 | }else 84 | cout << "Welcome, freshman " << user_name << endl; 85 | } 86 | 87 | // 用户想要继续猜某个数列 88 | while (next_seq == true) 89 | { 90 | seq_index = rand() % max_seq; 91 | current_vec = seq_addrs[seq_index]; 92 | std::cout << "The first 2 elements of the sequence are: " 93 | << (*current_vec)[0] << ", " << (*current_vec)[1] << endl; 94 | std::cout << "What is the next element?"; 95 | int tries_cnt = 0; 96 | next_seq = true; 97 | go_for_it = true; 98 | got_it = false; 99 | // 用户所猜不正确 && 用户想要再猜一次 100 | while(!got_it && go_for_it && (++tries_cnt <= max_tries)) 101 | { 102 | std::cin >> user_guess; 103 | ++num_tries; 104 | // 如果答案正确 105 | if(user_guess == (*current_vec)[2]) 106 | { 107 | ++num_right; 108 | std::cout << "Very good, yes, " << (*current_vec)[2] 109 | << " is the next element in the " 110 | << seq_names[seq_index] << " sequence." << endl; 111 | got_it = true; 112 | }else{ 113 | // 如果答案错误 114 | switch (tries_cnt) 115 | { 116 | case 1: 117 | std::cout << "Oops, Nice guess but not quite it." << endl; 118 | break; 119 | case 2: 120 | std::cout << "Sorry, Wrong a second time!" << endl; 121 | break; 122 | case 3: 123 | std::cout << "Ah, this is harder than it looks, isn't it?" << endl; 124 | break; 125 | default: 126 | std::cout << "It must be getting pretty frustrating by now!!!" << endl; 127 | break; 128 | } 129 | 130 | std::cout << "do you want to continue?(y/n):" << endl; 131 | std::cin >> user_rsp; 132 | if(user_rsp == 'N' || user_rsp == 'n') 133 | go_for_it = false; 134 | } 135 | } // 内层while结束 136 | std::cout << "Want to try another sequence? (y/n)?" << endl; 137 | std::cin >> try_again; 138 | if(try_again == 'N' || try_again == 'n') 139 | next_seq = false; 140 | } // 外层while结束 141 | 142 | ofstream outfile("seq_data.txt", ios_base::app); 143 | if(!outfile) 144 | cerr << "Oops, unable to open file!" << endl; 145 | else 146 | { 147 | outfile << user_name << ' ' 148 | << num_tries << ' ' 149 | << num_right << endl; 150 | } 151 | 152 | 153 | return 0; 154 | } -------------------------------------------------------------------------------- /chapt1/C++编程基础.txt: -------------------------------------------------------------------------------- 1 | p205 附录A 2 | p255 附录B 3 | 4 | 01 什么是class 5 | 用户自定义的数据类型(user-defined data type),增强类型抽象化的层次 6 | class定义应该分为两部分 7 | (1)头文件(header file)——用来声明该class的各种操作行为 8 | (2)代码文件(program text)——包含了操作行为的具体实现 9 | 10 | 02 对象初始化方式 11 | (1)使用=运算符,如int num_tries = 0 12 | 源自C语言 13 | (2)构造函数语法,如int num_right(0) 14 | 解决“多值初始化”问题 15 | 使内置类型与class类型的初始化方式得到统一 16 | 17 | 03 vector的定义 18 | (1)包含vector头文件 19 | (2)类名后尖括号内指定元素类型,vector的大小写在变量名后的小括号内, 20 | 如vector vec_name(vec_size); 21 | 22 | 04 文件读写 23 | (0)包含头文件fstream 24 | (1)若写文件,则创建ofstream对象 25 | 如ofstream outfile("filename");则以覆盖的方式写文件 26 | 若ofstream outfile("filename", ios_base::app);则以追加的方式写文件 27 | (2)若读文件,则创建ifstream对象 28 | 如ifstream infile("filename"); -------------------------------------------------------------------------------- /chapt1/exe1.6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(void) 6 | { 7 | vector ivec; 8 | int ival; 9 | int sum = 0; 10 | 11 | while(cin >> ival) 12 | ivec.push_back(ival); 13 | 14 | for (int index = 0; index < ivec.size(); index++) 15 | sum += ivec[index]; 16 | cout << "sum = " << sum << endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /chapt1/exe1.7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main(void) 9 | { 10 | ifstream infile("string.txt"); 11 | ofstream outfile("string_sort.txt"); 12 | if(!infile || !outfile) 13 | cerr << "Unable to open file" << endl; 14 | else 15 | { 16 | string words; 17 | vector text_vec; 18 | while(infile >> words) 19 | text_vec.push_back(words); 20 | int index = 0; 21 | cout << "Unsorted text: " << endl; 22 | for(index = 0; index < text_vec.size(); index++) 23 | cout < 2 | using namespace std; 3 | 4 | bool fibon_elem(int pos, int &elem); 5 | 6 | int main() 7 | { 8 | int pos; 9 | int elem; 10 | 11 | cout << "Please enter a position: "; 12 | cin >> pos; 13 | if(fibon_elem(pos, elem)) 14 | { 15 | cout << "element # " << pos << " : " << elem << endl; 16 | }else 17 | cout << "Sorry, could not calculate element # " << pos << endl; 18 | return 0; 19 | } 20 | 21 | bool fibon_elem(int pos, int &elem) 22 | { 23 | if(pos<=0 || pos>1024) 24 | { 25 | elem = 0; 26 | return false; 27 | } 28 | elem = 1; 29 | int n1 = 1; 30 | int n2 = 1; 31 | for (int index = 3; index <= pos; index++) 32 | { 33 | elem = n1 + n2; 34 | n1 = n2; 35 | n2 = elem; 36 | } 37 | return true; 38 | } -------------------------------------------------------------------------------- /chapt2/2_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool fibon_elem(int pos, int &elem); 5 | bool print_fibon(int pos); 6 | int main() 7 | { 8 | int pos; 9 | int elem; 10 | 11 | cout << "Please enter a position: "; 12 | while(cin >> pos) 13 | { 14 | if(fibon_elem(pos, elem)) 15 | { 16 | cout << "element # " << pos << " : " << elem << endl; 17 | print_fibon(pos); 18 | }else 19 | cout << "Sorry, could not calculate element # " << pos << endl; 20 | cout << "Please enter another position: "; 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | bool fibon_elem(int pos, int &elem) 27 | { 28 | if(pos<=0 || pos>1024) 29 | { 30 | elem = 0; 31 | return false; 32 | } 33 | elem = 1; 34 | int n1 = 1; 35 | int n2 = 1; 36 | for (int index = 3; index <= pos; index++) 37 | { 38 | elem = n1 + n2; 39 | n1 = n2; 40 | n2 = elem; 41 | } 42 | return true; 43 | } 44 | 45 | bool print_fibon(int pos) 46 | { 47 | if(pos<=0 || pos>1024) 48 | { 49 | cerr << "Invalid position: " << pos 50 | << " -- cannot handle request!" << endl; 51 | return false; 52 | } 53 | cout << "The Fibonacci Sequence for " << pos << " positions: " << endl; 54 | switch (pos) 55 | { 56 | default: 57 | case 2: 58 | case 1: 59 | cout << "1 "; 60 | break; 61 | } 62 | int elem; 63 | int n1 = 1; 64 | int n2 = 1; 65 | for (int index = 3; index <= pos; index++) 66 | { 67 | elem = n1 + n2; 68 | n1 = n2; 69 | n2 = elem; 70 | cout << elem << (!(index % 10) ? "\n" : " "); 71 | } 72 | cout << endl; 73 | return true; 74 | } -------------------------------------------------------------------------------- /chapt2/2_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void display(vector vec); 6 | void bubble_sort(vector vec); 7 | void swap(int val1, int val2); 8 | 9 | int main() 10 | { 11 | int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2}; 12 | vector vec(ia, ia+8); 13 | cout << "vector before sort: "; 14 | display(vec); 15 | 16 | bubble_sort(vec); 17 | cout << "vector after sort: "; 18 | display(vec); 19 | return 0; 20 | } 21 | 22 | 23 | void display(vector vec) 24 | { 25 | for (int index = 0; index < vec.size(); index++) 26 | cout << vec[index] << " "; 27 | cout << endl; 28 | } 29 | 30 | void bubble_sort(vector vec) 31 | { 32 | for(int ix = 0; ix < vec.size(); ix++) 33 | for(int jx = ix+1; jx < vec.size(); jx++) 34 | if(vec[ix] > vec[jx]) 35 | swap(vec[ix], vec[jx]); 36 | } 37 | 38 | void swap(int val1, int val2) 39 | { 40 | int temp = val1; 41 | val1 = val2; 42 | val2 = temp; 43 | } -------------------------------------------------------------------------------- /chapt2/2_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void display(vector & vec); 6 | void bubble_sort(const vector & vec); 7 | void swap(int & val1, int & val2); 8 | 9 | int main() 10 | { 11 | int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2}; 12 | vector vec(ia, ia+8); 13 | cout << "vector before sort: "; 14 | display(vec); 15 | 16 | bubble_sort(vec); 17 | cout << "vector after sort: "; 18 | display(vec); 19 | return 0; 20 | } 21 | 22 | 23 | void display(const vector & vec) 24 | { 25 | for (int index = 0; index < vec.size(); index++) 26 | cout << vec[index] << " "; 27 | cout << endl; 28 | } 29 | 30 | void bubble_sort(vector & vec) 31 | { 32 | for(int ix = 0; ix < vec.size(); ix++) 33 | for(int jx = ix+1; jx < vec.size(); jx++) 34 | if(vec[ix] > vec[jx]) 35 | swap(vec[ix], vec[jx]); 36 | } 37 | 38 | void swap(int & val1, int & val2) 39 | { 40 | int temp = val1; 41 | val1 = val2; 42 | val2 = temp; 43 | } -------------------------------------------------------------------------------- /chapt2/2_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void display(const vector * vec); 6 | void bubble_sort(vector * vec); 7 | void swap(int * val1, int * val2); 8 | 9 | int main() 10 | { 11 | int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2}; 12 | vector vec(ia, ia+8); 13 | cout << "vector before sort: "; 14 | display(&vec); 15 | 16 | bubble_sort(&vec); 17 | cout << "vector after sort: "; 18 | display(&vec); 19 | return 0; 20 | } 21 | 22 | 23 | void display(const vector * vec) 24 | { 25 | for (int index = 0; index < vec->size(); index++) 26 | cout << (*vec)[index] << " "; 27 | cout << endl; 28 | } 29 | 30 | void bubble_sort(vector * vec) 31 | { 32 | for(int ix = 0; ix < vec->size(); ix++) 33 | for(int jx = ix+1; jx < vec->size(); jx++) 34 | if((*vec)[ix] > (*vec)[jx]) 35 | swap((*vec)[ix], (*vec)[jx]); 36 | } 37 | 38 | void swap(int * val1, int * val2) 39 | { 40 | int temp = *val1; 41 | *val1 = *val2; 42 | *val2 = temp; 43 | } -------------------------------------------------------------------------------- /chapt2/2_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void display(const vector * vec, ostream & os = cout); 7 | void bubble_sort(vector * vec, ofstream * ofil = 0); 8 | void swap(int * val1, int * val2); 9 | 10 | int main() 11 | { 12 | int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2}; 13 | vector vec(ia, ia+8); 14 | ofstream ofil("data.txt"); 15 | cout << "vector before sort: "; 16 | display(&vec, ofil); 17 | 18 | cout << "vector after sort: "; 19 | bubble_sort(&vec, &ofil); 20 | display(&vec); 21 | return 0; 22 | } 23 | 24 | 25 | void display(const vector * vec, ostream & os) 26 | { 27 | for (int index = 0; index < vec->size(); index++) 28 | os << (*vec)[index] << " "; 29 | os << endl; 30 | } 31 | 32 | void bubble_sort(vector * vec, ofstream * ofil) 33 | { 34 | for(int ix = 0; ix < vec->size()-1; ix++) 35 | { 36 | for(int jx = ix+1; jx < vec->size(); jx++) 37 | { 38 | if((*vec)[ix] > (*vec)[jx]) 39 | swap((*vec)[ix], (*vec)[jx]); 40 | } 41 | if(ofil != 0) 42 | display(vec, *ofil); 43 | } 44 | } 45 | 46 | void swap(int * val1, int * val2) 47 | { 48 | int temp = *val1; 49 | *val1 = *val2; 50 | *val2 = temp; 51 | } -------------------------------------------------------------------------------- /chapt2/2_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | vector * fibon_seq(int pos, istream &is , ostream &os); 7 | 8 | int main() 9 | { 10 | int pos; 11 | ifstream infile("fibon_seq.txt"); 12 | ofstream outfile("fibon_seq.txt", ios_base::app); 13 | 14 | cout << "Please enter a position: "; 15 | while(cin >> pos) 16 | { 17 | if(fibon_seq(pos, infile, outfile)) 18 | { 19 | 20 | 21 | }else 22 | cout << "Sorry, could not calculate element # " << pos << endl; 23 | cout << "Please enter another position: "; 24 | } 25 | 26 | return 0; 27 | } 28 | 29 | vector * fibon_seq(int pos, istream &is, ostream & os) 30 | { 31 | static vector elems; 32 | if(pos<=0 || pos>1024) 33 | { 34 | return NULL; 35 | } 36 | 37 | for(int ix = elems.size(); ix < pos; ++ix) 38 | { 39 | if(ix==0 || ix==1) 40 | { 41 | elems.push_back(1); 42 | os << 1 << endl; 43 | } 44 | else 45 | { 46 | elems.push_back(elems[ix-1] + elems[ix-2]); 47 | os << elems[ix-1] + elems[ix-2] << endl; 48 | } 49 | } 50 | cout << endl; 51 | return &elems; 52 | } -------------------------------------------------------------------------------- /chapt2/2_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | template 6 | void display_message(const string & msg, const vector &vec) 7 | { 8 | cout << msg << endl; 9 | for (int index = 0; index < vec.size(); index++) 10 | cout << vec[index] << endl; 11 | } 12 | 13 | int main(void) 14 | { 15 | string msg = "Hello, andy"; 16 | vector ivec; 17 | vector svec; 18 | 19 | ivec.push_back(1); 20 | ivec.push_back(2); 21 | svec.push_back("bnu"); 22 | svec.push_back("hsu"); 23 | 24 | display_message(msg, ivec); 25 | display_message(msg, svec); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /chapt2/2_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int add(int a, int b) 5 | { 6 | return a + b; 7 | } 8 | 9 | int substract(int a, int b) 10 | { 11 | return a - b; 12 | } 13 | 14 | int main() 15 | { 16 | int x = 5, y = 3; 17 | int (*pf)(int, int); 18 | pf = add; 19 | cout << "pf point to add: " << pf(x, y) << endl; 20 | 21 | pf = substract; 22 | pf(x, y); 23 | cout << "pf point to substract: " << pf(x, y) << endl; 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /chapt2/面向过程的编码风格.txt: -------------------------------------------------------------------------------- 1 | 01 return作用 2 | (1)用在有返回值的函数中,向主调函数返回一个值 3 | (2)用在无返回值的函数中,提交结束函数的执行 4 | 5 | 02 pass by refernece 6 | (1)对象本身并不会被复制一份,复制的是对象的地址 7 | (2)如果希望对所传入的对象尽心修改,要将参数声明为reference 8 | (3)声明为reference的形参加上const时,为的是程序效率的问题,而不是要在函数中修改将要 9 | 传来的实参 10 | (4)传递内置类型用reference,传递class用pointer 11 | 12 | 03 动态内存分配 13 | 特点:(1)在heap堆上分配 (2)由程序员自己分配,自己释放 14 | 应用:(1)new申请,delete释放 (2)C风格的动态内存管理(malloc(), free()) 15 | 16 | 04 默认参数值规则 17 | (1)如果我们为某个参数提供了默认值,则这一参数右侧所有参数必须有默认值 18 | (2)默认值在声明和定义处只能指定一次【推荐在声明处指定】 19 | 20 | 05 函数模版(function template) 21 | 将参数列表的部分(或全部)参数的类型信息抽离出来 22 | 具体的类型信息在采用function template具体实例时指定 23 | 24 | 06 const object 25 | (1)"一次定义"对const object失效 26 | 27 | -------------------------------------------------------------------------------- /chapt3/3_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int arr[4] = {1, 3, 5, 7}; 9 | vector svec; 10 | const vector ivec(arr, arr + 4); 11 | svec.push_back("andy"); 12 | svec.push_back("bnu"); 13 | svec.push_back("c++"); 14 | 15 | vector::iterator it_first = svec.begin(); 16 | vector::const_iterator ivec_first = ivec.begin(); 17 | while(it_first != svec.end()) 18 | { 19 | cout << *(it_first) << endl; 20 | it_first++; 21 | } 22 | 23 | cout << endl; 24 | 25 | while(ivec_first != ivec.end()) 26 | { 27 | cout << *(ivec_first) << endl; 28 | ivec_first++; 29 | } 30 | 31 | 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /chapt3/3_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | template 8 | ItType find(ItType first, ItType last, const elemType & value) 9 | { 10 | for(; first != last; ++first) 11 | { 12 | if(*first == value) 13 | return first; 14 | } 15 | return last; 16 | } 17 | 18 | int main() 19 | { 20 | int array[8] = {1, 1, 2, 3, 5, 8, 13, 21}; 21 | vector ivec(array, array + 8); 22 | list ilist(array, array + 8); 23 | 24 | int * p = find(array, array + 8, 5); 25 | if(p != array + 8) 26 | { 27 | // 找到了 28 | cout << "find at: " << hex << p << endl; 29 | }else 30 | cout << "Not find" << endl; 31 | 32 | vector::iterator iter; 33 | iter = find(ivec.begin(), ivec.end(), 5); 34 | if(iter != ivec.end()) 35 | cout << "Find" << endl; 36 | else 37 | cout << "Not find" << endl; 38 | 39 | list::iterator it_list; 40 | it_list = find(ilist.begin(), ilist.end(), 8); 41 | if(it_list != ilist.end()) 42 | cout << "Find" << endl; 43 | else 44 | cout << "Not find" << endl; 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /chapt3/3_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | bool grow_vec(vector & vec, int elem) 7 | { 8 | while(vec.back() < elem) 9 | { 10 | vec.push_back(vec.back() + vec[vec.size() - 2]); 11 | } 12 | if(vec.back() == elem) 13 | return true; 14 | else 15 | return false; 16 | } 17 | 18 | bool is_elem( vector & vec, int elem) 19 | { 20 | // int max_value = *max_element(vec.begin(), vec.end()); 21 | int max_value = vec.empty() ? 0: vec.back(); 22 | if(max_value < elem) 23 | return grow_vec(vec, elem); 24 | else if(max_value == elem) 25 | return true; 26 | else 27 | return binary_search(vec.begin(), vec.end(), elem); 28 | } 29 | 30 | int main(void) 31 | { 32 | int arr[6] = {1, 1, 2, 3, 5, 8}; 33 | vector seq_vec(arr, arr+6); 34 | cout << "is_elem(seq_vec, 3): " << is_elem(seq_vec, 3) << endl; 35 | cout << "is_elem(seq_vec, 4): " << is_elem(seq_vec, 4) << endl; 36 | cout << "is_elem(seq_vec, 21): " << is_elem(seq_vec, 21) << endl; 37 | cout << "is_elem(seq_vec, 22): " << is_elem(seq_vec, 22) << endl; 38 | cout << "last number of seq: " << seq_vec.back() << endl; 39 | return 0; 40 | } -------------------------------------------------------------------------------- /chapt3/3_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main( ) 5 | { 6 | const int seq_size = 18; 7 | int elem_seq[seq_size] = { 8 | 1, 2, 3, // Fibonacci 9 | 3, 4, 7, // Lucas 10 | 2, 5, 12, // Pell 11 | 3, 6, 10, // Triangular 12 | 4, 9, 16, // Square 13 | 5, 12, 22 // Pentagonal 14 | }; 15 | vector fibonacci(elem_seq, elem_seq+3); 16 | vector Pell(elem_seq+6, elem_seq+9); 17 | vector fib_plus_pell(3); 18 | 19 | transform(fibonacci.begin(), fibonacci.end(), 20 | Pell.begin(), fib_plus_pell.begin(), plus()); 21 | 22 | for (int index = 0; index < fib_plus_pell.size(); index++) 23 | cout << fib_plus_pell[index] << " "; 24 | cout << endl; 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /chapt3/3_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | map words; 10 | map::iterator it; 11 | ofstream out_file("data.txt"); 12 | string tword; 13 | while (cin >> tword) 14 | { 15 | words[tword]++; 16 | } 17 | 18 | if(!out_file) 19 | cerr << "Unable to use data.txt" << endl; 20 | else 21 | { 22 | for( it = words.begin(); it != words.end(); it++) 23 | out_file << it->first << "[" << it->second << "]" << endl; 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /chapt3/3_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | string bad_words[] = { "cat", "apple", "banana", "dog"}; 11 | map words; 12 | map::iterator it; 13 | set word_exclusion(bad_words, bad_words+4); 14 | set::iterator it_set; 15 | ofstream out_file("data.txt"); 16 | string tword; 17 | 18 | // 测试set集合 19 | for( it_set = word_exclusion.begin(); it_set != word_exclusion.end(); it_set++) 20 | cout << *it_set << endl; 21 | 22 | while (cin >> tword) 23 | { 24 | if(word_exclusion.count(tword)) 25 | continue; 26 | words[tword]++; 27 | } 28 | 29 | if(!out_file) 30 | cerr << "Unable to use data.txt" << endl; 31 | else 32 | { 33 | for( it = words.begin(); it != words.end(); it++) 34 | out_file << it->first << "[" << it->second << "]" << endl; 35 | } 36 | return 0; 37 | } -------------------------------------------------------------------------------- /chapt3/3_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main(void) 8 | { 9 | string word; 10 | vector text; 11 | 12 | while(cin >> word) 13 | text.push_back(word); 14 | for (int index = 0; index < text.size(); index++) 15 | cout << text[index] << endl; 16 | cout << "-----------------------" << endl; 17 | 18 | sort(text.begin(), text.end()); 19 | for (int index = 0; index < text.size(); index++) 20 | cout << text[index] << endl; 21 | cout << "-----------------------" << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /chapt3/3_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | ifstream in_file("input.txt"); 12 | ofstream out_file("output.txt"); 13 | istream_iterator is(in_file); 14 | istream_iterator eof; 15 | ostream_iterator os(out_file, "\n"); 16 | vector text; 17 | 18 | if(!in_file || !out_file) 19 | { 20 | cerr << "Unable to use files" << endl; 21 | return -1; 22 | } 23 | copy(is, eof, back_inserter(text)); 24 | sort(text.begin(), text.end()); 25 | 26 | copy(text.begin(), text.end(), os); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /chapt3/input.txt: -------------------------------------------------------------------------------- 1 | andy 2 | apple 3 | CPP 4 | hello 5 | cat -------------------------------------------------------------------------------- /chapt3/output.txt: -------------------------------------------------------------------------------- 1 | CPP 2 | andy 3 | apple 4 | cat 5 | hello 6 | -------------------------------------------------------------------------------- /chapt3/范型编程风格.txt: -------------------------------------------------------------------------------- 1 | 01 STL组织架构图 2 | (1)容器 3 | 1 顺序容器 4 | vector【类似数组】 5 | deque【两端插入、删除效率都很高】 6 | list【类似双向链表】 7 | 2 关联容器 8 | map 9 | set 10 | (2)范型算法[75个] 11 | 实现原理 12 | function template —— 实现与操作对象类型的无关性 13 | iterator - 实现了与容器的无关性 14 | 15 | 02 产生顺序容器 16 | (1)空容器 17 | 如list slist; 18 | (2)特定大小,元素值默认的容器 19 | 如list ilist(10); 20 | (3)特定大小,指定值的容器 21 | 如list ilist(10, -1); 22 | (4)通过一对儿iterator产生容器 23 | 如list ilist(Iterator it1, Iterator it2); 24 | (5)复制其他容器 25 | 如list ilist2(ilist); 26 | 27 | 03 顺序容器的insert()四种变形 28 | (1)iterator insert(iterator pos, elemType value) 29 | (2)void insert(iterator pos, int count, elemType value) 30 | (3)void insert(iterator1 pos, iterator2 first, iterator2 last) 31 | (4)iterator insert(iterator pos) 32 | 33 | 04 顺序容器的eraser()两种变形 34 | (1) iterator eraser(iterator pos) 35 | (2) iterator eraser(iterator first, iterator last) 36 | 37 | 05 function object 38 | 欲使用function object,需包含头文件#include 39 | 首先,它是某个class的实例对象 40 | 其次,function object可以被当成一般函数使用 41 | 42 | 分类 43 | (1)算术运算 44 | plus, minus, negate, 45 | mulitplies, divides, modules 46 | (2)关系运算 47 | less, less_equal, 48 | greater, greater_equal 49 | equal_to, not_equal_to 50 | (3)逻辑运算 51 | logical_and, logical_or, logical_not 52 | 53 | 06 set 54 | set中的元素具有互异性 55 | 默认less-than运算符进行排序 56 | -------------------------------------------------------------------------------- /chapt4/4_01/Stack.cpp: -------------------------------------------------------------------------------- 1 | #include "Stack.hpp" 2 | 3 | bool Stack::pop(string & elem) 4 | { 5 | if(empty()) 6 | return false; 7 | elem = _stack.back(); 8 | _stack.pop_back(); 9 | return true; 10 | } 11 | 12 | bool Stack::peek(string &elem) 13 | { 14 | if(empty()) 15 | return false; 16 | elem = _stack.back(); 17 | return true; 18 | } 19 | 20 | bool Stack::push(const string &elem) 21 | { 22 | if(full()) 23 | return false; 24 | _stack.push_back(elem); 25 | return true; 26 | } -------------------------------------------------------------------------------- /chapt4/4_01/Stack.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class Stack 6 | { 7 | private: 8 | vector _stack; 9 | public: 10 | bool push(const string &elem); 11 | bool pop(string &elem); 12 | bool peek(string &elem); 13 | bool empty() {return _stack.empty();} 14 | bool full() {return _stack.max_size() == _stack.size();} 15 | int size(){return _stack.size();} 16 | }; -------------------------------------------------------------------------------- /chapt4/4_01/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Stack.hpp" 2 | 3 | void fill_stack(Stack &stack, istream &is = cin) 4 | { 5 | string str; 6 | while(is >> str && !stack.full()) 7 | stack.push(str); 8 | cout << "Read in " << stack.size() << " elements\n"; 9 | } 10 | 11 | int main() 12 | { 13 | Stack s; 14 | string str; 15 | 16 | cout << "s.size() = " << s.size() << endl; 17 | fill_stack(s); 18 | cout << "s.size() = " << s.size() << endl; 19 | cout << "s.peek(str) = " << s.peek(str) << " ["<< str << "]"<< endl; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /chapt4/4_02/Triangular.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class Triangular_iterator 8 | { 9 | public: 10 | Triangular_iterator(int index) : _index(index) {} 11 | bool operator==(const Triangular_iterator &) const; 12 | bool operator!=(const Triangular_iterator &) const; 13 | int operator*() const; 14 | Triangular_iterator &operator++(); 15 | Triangular_iterator operator++(int); 16 | 17 | private: 18 | int _index; 19 | void check_integrity() const; 20 | }; 21 | 22 | class Triangular 23 | { 24 | friend class Triangular_iterator; 25 | 26 | public: 27 | typedef Triangular_iterator iterator; 28 | Triangular(int len = 1, int begin = 1); 29 | int length() const { return _length; } 30 | void length(int len) {_length = len; } 31 | int beg_pos() const { return _beg_pos; } 32 | void beg_pos(int pos) {_beg_pos = pos;} 33 | int elem(int pos) const; 34 | bool next(int &val) const; 35 | void next_reset() const { _next = _beg_pos - 1; } 36 | Triangular ©(const Triangular &rhs); 37 | 38 | static bool is_elem(int value); 39 | static void gen_elements(int length); 40 | static void gen_elems_to_value(int value); 41 | static void display(int length, int beg_pos, ostream &os = cout); 42 | static int cur_size() 43 | { 44 | return _elems.size(); 45 | } 46 | 47 | Triangular_iterator begin() const 48 | { 49 | return Triangular_iterator(_beg_pos); 50 | } 51 | 52 | Triangular_iterator end() const 53 | { 54 | return Triangular_iterator(_beg_pos + _length); 55 | } 56 | 57 | private: 58 | int _length; // 元素个数 59 | int _beg_pos; // 起始位置 60 | mutable int _next; // 下一个元素的索引 61 | 62 | static vector _elems; 63 | static const int _max_elems = 1024; 64 | }; 65 | 66 | // Triangular_iterator方法实现 67 | bool Triangular_iterator::operator==(const Triangular_iterator &rhs) const 68 | { 69 | return _index == rhs._index; 70 | } 71 | 72 | bool Triangular_iterator::operator!=(const Triangular_iterator &rhs) const 73 | { 74 | return !(*this == rhs); 75 | } 76 | 77 | int Triangular_iterator::operator*() const 78 | { 79 | check_integrity(); 80 | return Triangular::_elems[_index - 1]; 81 | } 82 | 83 | void Triangular_iterator::check_integrity() const 84 | { 85 | if (_index >= Triangular::_max_elems) 86 | // throw iterator_overflow(); // 抛出异常 87 | 88 | if (_index >= Triangular::_elems.size()) 89 | Triangular::gen_elements(_index + 1); 90 | } 91 | 92 | Triangular_iterator &Triangular_iterator::operator++() 93 | { 94 | ++_index; 95 | check_integrity(); 96 | return *this; 97 | } 98 | 99 | Triangular_iterator Triangular_iterator::operator++(int) 100 | { 101 | Triangular_iterator tmp = *this; 102 | ++_index; 103 | check_integrity(); 104 | return tmp; 105 | } 106 | 107 | // Triangular方法实现 108 | vector Triangular::_elems; 109 | Triangular::Triangular(int len, int begin) 110 | { 111 | _length = len > 0 ? len : 1; 112 | _beg_pos = begin > 0 ? begin : 1; 113 | _next = _beg_pos - 1; 114 | 115 | int elem_cnt = _beg_pos + _length - 1; // Triangular中已经计算出的元素个数 116 | if (_elems.size() < elem_cnt) 117 | gen_elements(elem_cnt); 118 | } 119 | 120 | int Triangular::elem(int pos) const 121 | { 122 | return _elems[pos - 1]; 123 | } 124 | 125 | bool Triangular::next(int &val) const 126 | { 127 | if (_next < _beg_pos + _length - 1) 128 | { 129 | val = _elems[_next++]; 130 | return true; 131 | } 132 | return false; 133 | } 134 | 135 | Triangular &Triangular::copy(const Triangular &rhs) 136 | { 137 | if (this != &rhs) 138 | { 139 | _length = rhs._length; 140 | _beg_pos = rhs._beg_pos; 141 | _next = rhs._beg_pos - 1; 142 | } 143 | return *this; 144 | } 145 | 146 | bool Triangular::is_elem(int value) 147 | { 148 | if (!_elems.size() || _elems[_elems.size() - 1]) 149 | gen_elems_to_value(value); 150 | 151 | vector::iterator found_it; 152 | vector::iterator end_it = _elems.end(); 153 | found_it = find(_elems.begin(), end_it, value); 154 | return found_it != end_it; 155 | } 156 | 157 | void Triangular::gen_elements(int length) 158 | { 159 | if (length < 0 || length > _max_elems) 160 | return; 161 | if (_elems.size() < length) 162 | { 163 | int ix = _elems.size() ? _elems.size() + 1 : 1; 164 | for (; ix <= length; ix++) 165 | _elems.push_back(ix * (ix + 1) / 2); 166 | } 167 | } 168 | 169 | void Triangular::gen_elems_to_value(int value) 170 | { 171 | int ix = _elems.size(); 172 | if (!ix) 173 | { 174 | _elems.push_back(1); 175 | ix = 1; 176 | } 177 | while (_elems[ix - 1] < value && ix < _max_elems) 178 | { 179 | ++ix; 180 | _elems.push_back(ix * (ix + 1) / 2); 181 | } 182 | if (ix == _max_elems) 183 | cerr << "Value too large " << value << "-- exceeds max size of " << _max_elems << endl; 184 | } 185 | 186 | void Triangular::display(int length, int beg_pos, ostream &os) 187 | { 188 | for (int ix = beg_pos; ix < beg_pos + length; ix++) 189 | os << Triangular::_elems[ix - 1] << ' '; 190 | } 191 | 192 | ostream &operator<<(ostream &os, const Triangular &rhs) 193 | { 194 | os << "( " << rhs.beg_pos() << ", " << rhs.length() << " )"; 195 | rhs.display(rhs.length(), rhs.beg_pos(), os); 196 | return os; 197 | } 198 | 199 | istream & operator>>(istream & is, Triangular & rhs) 200 | { 201 | int bp, len; 202 | is >> bp >> len; 203 | rhs.beg_pos(bp); 204 | rhs.length(len); 205 | rhs.next_reset(); 206 | 207 | int elem_cnt = bp + len - 1; 208 | if (Triangular::cur_size() < elem_cnt) 209 | Triangular::gen_elements(elem_cnt); 210 | return is; 211 | } -------------------------------------------------------------------------------- /chapt4/4_02/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Triangular.hpp" 2 | 3 | int sum(const Triangular & trian); 4 | 5 | int main() 6 | { 7 | Triangular tri1(4); 8 | Triangular tri2; 9 | 10 | cout << tri1 << ", sum = " << sum(tri1) << endl; 11 | cin >> tri2; 12 | 13 | cout << tri2 << ", sum = " << sum(tri2) << endl; 14 | return 0; 15 | } 16 | 17 | int sum(const Triangular & trian) 18 | { 19 | if(!trian.length()) 20 | return 0; 21 | int val, sum = 0; 22 | trian.next_reset(); 23 | while(trian.next(val)) 24 | sum += val; 25 | 26 | return sum; 27 | } -------------------------------------------------------------------------------- /chapt4/4_03/Matrix.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Matrix 5 | { 6 | public: 7 | Matrix(){ cout << "Default constructor was called" << endl; } 8 | Matrix(int row, int col) : _row(row), _col(col) 9 | { 10 | cout << "Matrix(int, int) was called" << endl; 11 | pmat = new double[row * col]; 12 | } 13 | 14 | Matrix(const Matrix & rhs): _row(rhs._row), _col(rhs._col) 15 | { 16 | cout << "Matrix(const Matrix &) was called" << endl; 17 | pmat = new double[_row * _col]; 18 | for (int ix = 0; ix < _row * _col; ix++) 19 | pmat[ix] = rhs.pmat[ix]; 20 | } 21 | 22 | Matrix & operator=(const Matrix &rhs) 23 | { 24 | cout << "copy assignment operator was called" << endl; 25 | if(this != &rhs) 26 | { 27 | _row = rhs._row; 28 | _col = rhs._col; 29 | pmat = new double[_row * _col]; 30 | for(int ix = 0; ix < _row * _col; ++ix) 31 | pmat[ix] = rhs.pmat[ix]; 32 | } 33 | return *this; 34 | } 35 | 36 | ~Matrix() 37 | { 38 | cout << "~Matrix() was called" << endl; 39 | delete [] pmat; 40 | } 41 | 42 | private: 43 | int _row; 44 | int _col; 45 | double * pmat; 46 | }; -------------------------------------------------------------------------------- /chapt4/4_03/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Matrix.hpp" 2 | 3 | int main() 4 | { 5 | Matrix mat1(8, 4); 6 | { 7 | Matrix mat2; 8 | mat2 = mat1; 9 | } 10 | 11 | cout << "Bye" << endl; 12 | return 0; 13 | } -------------------------------------------------------------------------------- /chapt4/4_04/LessThan.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class LessThan 6 | { 7 | public: 8 | LessThan(int val): _val(val) {} 9 | int com_val() const {return _val;} 10 | void com_val(int nval) {_val = nval;} 11 | 12 | inline bool operator()(int value) const; 13 | private: 14 | int _val; 15 | }; 16 | 17 | bool LessThan::operator()(int value) const 18 | { 19 | return value < _val; 20 | } -------------------------------------------------------------------------------- /chapt4/4_04/main.cpp: -------------------------------------------------------------------------------- 1 | #include "LessThan.hpp" 2 | #include 3 | 4 | int count_less_than(const vector & vec, int comp) 5 | { 6 | LessThan lt(comp); 7 | int count = 0; 8 | for(int ix = 0; ix < vec.size(); ix++) 9 | if(lt(vec[ix])) 10 | ++count; 11 | return count; 12 | } 13 | 14 | void print_less_than(const vector & vec, int comp, ostream &os = cout) 15 | { 16 | LessThan lt(comp); 17 | vector::const_iterator iter = vec.begin(); 18 | vector::const_iterator iter_end = vec.end(); 19 | 20 | while((iter = find_if(iter, iter_end, lt)) != iter_end) 21 | { 22 | os << *iter << ' '; 23 | iter++; 24 | } 25 | cout << endl; 26 | } 27 | 28 | int main() 29 | { 30 | int ia[16] = {17, 12, 44, 9, 18, 45, 6, 14, 31 | 23, 67, 9, 0, 27, 55, 8, 16}; 32 | vector ivec(ia, ia + 16); 33 | int comp_val = 20; 34 | 35 | cout << "Number of elements less than " << comp_val << " are " 36 | << count_less_than(ivec, comp_val) << endl; 37 | 38 | print_less_than(ivec, comp_val); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /chapt4/基于对象的编码风格.txt: -------------------------------------------------------------------------------- 1 | 01 Constructor 2 | 构造函数函数名必须与class名称相同 3 | 构造函数不应指定返回值类型,也不用返回任何值 4 | 可以被重载 5 | 6 | 02 Member initialization list 7 | 紧跟在参数后的冒号后面 8 | 列表以逗号作为分隔 9 | 主要用于将参数传递给拥有member object的class 10 | 11 | 03 const member function 12 | 保证该member function不会修改class object 13 | 在声明/定义处添加 14 | 参数列表和函数体之间添加const 15 | 16 | 注意:const member function可以修改添加了mutable的数据成员 17 | 18 | 04 static data member 19 | 每个类的成员共享的、唯一的member 20 | 我们必须在程序文件中提供其清楚的定义,若该member比较简单,则可在类定义的内部同时声明和定义 21 | 22 | 05 运算符函数 23 | 不用指定名称 24 | 在运算符的前面加上关键字operator 25 | 26 | 06 function object 27 | 通常function object被当作参数传递给范型算法 -------------------------------------------------------------------------------- /chapt5/5_01/LibMat.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class LibMat 6 | { 7 | public: 8 | LibMat() 9 | { 10 | cout << "LibMat::LibMat() default constructor was called" << endl; 11 | } 12 | 13 | virtual ~LibMat() 14 | { 15 | cout << "LibMat::~LibMat() default destructor was called" << endl; 16 | } 17 | virtual void print() const 18 | { 19 | cout << "LibMat::print() -- I am a LibMat object" << endl; 20 | } 21 | }; 22 | 23 | class Book : public LibMat 24 | { 25 | public: 26 | Book(const string &title, const string &author) : _title(title), _author(author) 27 | { 28 | cout << "Book::Book( " << _title << ", " << _author << " ) constructor was called" << endl; 29 | } 30 | 31 | virtual ~Book() 32 | { 33 | cout << "Book::~Book() was called" << endl; 34 | } 35 | 36 | virtual void print() const 37 | { 38 | cout << "Book::print() -- I am a Book object" << endl; 39 | cout << "My title is: " << _title << endl; 40 | cout << "My author is: " << _author << endl; 41 | } 42 | 43 | const string & title() const { return _title;} 44 | const string & author() const { return _author;} 45 | 46 | protected: 47 | string _title; 48 | string _author; 49 | }; 50 | 51 | class AudioBook : public Book 52 | { 53 | public: 54 | AudioBook(const string &title, const string &author, const string &narrator) : Book(title, author), _narrator(narrator) 55 | { 56 | cout << "AudioBook::AudioBook(" << _title << ", " << _author << ", " << _narrator 57 | << " ) constructor was called" << endl; 58 | } 59 | 60 | ~AudioBook() 61 | { 62 | cout << "AudioBook::~AudioBook() destructor was called" << endl; 63 | } 64 | 65 | virtual void print() const 66 | { 67 | cout << "AudiBook::print() -- I am a AudiBook object" << endl; 68 | cout << "My title is: " << _title << endl; 69 | cout << "My author is: " << _author << endl; 70 | cout << "My narrator is: " << _narrator << endl; 71 | } 72 | 73 | const string & narrator() const { return _narrator;} 74 | protected: 75 | string _narrator; 76 | }; 77 | -------------------------------------------------------------------------------- /chapt5/5_01/main.cpp: -------------------------------------------------------------------------------- 1 | #include "LibMat.hpp" 2 | 3 | void print(const LibMat & mat) 4 | { 5 | cout << "In global print(): about to print mat.print()" << endl; 6 | mat.print(); 7 | } 8 | 9 | int main() 10 | { 11 | // cout << "\nCreating a LibMat object to print()" << endl; 12 | // LibMat libmat; 13 | // print(libmat); 14 | 15 | // cout << "\nCreating a Book object to print()" << endl; 16 | // Book b("The Castle", "Franz Kafka"); 17 | // print(b); 18 | 19 | // cout << "\nCreating a AudiBook object to print()" << endl; 20 | // AudioBook ab("Man Without Qualities", "Robert Musil", "Keneth Meyer"); 21 | // print(ab); 22 | 23 | LibMat * audio = new AudioBook("Man Without Qualities", "Robert Musil", "Keneth Meyer"); 24 | delete audio; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /chapt5/5_02/fibonacci.hpp: -------------------------------------------------------------------------------- 1 | #include "num_sequence.hpp" 2 | 3 | class Fibonacci : public num_sequence 4 | { 5 | public: 6 | Fibonacci(int len = 1, int beg_pos = 1) : _length(len), _beg_pos(beg_pos) {} 7 | virtual int elem(int pos) const; 8 | virtual const char *what_am_i() const { return "Fibonacci"; } 9 | virtual ostream &print(ostream &os = cout) const; 10 | int length() const { return _length; } 11 | int beg_pos() const { return _beg_pos; } 12 | 13 | protected: 14 | virtual void gen_elems(int pos) const; 15 | int _length; 16 | int _beg_pos; 17 | static vector _elems; 18 | }; 19 | 20 | vector Fibonacci::_elems; 21 | 22 | int Fibonacci::elem(int pos) const 23 | { 24 | if (!check_integrity(pos, _elems.size())) 25 | return 0; 26 | if (pos > _elems.size()) 27 | Fibonacci::gen_elems(pos); 28 | return _elems[pos - 1]; 29 | } 30 | 31 | void Fibonacci::gen_elems(int pos) const 32 | { 33 | if (_elems.empty()) 34 | { 35 | _elems.push_back(1); 36 | _elems.push_back(1); 37 | } 38 | if (_elems.size() < pos) // 不用等号可以吗 39 | { 40 | int ix = _elems.size(); 41 | int n_2 = _elems[ix - 2]; 42 | int n_1 = _elems[ix - 1]; 43 | for (; ix < pos; ++ix) 44 | { 45 | int elem = n_1 + n_2; 46 | _elems.push_back(elem); 47 | cout << "gen_elmes: " << elem << endl; 48 | n_2 = n_1; 49 | n_1 = elem; 50 | } 51 | } 52 | } 53 | 54 | ostream & Fibonacci::print(ostream &os) const 55 | { 56 | int elem_pos = _beg_pos - 1; 57 | int end_pos = elem_pos + _length; 58 | // [elem_pos, end_pos) 59 | 60 | if(end_pos > _elems.size()) 61 | Fibonacci::gen_elems(end_pos); 62 | 63 | os << "( "<< _beg_pos << ", " << _length << " ) "; 64 | while(elem_pos < end_pos) 65 | os << _elems[elem_pos++] << ' '; 66 | return os; 67 | } 68 | -------------------------------------------------------------------------------- /chapt5/5_02/main.cpp: -------------------------------------------------------------------------------- 1 | #include "fibonacci.hpp" 2 | 3 | ostream & operator<<(ostream &os, const num_sequence &ns) 4 | { 5 | return ns.print(os); 6 | } 7 | int main() 8 | { 9 | Fibonacci fib; 10 | 11 | cout << fib << endl; 12 | 13 | Fibonacci fib2(16); 14 | cout << fib2 << endl; 15 | 16 | Fibonacci fib3(8, 12); 17 | cout << fib3 << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /chapt5/5_02/num_sequence.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class num_sequence 6 | { 7 | public: 8 | virtual ~num_sequence() {} 9 | virtual int elem(int pos) const = 0; 10 | virtual const char * what_am_i() const = 0; 11 | virtual ostream & print(ostream & os = cout) const = 0; 12 | static int max_elems() { return _max_elems; }; 13 | 14 | protected: 15 | virtual void gen_elems(int pos) const = 0; 16 | bool check_integrity(int pos, int size) const; 17 | 18 | const static int _max_elems = 1024; 19 | }; 20 | 21 | bool num_sequence::check_integrity(int pos, int size) const 22 | { 23 | if(pos <= 0 || pos > _max_elems) 24 | { 25 | cerr << "!! invalid position: " << pos << " Cannot honor request\n"; 26 | return false; 27 | } 28 | if(pos > size) 29 | gen_elems(pos); 30 | return true; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /chapt5/5_03/fibonacci.hpp: -------------------------------------------------------------------------------- 1 | #include "num_sequence.hpp" 2 | 3 | class Fibonacci : public num_sequence 4 | { 5 | public: 6 | Fibonacci(int len = 1, int beg_pos = 1) : num_sequence(len, beg_pos, _elems) 7 | { 8 | } 9 | virtual const char *what_am_i() const { return "Fibonacci"; } 10 | 11 | protected: 12 | virtual void gen_elems(int pos) const; 13 | static vector _elems; 14 | }; 15 | 16 | vector Fibonacci::_elems; 17 | 18 | void Fibonacci::gen_elems(int pos) const 19 | { 20 | if (_elems.empty()) 21 | { 22 | _elems.push_back(1); 23 | _elems.push_back(1); 24 | } 25 | if (_elems.size() < pos) // 不用等号可以吗 26 | { 27 | int ix = _elems.size(); 28 | int n_2 = _elems[ix - 2]; 29 | int n_1 = _elems[ix - 1]; 30 | for (; ix < pos; ++ix) 31 | { 32 | int elem = n_1 + n_2; 33 | _elems.push_back(elem); 34 | cout << "gen_elmes: " << elem << endl; 35 | n_2 = n_1; 36 | n_1 = elem; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /chapt5/5_03/main.cpp: -------------------------------------------------------------------------------- 1 | #include "fibonacci.hpp" 2 | 3 | ostream & operator<<(ostream &os, const num_sequence &ns) 4 | { 5 | return ns.print(os); 6 | } 7 | int main() 8 | { 9 | Fibonacci fib; 10 | cout << fib << endl; 11 | 12 | Fibonacci fib2(16); 13 | cout << fib2 << endl; 14 | 15 | num_sequence * pns = new Fibonacci(8, 12); 16 | cout << *pns << endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /chapt5/5_03/num_sequence.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class num_sequence 6 | { 7 | public: 8 | virtual ~num_sequence() { } 9 | int elem(int pos) const; 10 | virtual const char *what_am_i() const = 0; 11 | ostream &print(ostream &os = cout) const; 12 | static int max_elems() { return _max_elems; }; 13 | int length() const { return _length; } 14 | int beg_pos() const { return _beg_pos; } 15 | 16 | protected: 17 | num_sequence(int len, int bp, vector &re) : _length(len), _beg_pos(bp), _relems(re) 18 | { 19 | } 20 | virtual void gen_elems(int pos) const = 0; 21 | bool check_integrity(int pos, int size) const; 22 | int _length; 23 | int _beg_pos; 24 | const static int _max_elems = 1024; 25 | vector &_relems; 26 | }; 27 | 28 | bool num_sequence::check_integrity(int pos, int size) const 29 | { 30 | if (pos <= 0 || pos > _max_elems) 31 | { 32 | cerr << "!! invalid position: " << pos << " Cannot honor request\n"; 33 | return false; 34 | } 35 | if (pos > size) 36 | gen_elems(pos); 37 | return true; 38 | } 39 | 40 | int num_sequence::elem(int pos) const 41 | { 42 | if (!check_integrity(pos, _relems.size())) 43 | return 0; 44 | if (pos > _relems.size()) 45 | gen_elems(pos); 46 | return _relems[pos - 1]; 47 | } 48 | 49 | ostream &num_sequence::print(ostream &os) const 50 | { 51 | int elem_pos = _beg_pos - 1; 52 | int end_pos = elem_pos + _length; 53 | 54 | if (end_pos > _relems.size()) 55 | gen_elems(end_pos); 56 | 57 | os << "( " << _beg_pos << ", " << _length << " ) "; 58 | while (elem_pos < end_pos) 59 | os << _relems[elem_pos++] << ' '; 60 | return os; 61 | } 62 | -------------------------------------------------------------------------------- /chapt5/5_04/fibonacci.hpp: -------------------------------------------------------------------------------- 1 | #include "num_sequence.hpp" 2 | 3 | class Fibonacci : public num_sequence 4 | { 5 | public: 6 | virtual const char *what_am_i() 7 | { 8 | return "Fibonacci"; 9 | } 10 | }; -------------------------------------------------------------------------------- /chapt5/5_04/main.cpp: -------------------------------------------------------------------------------- 1 | #include "fibonacci.hpp" 2 | #include 3 | 4 | int main() 5 | { 6 | Fibonacci fib; 7 | Fibonacci fib2; 8 | num_sequence ns; 9 | 10 | num_sequence * pns = &fib; 11 | 12 | cout << fib.what_am_i() << endl; 13 | cout << pns->what_am_i() << endl; 14 | cout << typeid(fib).name() << endl; 15 | cout << typeid(fib2). name() << endl; 16 | cout << (typeid(fib)==typeid(fib2)) << endl; 17 | 18 | cout << typeid(ns).name() << endl; 19 | cout << (typeid(fib)==typeid(ns)) << endl; 20 | 21 | cout << typeid(*pns).name() << endl; 22 | 23 | return 0; 24 | } 25 | 26 | // -Wall == warning all -------------------------------------------------------------------------------- /chapt5/5_04/num_sequence.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class num_sequence 6 | { 7 | public: 8 | virtual const char *what_am_i() const 9 | { 10 | return "num_sequence"; 11 | } 12 | }; 13 | 14 | -------------------------------------------------------------------------------- /chapt5/面向对象编码风格.txt: -------------------------------------------------------------------------------- 1 | 01 面向对象的三个特性 2 | 继承 3 | 多态 4 | 动态绑定 5 | 6 | 注:关于多态和动态绑定 7 | 只有在通过抽象基类的pointer或reference操作共通接口时,才能体现 8 | 9 | 02 派生类 10 | 使用派生类不必刻意区分“继承而来的成员”还是“自身定义的成员”,两者的使用完全透明 11 | 12 | 03 定义抽象类 13 | (1)找出所有子类共通的操作 14 | (2)确定和类型(子类)相关的操作行为,成为virtual函数 15 | 有纯虚函数的类不能产生任何对象,只能用来派生子类 16 | 一般原则,有virtual虚函数的基类的destructor一般声明为virtual 17 | (3)确定访问层级 18 | 19 | 04 虚函数静态解析 20 | 使用基类对象,而不是基类的pointer或reference 21 | 基类的constructor和destructor内 22 | 23 | 05 typeid 24 | 使用前要#include 25 | typeid是个运算符 26 | 27 | 06 类型转换 28 | static_cast<目标类型> (转化对象) --- 无条件转化 29 | dynameic_cast<目标类型> (转化对象) --- 能转则转,不能转返回0 -------------------------------------------------------------------------------- /chapt6/6_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | template 6 | class PrintIt{ 7 | public: 8 | PrintIt(OS & os) : _os(os){} 9 | 10 | template 11 | void print(const elemType & elem, char delimiter = '\n') 12 | { 13 | _os << elem << delimiter; 14 | } 15 | private: 16 | ostream & _os; 17 | }; 18 | 19 | 20 | int main() 21 | { 22 | PrintIt to_standard_out(cout); 23 | to_standard_out.print("hello"); 24 | to_standard_out.print(1024); 25 | 26 | string my_string("i am a string"); 27 | to_standard_out.print(my_string); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /chapt6/以template进行编程.txt: -------------------------------------------------------------------------------- 1 | 本章无笔记 -------------------------------------------------------------------------------- /chapt7/异常处理Exception Handling.txt: -------------------------------------------------------------------------------- 1 | 01 异常 2 | 异常是一个对象 3 | 最简单的异常对象可以设计为整数字符串;大部分被抛出的异常属于特定的异常类 4 | 5 | 02 异常处理步骤 6 | 异常的鉴定与发出 7 | 异常的处理方式 8 | (1)再次抛出【此时只用写下throw关键字即可】 9 | (2)想要捕获任意类型的异常,可以使用catch( ... ) 10 | 11 | 03 局部资源管理器 12 | C++保证所有局部对象的destructor都会被调用【无论是否有异常机制】 13 | 14 | auto_ptr:智能指针 15 | (1)标准库提供的,使用前必须包含memory头文件 16 | (2)是一个class template,可以实现资源的自动释放 17 | (3)已被废弃 18 | 19 | 04 ostringstream类 20 | (1)将多笔不同格式的数据类型转换为字符串 21 | (2)调用ostringstream类中的str()方法,返回string对象 22 | (3)要包含头文件sstream 23 | 24 | ps: 相对应的类为istringstream --------------------------------------------------------------------------------