├── chap17 ├── data │ ├── answer │ ├── input │ ├── person_info │ ├── store1 │ ├── store2 │ ├── map │ ├── store0 │ ├── paragraph │ ├── quiz │ └── story ├── Exer17_09.cpp ├── Page745_random_engine.cpp ├── Page749_seed_time.cpp ├── Exer17_28.cpp ├── Exer17_10.cpp ├── Exer17_01_02.cpp ├── Exer17_29.cpp ├── Page731_regex.cpp ├── Exer17_30.cpp ├── Page746_random_engine.cpp ├── Exer17_27.cpp ├── Exer17_35.cpp ├── Page728_grading.cpp ├── Exer17_24.cpp └── Exer17_16.cpp ├── chap01 ├── Exer01_01.cpp ├── Exer01_02.cpp ├── Exer01_03.cpp ├── Exer01_12.cpp ├── Exer01_13_2.cpp ├── Exer01_20.cpp ├── Exer01_10.cpp ├── Exer01_22.cpp ├── Exer01_21.cpp ├── Exer01_04.cpp ├── Exer01_16.cpp ├── Exer01_13_1.cpp ├── Exer01_08.cpp ├── Exer01_09.cpp ├── Exer01_13_3.cpp ├── Exer01_05.cpp ├── 4th │ ├── Exer1_16_4th.cpp │ ├── Exer1_17_4th.cpp │ ├── Exer1_18_4th.cpp │ ├── Exer1_17_1_4th.cpp │ └── Exer1_15_4th.cpp ├── Exer01_19.cpp ├── Exer01_11.cpp ├── Exer01_24.cpp ├── Exer01_25.cpp └── Exer01_18.cpp ├── chap11 ├── data │ ├── input │ ├── author_book │ ├── map │ └── paragraph ├── Exer11_15.cpp ├── Exer11_19.cpp ├── Exer11_12.cpp ├── Exer11_26.cpp ├── Exer11_20.cpp └── Exer11_13.cpp ├── .gitignore ├── chap08 ├── data │ ├── person_info │ └── transactions ├── Page315_buffer.cpp ├── Exer08_01.cpp ├── Page316_tie.cpp ├── Exer08_09.cpp └── Exer08_04.cpp ├── chap19 ├── Exer19_26.c ├── Exer19_26.h ├── Exer19_14.cpp ├── Exer19_13.cpp ├── Exer19_02.cpp ├── Exer19_16.cpp ├── Exer19_17.cpp ├── Page823_placement_new.cpp ├── Exer19_01_new_delete.h ├── Exer19_10.cpp └── Exer19_12.cpp ├── chap06 ├── Chapter6.h ├── Exer06_09.cpp ├── Exer06_26.cpp ├── fact.cc ├── Page224_makeplural.h ├── Exer06_05.cpp ├── Exer06_03.cpp ├── Page239_inline.h ├── Exer06_04.cpp ├── Exer06_11.cpp ├── Exer06_07.cpp ├── Exer06_32.cpp ├── Page247_function_pointers.cpp ├── Exer06_43.cpp ├── Exer06_27.cpp ├── Exer06_10.cpp ├── Page241_assert.cpp ├── Exer06_22.cpp ├── Exer06_25.cpp ├── Exer06_12.cpp ├── Exer06_33.cpp ├── Exer06_55_56.cpp ├── Exer06_42.cpp ├── Page218_arrayref.cpp ├── Exer06_23.cpp └── Exer06_47.cpp ├── tools ├── SearchText.sh ├── SetUpObjectionFolder.sh ├── FormatFileName.sh └── InsertAnswer.h ├── chap10 ├── data │ └── integers.txt ├── Exer10_21.cpp ├── Exer10_03.cpp ├── Exer10_34.cpp ├── Exer10_35.cpp ├── Exer10_04.cpp ├── Page405_ostreamiterator.cpp ├── Exer10_05_2.cpp ├── Exer10_36.cpp ├── Exer10_06.cpp ├── Exer10_13.cpp ├── Exer10_30.cpp ├── Exer10_01.cpp ├── Exer10_17.cpp ├── Exer10_31.cpp ├── Exer10_12.cpp ├── Page406_streamiterator.cpp ├── Exer10_02.cpp ├── Exer10_27.cpp └── Page404_istreamiterator.cpp ├── chap02 ├── Exer02_12.cpp ├── Sales_data.h ├── Exer02_08.cpp ├── Exer02_16.cpp ├── Exer02_04.cpp ├── Exer02_35.cpp ├── Exer02_42_1.cpp ├── Exer02_09.cpp ├── Page056_void.cpp ├── Exer02_41_1.cpp ├── Page061_const.cpp ├── Exer02_39.cpp ├── Page046_underline.cpp └── Exer02_42_2.cpp ├── .dir-locals.el ├── chap13 ├── Exer13_05.cpp ├── Exer13_30.cpp ├── Exer13_08.cpp ├── Exer13_14.h ├── Exer13_16.h ├── Exer13_14.cpp ├── Exer13_15.cpp ├── Exer13_16.cpp ├── Exer13_15.h ├── Exer13_13.h ├── Exer13_43.cpp ├── Exer13_53_1.cpp ├── Page540_move.cpp ├── Page532_rvalue_reference.cpp ├── Exer13_18.h ├── Exer13_31.cpp ├── Page517_swap.cpp ├── Exer13_44.cpp ├── Exer13_28_pointer.cpp ├── Exer13_28_value.cpp ├── Page545_rvaluereference.cpp └── Exer13_34_36_37.cpp ├── chap07 ├── Exer07_58_example.cpp ├── Exer07_58_example.h ├── Exer07_27.cpp ├── Exer07_29.cpp ├── Exer07_34.cpp ├── Exer07_09.cpp ├── Page298_aggregate.cpp ├── Exer07_32.cpp ├── Exer07_53_Debug.h ├── Exer07_49.cpp ├── Exer07_21.cpp ├── Exer07_57.cpp ├── Exer07_31.cpp ├── Exer07_23_Screen.h ├── Exer07_43_1.cpp └── Exer07_24_Screen.h ├── chap16 ├── Exer16_07_size.h ├── Exer16_02_compare.h ├── Exer16_26.cpp ├── Exer16_20_print_iter.h ├── Exer16_41.h ├── Exer16_05_print.h ├── Exer16_19_print_container.h ├── Exer16_14_15.cpp ├── Exer16_04_find.h ├── Exer16_05.cpp ├── Exer16_02.cpp ├── Exer16_06.cpp ├── Exer16_06_begin_end.h ├── Exer16_07.cpp ├── Exer16_39.cpp ├── Exer16_61.cpp ├── Exer16_36.cpp ├── Exer16_56.cpp ├── Exer16_41.cpp ├── Exer16_45.cpp ├── Exer16_11.cpp ├── Exer16_16.cpp ├── Exer16_21_DebugDelete.h ├── Exer16_44_2.cpp ├── Exer16_62.cpp ├── Exer16_19.cpp ├── Exer16_18.h ├── Exer16_04.cpp ├── Exer16_48.cpp ├── Exer16_43.cpp ├── Exer16_65.cpp ├── Exer16_44_1.cpp ├── Page669_template_declaration.cpp ├── Exer16_20.cpp ├── Exer16_58.cpp └── Page684_trailing_return.cpp ├── chap04 ├── Exer04_10.cpp ├── Exer04_33.cpp ├── Exer04_04.cpp ├── Exer04_05.cpp ├── Page138_evaluation.cpp ├── Exer04_07.cpp ├── Exer04_21.cpp ├── Page148_increment.cpp ├── Exer04_29.cpp ├── Exer04_31.cpp ├── Exer04_22_1.cpp ├── Exer04_22_2.cpp ├── Makefile ├── Exer04_37.cpp └── Exer04_24.cpp ├── chap03 ├── Exer03_31.cpp ├── Exer03_14.cpp ├── Exer03_12.cpp ├── Exer03_01_2.cpp ├── Exer03_02_2.cpp ├── Exer03_32_2.cpp ├── Exer03_02_1.cpp ├── Exer03_15.cpp ├── Exer03_32_1.cpp ├── Exer03_13.cpp ├── Exer03_42.cpp ├── Exer03_35.cpp ├── Exer03_01_1.cpp ├── Exer03_41.cpp ├── Exer03_10.cpp ├── Exer03_40.cpp ├── Exer03_07.cpp ├── Exer03_06.cpp ├── Exer03_25.cpp ├── Exer03_16.cpp ├── Exer03_21_1.cpp ├── Exer03_23.cpp ├── Page130_typealias.cpp ├── Exer03_05.cpp ├── Exer03_22.cpp ├── Exer03_08.cpp ├── Exer03_01_3.cpp ├── Page128_array.cpp ├── Page095_subscript.cpp ├── Exer03_04.cpp ├── Exer03_17.cpp ├── Exer03_21_2.cpp ├── Exer03_45.cpp ├── Exer03_43.cpp └── Exer03_36_2.cpp ├── chap14 ├── Exer14_35.cpp ├── Exer14_07.cpp ├── Exer14_34.h ├── Exer14_27.cpp ├── Page582_conversion.cpp ├── Page586_overload_conversion.cpp ├── Exer14_48_49.cpp ├── Exer14_45.cpp ├── Exer14_36.cpp ├── Exer14_34.cpp ├── Exer14_30.cpp ├── Exer14_35_ReadString.h ├── Exer14_10.cpp ├── Exer14_43.cpp ├── Exer14_32.cpp ├── Exer14_28.cpp └── Page588_function_matching.cpp ├── chap05 ├── Exer05_23.cpp ├── Exer05_15.cpp ├── Exer05_24.cpp ├── Exer05_20.cpp ├── Exer05_17.cpp ├── Exer05_21.cpp ├── Exer05_03.cpp ├── Exer05_25.cpp ├── Exer05_18.cpp ├── Exer05_19.cpp └── Exer05_09.cpp ├── chap15 ├── Exer15_36.cpp ├── Exer15_27.cpp ├── Exer15_39 │ ├── Exer15_39_Query.cpp │ ├── Exer15_39_OrQuery.h │ ├── Exer15_39_WordQuery.h │ ├── Exer15_39_Query_base.h │ ├── Exer15_39_BinaryQuery.h │ ├── Exer15_39_NotQuery.h │ └── Exer15_39_AndQuery.h ├── data │ └── story.txt ├── Exer15_06.cpp ├── Page618_name_collisions.cpp ├── Exer15_15_Disc_quote.h ├── Exer15_28.cpp ├── Exer15_41_42 │ └── README.md ├── Page617_Disc_quote.h ├── Exer15_07.cpp ├── Page603_no_conversion.cpp ├── Exer15_15_16.cpp └── Exer15_29.cpp ├── Makefile ├── chap18 ├── Exer18_10_1.cpp ├── Exer18_05.cpp ├── Exer18_20_1.cpp ├── Exer18_10_2.cpp ├── Exer18_20_2.cpp ├── Exer18_23.cpp ├── Exer18_03_2.cpp ├── Page794_using_directive.cpp ├── Exer18_22.cpp └── Exer18_17_1.cpp ├── chap09 ├── Exer09_42.cpp ├── Exer09_15.cpp ├── Exer09_19.cpp ├── Exer09_18.cpp ├── Page366_loopsearch.cpp ├── Page349_erase.cpp ├── Exer09_16.cpp ├── Exer09_11.cpp ├── Exer09_33.cpp ├── Exer09_46.cpp ├── Exer09_45.cpp ├── Exer09_50.cpp ├── Exer09_41.cpp ├── Exer09_27.cpp ├── Exer09_34.cpp ├── Exer09_20.cpp ├── Exer09_24.cpp ├── Exer09_51.cpp ├── Exer09_47.cpp └── Exer09_14.cpp └── chap12 ├── Exer12_10.cpp ├── Page473_weak_ptr.cpp ├── Exer12_24.cpp ├── Page460_newdelete.cpp ├── Exer12_13.cpp ├── Exer12_26.cpp ├── Exer12_06.cpp ├── Exer12_02.cpp └── Exer12_17.cpp /chap17/data/answer: -------------------------------------------------------------------------------- 1 | 0111000110 -------------------------------------------------------------------------------- /chap01/Exer01_01.cpp: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | return 0; 4 | } 5 | -------------------------------------------------------------------------------- /chap01/Exer01_02.cpp: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | return -1; 4 | } 5 | -------------------------------------------------------------------------------- /chap11/data/input: -------------------------------------------------------------------------------- 1 | where r u 2 | y dont u send me a pic 18r 3 | k thk -------------------------------------------------------------------------------- /chap17/data/input: -------------------------------------------------------------------------------- 1 | where r u 2 | y dont u send me a pic 18r 3 | k thk -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #ignore generated files 2 | chap*/obj/ 3 | *.obj 4 | *.o 5 | *.exe -------------------------------------------------------------------------------- /chap11/data/author_book: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chihyang/CPP_Primer/HEAD/chap11/data/author_book -------------------------------------------------------------------------------- /chap08/data/person_info: -------------------------------------------------------------------------------- 1 | morgan 201555-2368 8625550123 2 | drew 9735550130 3 | lee 6095550132 2015550175 8005550000 -------------------------------------------------------------------------------- /chap19/Exer19_26.c: -------------------------------------------------------------------------------- 1 | #include "Exer19_26.h" 2 | int compute(int *p, int i) 3 | { 4 | p = &i; 5 | return *p; 6 | } 7 | -------------------------------------------------------------------------------- /chap19/Exer19_26.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER19_26_LINKAGE_H 2 | #define EXER19_26_LINKAGE_H 3 | int compute(int*, int); 4 | #endif 5 | -------------------------------------------------------------------------------- /chap17/data/person_info: -------------------------------------------------------------------------------- 1 | morgan (201) 555-2368 862-555-0123 2 | drew (973)555.0130 3 | lee (609) 555-0132 2015550175 800.555-0000 -------------------------------------------------------------------------------- /chap11/data/map: -------------------------------------------------------------------------------- 1 | brb be right back 2 | k okay? 3 | y why 4 | r are 5 | u you 6 | pic picture 7 | thk thanks! 8 | 18r later 9 | dont don't -------------------------------------------------------------------------------- /chap01/Exer01_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | std::cout << "Hello World!" << std::endl; 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /chap06/Chapter6.h: -------------------------------------------------------------------------------- 1 | // Exer06_08 2 | #ifndef CHAP06_H 3 | #define CHAP06_H 4 | int fact(int); 5 | void fact(); 6 | int abs(int); 7 | #endif 8 | -------------------------------------------------------------------------------- /tools/SearchText.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo 'Usage: SearchText ' 3 | echo 4 | find $1 -type f -name "*."$2 |xargs grep -E $3 5 | -------------------------------------------------------------------------------- /chap06/Exer06_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | int main() 6 | { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /chap08/data/transactions: -------------------------------------------------------------------------------- 1 | 978-7-121-20038-0 5 128.0 2 | 978-7-121-20038-0 2 98.0 3 | 978-7-121-20038-0 1 100.0 4 | 978-0-553-21393-8 1 6.99 5 | 978-0-87779-855-2 2 6.99 -------------------------------------------------------------------------------- /chap10/data/integers.txt: -------------------------------------------------------------------------------- 1 | 54 39 51 93 66 94 18 4 37 18 57 55 99 32 22 2 70 80 11 22 85 12 10 26 80 70 33 50 47 1 69 94 68 80 55 63 73 80 82 9 46 5 62 85 75 7 62 58 99 16 -------------------------------------------------------------------------------- /chap02/Exer02_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | // (b), strange but legal, see Page046_underline.cpp for more examples. 5 | int _; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /.dir-locals.el: -------------------------------------------------------------------------------- 1 | ;;; Directory Local Variables 2 | ;;; For more information see (info "(emacs) Directory Variables") 3 | 4 | ((c++-mode 5 | (flycheck-gcc-language-standard . "c++11"))) 6 | -------------------------------------------------------------------------------- /chap13/Exer13_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Page511_540_HasPtr.h" 3 | int main() 4 | { 5 | HasPtr ps1("This is a string"); 6 | HasPtr ps2 = ps1; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /chap13/Exer13_30.cpp: -------------------------------------------------------------------------------- 1 | #include "Page511_540_HasPtr.h" 2 | int main() 3 | { 4 | HasPtr h1("This is h1"); 5 | HasPtr h2("This is h2"); 6 | swap(h1, h2); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /chap17/data/store1: -------------------------------------------------------------------------------- 1 | 978-7-121-20038-0 10 100.0 2 | 978-0-553-21393-8 1 6.99 3 | 978-0-87779-855-2 2 6.99 4 | 978-7-121-12332-0 5 65.00 5 | 978-7-121-12332-0 1 59.00 6 | 978-7-121-12570-6 20 46.00 -------------------------------------------------------------------------------- /chap02/Sales_data.h: -------------------------------------------------------------------------------- 1 | #ifndef SALES_DATA_H 2 | #define SALES_DATA_H 3 | struct Sales_data { 4 | std::string bookNo; 5 | unsigned units_sold = 0; 6 | double revenue = 0.0; 7 | }; 8 | #endif 9 | -------------------------------------------------------------------------------- /chap07/Exer07_58_example.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 7.58 2 | #include "Exer07_58_example.h" 3 | double Example::rate = 6.5; 4 | vector Example::vec(vecSize); 5 | int main() 6 | { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /chap16/Exer16_07_size.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER16_07_SIZE_H 2 | #define EXER16_07_SIZE_H 3 | template 4 | inline constexpr unsigned size(const T(&arr)[N]) 5 | { 6 | return N; 7 | } 8 | #endif 9 | -------------------------------------------------------------------------------- /chap01/Exer01_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int sum = 0; 5 | for (int i = -100; i <= 100; ++i) 6 | sum += i; 7 | std::cout << sum << std::endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /chap01/Exer01_13_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | // for loop 5 | for(int i = 10; i >= 0; i--) 6 | { 7 | std::cout << i << std::endl; 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chap01/Exer01_20.cpp: -------------------------------------------------------------------------------- 1 | #include "Sales_item.h" 2 | int main() 3 | { 4 | Sales_item book; 5 | while(std::cin >> book) 6 | { 7 | std::cout << book << std::endl; 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chap04/Exer04_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cin; 3 | int main() 4 | { 5 | int i; 6 | while(cin >> i) 7 | { 8 | if(i == 42) 9 | break; 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap17/data/store2: -------------------------------------------------------------------------------- 1 | 978-7-121-20038-0 10 100.0 2 | 978-0-553-21393-8 1 6.99 3 | 978-0-87779-855-2 2 6.99 4 | 978-7-121-12332-0 5 65.00 5 | 978-7-121-12332-0 1 59.00 6 | 978-7-121-12570-6 20 46.00 7 | 978-7-04-026845-4 50 60.00 -------------------------------------------------------------------------------- /chap19/Exer19_14.cpp: -------------------------------------------------------------------------------- 1 | #include "Exer19_12_Screen.h" 2 | int main() 3 | { 4 | auto pmf = &Screen::get_cursor; 5 | pmf = &Screen::get; // ok: one version of get has the same type as get_cursor 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /chap06/Exer06_26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main(int argc, char *argv[]) 5 | { 6 | for(int i = 0; i != argc; ++i) 7 | cout << argv[i] << endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /chap07/Exer07_58_example.h: -------------------------------------------------------------------------------- 1 | // Exercise 7.58 2 | #include 3 | using std::vector; 4 | class Example { 5 | public: 6 | static double rate; 7 | static const int vecSize = 20; 8 | static vector vec; 9 | }; 10 | -------------------------------------------------------------------------------- /chap01/Exer01_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | // while loop 5 | int j = 10; 6 | while(j >= 0) 7 | { 8 | std::cout << j << std::endl;; 9 | j--; 10 | } 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap03/Exer03_31.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | int main() 6 | { 7 | int array[10]; 8 | for(int i = 0; i < 10; ++i) 9 | array[i] = i; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /chap14/Exer14_35.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer14_35_ReadString.h" 4 | int main() 5 | { 6 | ReadString r; 7 | std::string s = r(); 8 | std::cout << s << std::endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chap13/Exer13_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Page511_540_HasPtr.h" 3 | int main() 4 | { 5 | HasPtr ps1("This is a string"), ps3("This is another string"); 6 | HasPtr ps2 = ps1; 7 | ps2 = ps3; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /chap14/Exer14_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "String.h" 3 | int main() 4 | { 5 | String s1 = "This is a whole sentence.", s2; 6 | std::cout << s1 << std::endl; 7 | std::cout << s2 << std::endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /chap02/Exer02_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | // 2M followed by a newline 5 | std::cout << "2M\n" << std::endl; 6 | // 2\tM followed by a newline 7 | std::cout <<"2\tM\n" << std::endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /chap06/fact.cc: -------------------------------------------------------------------------------- 1 | // Exer06_09, fact.cc, see factMain.cc for compile and link info 2 | #include 3 | #include "Chapter6.h" 4 | int fact(int val) 5 | { 6 | int ret = 1; 7 | while(val > 1) 8 | ret *= val--; 9 | return ret; 10 | } 11 | -------------------------------------------------------------------------------- /chap17/data/map: -------------------------------------------------------------------------------- 1 | brb be_right_back Be_right_back 2 | k okay? ok? OKAY? Ok? OK? 3 | y why WHY Why WhY 4 | r are were was 5 | u you thou thy thee 6 | pic picture pictures 7 | thk thanks! thks! THANKS! thank_you! 8 | 18r later Later LATER 9 | dont don't do_not DON'T DONOT -------------------------------------------------------------------------------- /chap05/Exer05_23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | int main() 6 | { 7 | int i1, i2; 8 | while(cin >> i1 >> i2) 9 | { 10 | cout << i1 / i2 << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap16/Exer16_02_compare.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER16_02_COMPARE_H 2 | #define EXER16_02_COMPARE_H 3 | template 4 | int compare(const T &v1, const T &v2) 5 | { 6 | if(v1 < v2) return -1; 7 | if(v2 < v1) return 1; 8 | return 0; 9 | } 10 | #endif 11 | -------------------------------------------------------------------------------- /chap16/Exer16_26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::vector; 3 | class NoDefault { 4 | public: 5 | NoDefault(int i) : val(i) {} 6 | private: 7 | int val; 8 | }; 9 | template class vector; 10 | int main() 11 | { 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap03/Exer03_14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cin; 4 | using std::vector; 5 | int main() 6 | { 7 | vector ivec; 8 | int i; 9 | while(cin >> i) { 10 | ivec.push_back(i); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap04/Exer04_33.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | int someValue = 0; 7 | int x = 2, y = 5; 8 | int val = (someValue ? ++x, --y : --x, --y); 9 | cout << val << endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /chap15/Exer15_36.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer15_35_Query.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | Query q = Query("fiery") & Query("bird") | Query("wind"); 8 | cout << "\n" << q << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chap02/Exer02_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i = 0, &r1 = i; 5 | double d = 0, &r2 = d; 6 | // all of the operations below are legal. 7 | r2 = 3.14159; 8 | r2 = r1; 9 | i = r2; 10 | r1 = d; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap06/Page224_makeplural.h: -------------------------------------------------------------------------------- 1 | // This function will be used frequently by subsequent chapters. 2 | #include 3 | using std::string; 4 | string make_plural(size_t ctr, const string &word, const string &ending) 5 | { 6 | return (ctr > 1) ? word + ending : word; 7 | } 8 | -------------------------------------------------------------------------------- /chap15/Exer15_27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer15_27_Bulk_quote.h" 3 | using std::cout; 4 | int main() 5 | { 6 | Bulk_quote item("978-7-121-20038-0", 128.0, 6, 0.05); 7 | print_total(cout, item, 10); // calls Bulk_quote::net_price 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /chap01/Exer01_22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_item.h" 3 | int main() 4 | { 5 | Sales_item book; 6 | Sales_item bookSum; 7 | while(std::cin >> book) 8 | { 9 | bookSum = bookSum + book; 10 | } 11 | 12 | std::cout << bookSum << std::endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap04/Exer04_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | int i1 = 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2; 7 | int i2 = ((12 /3) * 4) + (5 * 15) + ((24 % 4) / 2); 8 | cout << i1 << " " << i2 << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chap10/Exer10_21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | int i = 10; 7 | auto f = [&]() { return (--i <= 0) ? true : false; }; 8 | while(!f()) 9 | cout << i << " "; 10 | cout << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap01/Exer01_21.cpp: -------------------------------------------------------------------------------- 1 | #include "Sales_item.h" 2 | int main() 3 | { 4 | Sales_item book1; 5 | Sales_item book2; 6 | std::cout << "Input two sale items:" << std::endl; 7 | std::cin >> book1 >> book2; 8 | std::cout << book1 + book2 << std::endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chap03/Exer03_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | vector v; 10 | int i; 11 | while(cin >> i) 12 | v.push_back(i); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap06/Exer06_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | int abs(int n) 6 | { 7 | return (n >= 0) ? n : -n; 8 | } 9 | int main() 10 | { 11 | int n; 12 | while(cin >> n) 13 | cout << abs(n) << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap11/data/paragraph: -------------------------------------------------------------------------------- 1 | Although most programmers are familiar with data structures such as vectors and lists, many have never used an associative DATA structure. Before we look at the details of how the library supports these types, it will be helpful to start with examples of how we can use these containers. -------------------------------------------------------------------------------- /chap03/Exer03_01_2.cpp: -------------------------------------------------------------------------------- 1 | // Page 13, exercise 1.10 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | // while loop 8 | int j = 10; 9 | while(j >= 0) 10 | { 11 | cout << j << endl;; 12 | j--; 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap16/Exer16_20_print_iter.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER16_20_PRINT_ITER_H 2 | #define EXER16_20_PRINT_ITER_H 3 | #include 4 | template 5 | void print(const C &c) 6 | { 7 | for(auto iter = c.begin(); iter != c.end(); ++iter) 8 | std::cout << *iter << " "; 9 | } 10 | #endif 11 | -------------------------------------------------------------------------------- /chap01/Exer01_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | std::cout << "Enter two numbers:" << std::endl; 5 | int v1 = 0, v2 = 0; 6 | std::cin >> v1 >> v2; 7 | std::cout << "The sum of " << v1 << " and " << v2 8 | << " is " << v1 * v2 << std::endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chap01/Exer01_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | std::cout << "Enter some numbers:" << std::endl; 5 | int value, sum = 0; 6 | while(std::cin >> v1) 7 | sum += value; 8 | std::cout << "The sum of these values are: " << sum << std::endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /chap14/Exer14_34.h: -------------------------------------------------------------------------------- 1 | #ifndef FINAL_GRADE_H 2 | #define FINAL_GRADE_H 3 | #include 4 | using std::string; 5 | class FinalGrade { 6 | public: 7 | const string& operator()(bool grade, const string &s1, const string &s2) const { 8 | return grade ? s1 : s2; 9 | } 10 | }; 11 | #endif 12 | -------------------------------------------------------------------------------- /chap16/Exer16_41.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER16_41_SUM_H 2 | #define EXER16_41_SUM_H 3 | // using trailing return type, the return type is guaranteed to hold the result of sum 4 | template 5 | auto sum(T1 lhs, T2 rhs) -> decltype(lhs + rhs) 6 | { 7 | return lhs + rhs; 8 | } 9 | #endif 10 | -------------------------------------------------------------------------------- /chap01/Exer01_13_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int sum1 = 0, sum2 = 0; 5 | // for loop 6 | for(int i = 50; i <= 100; i++) 7 | { 8 | sum2 += i; 9 | } 10 | std::cout << "The sum of 50 to 100 is:" << sum1 << "," << sum2 << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap03/Exer03_02_2.cpp: -------------------------------------------------------------------------------- 1 | // Read a word a time 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string word; 11 | while(cin >> word) 12 | cout << word << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Clean or make all of the chapters. 2 | 3 | chapters = $(wildcard chap*) 4 | cleanchap = $(chapters) 5 | 6 | .PHONY: all $(chapters) clean $(cleanchap) 7 | 8 | all: $(chapters) 9 | 10 | $(chapters): 11 | $(MAKE) -C $@ 12 | 13 | clean: $(cleanchap) 14 | 15 | $(cleanchap): 16 | $(MAKE) -C $@ clean 17 | -------------------------------------------------------------------------------- /chap03/Exer03_32_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | vector a(10); 10 | for(int i = 0; i < 10; ++i) 11 | a[i] = i; 12 | vector a2 = a; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap06/Exer06_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int fact(int val) 5 | { 6 | int ret = 1; 7 | while(val > 1) 8 | ret *= val--; 9 | return ret; 10 | } 11 | int main() 12 | { 13 | int j = fact(12); 14 | cout << j << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap16/Exer16_05_print.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER16_05_PRINT_H 2 | #define EXER16_05_PRINT_H 3 | #include 4 | template 5 | void print(const V (&arr)[N]) 6 | { 7 | for(auto elem : arr) { 8 | std::cout << elem << " "; 9 | } 10 | std::cout << std::endl; 11 | } 12 | #endif 13 | -------------------------------------------------------------------------------- /chap16/Exer16_19_print_container.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER16_19_PRINT_CONTAINER_H 2 | #define EXER16_19_PRINT_CONTAINER_H 3 | #include 4 | template 5 | void print(const C &c) 6 | { 7 | for(typename C::size_type i = 0; i != c.size(); ++i) 8 | std::cout << c[i] << " "; 9 | } 10 | #endif 11 | -------------------------------------------------------------------------------- /chap17/data/store0: -------------------------------------------------------------------------------- 1 | 978-7-121-20038-0 5 128.0 2 | 978-7-121-20038-0 2 98.0 3 | 978-7-121-20038-0 1 100.0 4 | 978-0-553-21393-8 1 6.99 5 | 978-0-87779-855-2 2 6.99 6 | 978-7-121-12332-0 5 65.00 7 | 978-7-121-12332-0 1 59.00 8 | 978-7-121-12570-6 3 59.00 9 | 978-7-121-12570-6 3 49.00 10 | 978-7-5399-2460-1 1 15.00 11 | -------------------------------------------------------------------------------- /chap03/Exer03_02_1.cpp: -------------------------------------------------------------------------------- 1 | // Read a line at a time. 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string line; 11 | while(getline(cin, line)) 12 | cout << line << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap05/Exer05_15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | int sz = 9; 7 | // The space between ++ and sz is immaterial. 8 | for(int ix = 0; ix != sz; ++ix, ++ sz) 9 | { 10 | cout << ix << " " << sz << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap13/Exer13_14.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER13_14_H 2 | #define EXER13_14_H 3 | #include 4 | #include 5 | struct numbered { 6 | numbered() : mysn(rand()) {} 7 | int mysn = 0; 8 | }; 9 | void f(numbered s) { std::cout << s.mysn << std::endl; } // no copy constructor that generates new value 10 | #endif 11 | -------------------------------------------------------------------------------- /chap03/Exer03_15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cin; 5 | using std::string; 6 | using std::vector; 7 | int main() 8 | { 9 | vector svec; 10 | string str; 11 | while(cin >> str) { 12 | svec.push_back(str); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap03/Exer03_32_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | int main() 6 | { 7 | int array[10], array2[10]; 8 | for(int i = 0; i < 10; ++i) 9 | array[i] = i; 10 | for(int i = 0; i < 10; ++i) 11 | array2[i] = array[i]; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap06/Page239_inline.h: -------------------------------------------------------------------------------- 1 | #ifndef INILINE_TEST 2 | #define INILINE_TEST 3 | #include 4 | using std::string; 5 | inline const string &shorterString(const string &s1, const string &s2) 6 | { 7 | return s1.size() <= s2.size() ? s1 : s2; 8 | } 9 | constexpr int new_sz() 10 | { 11 | return 42; 12 | } 13 | #endif 14 | -------------------------------------------------------------------------------- /chap14/Exer14_27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer14_27_StrBlob.h" 3 | int main() 4 | { 5 | StrBlob v = {"Like", "a", "thunderbolt", "he", "falls"}; 6 | StrBlobPtr pv(v); 7 | for(; pv != v.end(); ++pv) 8 | std::cout << pv.deref() << " "; 9 | std::cout << std::endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /chap14/Page582_conversion.cpp: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | bool b = false; 4 | // error, bool is promoted to int because all of its possible values fit in 5 | // ant int(page 160) on a x64 machine; int is 32 bit wide, so 42 is larger 6 | // than the number of bits in the result 7 | b << 42; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /chap01/Exer01_08.cpp: -------------------------------------------------------------------------------- 1 | //Comment test. 2 | #include 3 | int main() 4 | { 5 | //Right 6 | std::cout << "/*"; 7 | std::cout << "*/"; 8 | std::cout << /* "*/" /* "/*" */; 9 | //Wrong 10 | // std::cout << /* "*/" */; 11 | std::cout << /* "*/" */"; // This is a possible remedy. 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap01/Exer01_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int sum1 = 0, sum2 = 0; 5 | // while loop 6 | int j = 50; 7 | while(j <= 100) 8 | { 9 | sum1 += j; 10 | j++; 11 | } 12 | std::cout << "The sum of 50 to 100 is:" << sum1 << "," << sum2 << std::endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap07/Exer07_27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer07_27_Screen.h" 4 | using std::cout; 5 | int main() 6 | { 7 | Screen myScreen(5, 5, 'X'); 8 | myScreen.move(4, 0).set('#').display(cout); 9 | cout << "\n"; 10 | myScreen.display(cout); 11 | cout << "\n"; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap07/Exer07_29.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer07_29_Screen.h" 4 | using std::cout; 5 | int main() 6 | { 7 | Screen myScreen(5, 5, 'X'); 8 | myScreen.move(4, 0).set('#').display(cout); 9 | cout << "\n"; 10 | myScreen.display(cout); 11 | cout << "\n"; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap16/Exer16_14_15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer16_14_15_Screen.h" 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | int main() 7 | { 8 | Screen<5, 5> scr('c'); 9 | Screen<5, 5> scr2; 10 | cout << scr << endl; 11 | cin >> scr2; 12 | cout << scr2 << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap06/Exer06_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | void fact() 6 | { 7 | int val; 8 | cin >> val; 9 | int ret = 1; 10 | while(val > 1) 11 | ret *= val--; 12 | cout << ret << endl; 13 | } 14 | int main() 15 | { 16 | fact(); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap06/Exer06_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | void reset(int &i) 6 | { 7 | i = 0; 8 | } 9 | int main() 10 | { 11 | int ival = 9; 12 | cout << "ival: " < 4 | TI find(TI begin, TI end, const TV &val) 5 | { 6 | while(begin != end) 7 | { 8 | if(*begin == val) 9 | return begin; 10 | ++begin; 11 | } 12 | return end; 13 | } 14 | #endif 15 | -------------------------------------------------------------------------------- /chap03/Exer03_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | using std::vector; 9 | int main() 10 | { 11 | vector v; 12 | string s; 13 | while(cin >> s) 14 | v.push_back(s); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap04/Exer04_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | int ia = -30 * 3 + 21 / 5; 7 | int ib = -30 + 3 * 21 / 5; 8 | int ic = 30 /3 * 21 % 5; 9 | int id = -30 / 3 * 21 % 4; 10 | cout << ia << " " << ib << " " << ic <<" " << id << " " << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap07/Exer07_34.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int height; 3 | class Screen { 4 | public: 5 | void dummy_fcn(pos height) { 6 | cursor = witdh * height; 7 | } 8 | private: 9 | pos cursor = 0; 10 | pos height = 0, width = 0; 11 | typedef std::string::size_type pos; 12 | }; 13 | int main() 14 | { 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap16/Exer16_05.cpp: -------------------------------------------------------------------------------- 1 | #include "Exer16_05_print.h" 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; 7 | const char p[] = "To be with you"; 8 | const char* str[] = {"one", "two", "three"}; 9 | print(arr); 10 | print(p); 11 | print(str); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap14/Page586_overload_conversion.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. It cannot compile. 2 | struct C { 3 | C(int) {} 4 | }; 5 | struct D { 6 | D(int) {} 7 | }; 8 | void manip(const C&); 9 | void manip(const D&); 10 | int main() 11 | { 12 | manip(10); // error ambiguous: manip(C(10)) or manip(D(10))? 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap16/Exer16_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer16_02_compare.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | cout << compare(1, 0) << endl; 10 | vector vec1{1, 2, 3}, vec2{4, 5, 6}; 11 | cout << compare(vec1, vec2) << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap18/Exer18_10_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer18_09_Sales_data.h" 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | int main() 7 | { 8 | Sales_data item1, item2, sum; 9 | while (cin >> item1 >> item2) { 10 | sum = item1 + item2; 11 | cout << sum << endl; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap06/Exer06_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | size_t countCalls() 7 | { 8 | static size_t callCtr = 0; 9 | return callCtr++; 10 | } 11 | int main() 12 | { 13 | for(int i = 0; i < 36; ++i) 14 | cout << countCalls() << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap07/Exer07_09.cpp: -------------------------------------------------------------------------------- 1 | // see Exer07_04_Person.h for function declarations and definitions. 2 | #include 3 | #include 4 | #include "Exer07_04_Person.h" 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | int main() 9 | { 10 | Person psn; 11 | read(cin, psn); 12 | print(cout, psn) << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap13/Exer13_16.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER13_14_H 2 | #define EXER13_14_H 3 | #include 4 | #include 5 | struct numbered { 6 | numbered() : mysn(rand()) {} 7 | numbered(const numbered&) : mysn(rand()) {} 8 | int mysn = 0; 9 | }; 10 | void f(const numbered &s) { std::cout << s.mysn << std::endl; } // correct implementation 11 | #endif 12 | -------------------------------------------------------------------------------- /chap04/Page138_evaluation.cpp: -------------------------------------------------------------------------------- 1 | // Order of evaluation 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | int i = 0, j = 1; 8 | // The results compiled by G++ and CL are both 1 1, but this doesn't mean 9 | // with any compiler the result is 1 1. 10 | cout << i << " " << ++i << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap06/Exer06_32.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int &get(int *arry, int index) 5 | { 6 | return arry[index]; 7 | } 8 | int main() 9 | { 10 | int ia[10]; 11 | for(int i = 0; i != 10; ++i) 12 | get(ia, i) = i; 13 | for(const auto ele : ia) 14 | cout << ele << " "; 15 | cout << endl; 16 | } 17 | -------------------------------------------------------------------------------- /chap10/Exer10_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | vector vi = { 7, 198, 39, 429, 345, 440, 66, 432, 17, 500, 331 }; 10 | auto sum = accumulate(vi.cbegin(), vi.cend(), 0); 11 | cout << sum << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap13/Exer13_14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer13_14.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | numbered a, b = a, c = b; 8 | f(a); f(b); f(c); 9 | cout << "Real value of a, b and c" << endl; 10 | cout << a.mysn << endl; 11 | cout << b.mysn << endl; 12 | cout << c.mysn << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap13/Exer13_15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer13_15.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | numbered a, b = a, c = b; 8 | f(a); f(b); f(c); 9 | cout << "Real value of a, b and c" << endl; 10 | cout << a.mysn << endl; 11 | cout << b.mysn << endl; 12 | cout << c.mysn << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap13/Exer13_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer13_16.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | numbered a, b = a, c = b; 8 | f(a); f(b); f(c); 9 | cout << "Real value of a, b and c" << endl; 10 | cout << a.mysn << endl; 11 | cout << b.mysn << endl; 12 | cout << c.mysn << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap13/Exer13_15.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER13_14_H 2 | #define EXER13_14_H 3 | #include 4 | #include 5 | struct numbered { 6 | numbered() : mysn(rand()) {} 7 | numbered(const numbered&) : mysn(rand()) {} 8 | int mysn = 0; 9 | }; 10 | void f(numbered s) { std::cout << s.mysn << std::endl; } // copy constructor always generates a new value 11 | #endif 12 | -------------------------------------------------------------------------------- /chap13/Exer13_13.h: -------------------------------------------------------------------------------- 1 | #ifndef X_H 2 | #define X_H 3 | #include 4 | struct X { 5 | X() {std::cout << "X()" << std::endl;} 6 | X(const X&) {std::cout << "X(const X&)" << std::endl;} 7 | X& operator=(const X&) { 8 | std::cout << "X(const X&)" << std::endl; 9 | return *this;} 10 | ~X() {std::cout << "~X()" << std::endl;} 11 | }; 12 | #endif 13 | -------------------------------------------------------------------------------- /chap15/Exer15_39/Exer15_39_Query.cpp: -------------------------------------------------------------------------------- 1 | #include "Exer15_39_Query.h" 2 | // constructor of Query that takes a string parameter 3 | // inline Query::Query(const std::string &s) : q(new WordQuery(s)) {} 4 | std::ostream& operator<<(std::ostream &os, const Query &query) 5 | { 6 | // Query::rep makes a virtual call through its Query_base pointer to rep() 7 | return os << query.rep(); 8 | } 9 | -------------------------------------------------------------------------------- /chap03/Exer03_42.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | vector v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 10 | int a[10]; 11 | for(int i = 0; i < 10; ++i) 12 | a[i] = v[i]; 13 | for(int j : a) 14 | cout << j << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap04/Exer04_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | short si1 = 32767; 7 | si1 += 1; 8 | unsigned short si2 = 0xFFFF; 9 | si2++; 10 | unsigned char ch = 256; 11 | // overflow, get a warning from g++, but nothing from cl 12 | cout << si1 << " "<< si2 << " " << ch << " " << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap13/Exer13_43.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer13_40_43_StrVec.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | StrVec v = {"Gone", "with", "the"}; 10 | v.push_back("winds"); 11 | for_each(v.begin(), v.end(), [](const string &s) {cout << s << " ";}); 12 | cout << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap03/Exer03_35.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cin; 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | constexpr size_t sz = 5; 8 | int a[sz] = {2, 3, 5, 7, 9}; 9 | int *p = a; 10 | for(int i = 0; i < sz; ++i) 11 | p[i] = 0; 12 | for(int j = 0; j < sz; ++j) 13 | cout << p[j] << " "; 14 | cout << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap01/Exer01_13_3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i1, i2, upper, lower; 5 | std::cout << "Input two numbers separated by space:" << std::endl; 6 | std::cin >> i1 >> i2; 7 | //if you don't compare v1 and v2, result may be wrong. 8 | // for loop 9 | for(int i = lower; i <= upper; ++i) 10 | std::cout << i << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap03/Exer03_01_1.cpp: -------------------------------------------------------------------------------- 1 | // Page 13, exercise 1.9 2 | #include 3 | using std::cin; 4 | using std::cout; 5 | using std::endl; 6 | int main() 7 | { 8 | int sum1 = 0; 9 | // while loop 10 | int j = 50; 11 | while(j <= 100) 12 | { 13 | sum1 += j; 14 | j++; 15 | } 16 | cout << "The sum of 50 to 100 is:" << sum1 << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap13/Exer13_53_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer13_53_HasPtr.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | HasPtr hp("Young and Beautiful"); 8 | HasPtr hp1; 9 | hp1 = hp; 10 | HasPtr hp2 = std::move(hp); // now hp can't be used again, because it refers to null 11 | hp2.print(cout) << endl; 12 | hp.print(cout); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap02/Exer02_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | unsigned u = 10, u2 = 42; 5 | std::cout << u2 - u << std::endl; 6 | std::cout << u - u2 << std::endl; 7 | int i = 10, i2 =42; 8 | std::cout << i2 - i << std::endl; 9 | std::cout << i - i2 << std::endl; 10 | std::cout << i - u << std::endl; 11 | std::cout << u - i << std::endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap07/Page298_aggregate.cpp: -------------------------------------------------------------------------------- 1 | #include "Exer07_49_Sales_data.h" 2 | struct Data { 3 | Sales_data sdata; 4 | }; 5 | int main() 6 | { 7 | // Even if the member is class-type, as long as we provide an argument 8 | // corresponding to a construct, it's okay. There is an implicit class type 9 | // conversion(see page 294). 10 | Data val = {string("978-0590353403")}; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap09/Exer09_42.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string str; 11 | str.reserve(100); // at least 100 elements 12 | cout << str.capacity() << endl; 13 | char ch; 14 | while(cin >> ch) 15 | str.push_back(ch); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap15/data/story.txt: -------------------------------------------------------------------------------- 1 | Alice Emma has long flowing red hair. 2 | Her Daddy says when the wind blows 3 | through her hair, it looks almost alive, 4 | like a fiery bird in flight. 5 | A beautiful fiery bird, he tells her, 6 | magical but untamed. 7 | "Daddy, shush, there is no such thing," 8 | she tells him, at the same time wanting 9 | him to tell her more. 10 | Shyly, she asks, "I mean, Daddy, is there?" -------------------------------------------------------------------------------- /chap01/Exer01_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | std::cout << "Enter two numbers:" << std::endl; 5 | int v1 = 0, v2 = 0; 6 | std::cin >> v1 >> v2; 7 | std::cout << "The sum of "; 8 | std::cout << v1; 9 | std::cout << " and "; 10 | std::cout << v2; 11 | std::cout << " is "; 12 | std::cout<< v1 * v2; 13 | std::cout << std::endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap03/Exer03_41.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::vector; 8 | using std::begin; 9 | using std::end; 10 | int main() 11 | { 12 | int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 13 | vector v(begin(a), end(a)); 14 | for(auto &i : v) 15 | cout << i << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap08/Page315_buffer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | using std::flush; 5 | using std::ends; 6 | int main() 7 | { 8 | cout << "hi!" << endl; // writes hi! and a newline, then flushes the buffer 9 | cout << "hi!" << flush; // writes hi!, then flushes the buffer 10 | cout << "hi!" << ends; // writes hi! and a null, then flushes the buffer 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap18/Exer18_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cerr; 5 | using std::endl; 6 | using std::exception; 7 | using std::runtime_error; 8 | int main() 9 | { 10 | try { 11 | throw runtime_error("runtime error!"); 12 | } catch(const exception &e) { 13 | cerr << e.what() << endl; 14 | abort(); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap03/Exer03_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | string text; 10 | getline(cin, text); 11 | if(!text.empty()) 12 | { 13 | for(auto c : text) 14 | if(!ispunct(c)) 15 | cout << c; 16 | cout << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap10/Exer10_34.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | using std::ostream_iterator; 8 | int main() 9 | { 10 | vector vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; 11 | ostream_iterator os_iter(cout, " "); 12 | copy(vec.crbegin(), vec.crend(), os_iter); 13 | cout << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap10/Exer10_35.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | using std::ostream_iterator; 8 | int main() 9 | { 10 | vector vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; 11 | ostream_iterator os_iter(cout, " "); 12 | copy(vec.cbegin(), vec.cend(), os_iter); 13 | cout << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap16/Exer16_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer16_06_begin_end.h" 4 | using std::cout; 5 | using std::endl; 6 | int main() 7 | { 8 | int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 9 | const int(&arr_r)[11] = arr; 10 | for(auto p = begin(arr_r); p != end(arr_r); ++p) 11 | { 12 | cout << *p << " "; 13 | } 14 | cout << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap19/Exer19_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Sales_data.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | Sales_data item("978-7-121-20038-0", 120, 128.0), *pitem = &item; 10 | const string Sales_data::*pdata = Sales_data::data(); 11 | cout << item.*pdata << endl; 12 | cout << pitem->*pdata << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap03/Exer03_40.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | int main() 7 | { 8 | char str1[] = "I'm string 1."; 9 | char str2[] = "I'm string 2."; 10 | const int len = strlen(str1) + strlen(str2) + 2; 11 | char str3[len]; 12 | strcpy(str3, str1); 13 | strcat(str3, str2); 14 | cout << str3 << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap04/Exer04_21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::vector; 6 | int main() 7 | { 8 | vector v = {1, 3, 2, 4, 6, 23, 11, 2, -3, -20, 0}; 9 | for(auto p = v.begin(); p != v.end(); ++p) 10 | *p = (*p % 2 != 0) ? (2 * *p) : *p; 11 | for(const int &i : v) 12 | cout << i << " "; 13 | cout << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap06/Page247_function_pointers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void foo() { std::cout << "foo" << std::endl; } 3 | int main() 4 | { 5 | void (&f) () = foo; 6 | void (*fp) () = foo; 7 | f(); 8 | fp(); 9 | return 0; 10 | } 11 | // Note: as the example above shows, we can also declare a function reference. 12 | // See the link below. 13 | // https://stackoverflow.com/questions/480248/function-references 14 | -------------------------------------------------------------------------------- /chap13/Page540_move.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Foo { 3 | public: 4 | Foo() = default; 5 | Foo(const Foo&) {} // copy constructor 6 | // other members, but Foo does not define a move constructor 7 | }; 8 | int main() 9 | { 10 | Foo x; 11 | Foo y(x); // copy constructor; x is an lvalue 12 | Foo z(std::move(x)); // copy constructor, because there is no move constructor 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /tools/SetUpObjectionFolder.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Add object folder under each chapter folder. 3 | for folder in `ls -F | grep "chap[0-9]"` 4 | do 5 | o="$folder/obj" # No space between expressions and '=' 6 | if [ -d $o ];then # -d is used to judge if $o is a directory 7 | echo "$o already exists" | sed 's/\/\//\//' 8 | else 9 | mkdir $folder/obj 10 | echo "$o is created" | sed 's/\/\//\//' 11 | fi 12 | done 13 | -------------------------------------------------------------------------------- /chap11/Exer11_15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::map; 5 | using std::vector; 6 | int main() 7 | { 8 | map> mp; 9 | map>::key_type map_key; // note: key_type of a map is not const!!! 10 | map_key = 3; 11 | std::cout << map_key << std::endl; 12 | map_key = 4; 13 | std::cout << map_key << std::endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap14/Exer14_48_49.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer14_48_49_Book.h" 3 | int main() 4 | { 5 | Book book1("Of Human Bondage", "W.Somerset Maugham", "978-0-553-21392-8"); 6 | Book book2; 7 | if(book1) 8 | std::cout << "True" << std::endl; 9 | if(book2) 10 | std::cout << "True" << std::endl; 11 | bool bk = static_cast(book2); // meaningless conversion 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap19/Exer19_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer19_02_StrVec.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | StrVec v1{"This", "is", "a", "new", "day"}, v2{"This", "is", "new", "day"}; 8 | if(v1 < v2) 9 | cout << "v1 < v2" << endl; 10 | else if(v1 == v2) 11 | cout << "v1 == v2" << endl; 12 | else 13 | cout << "v1 > v2" << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /tools/FormatFileName.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Add prefix 0 before exercises' chapter number. 3 | for folder in `ls -F | grep '/'` 4 | do 5 | for file in `ls $folder | grep '.cpp'` 6 | do 7 | newfile=`echo $file | sed 's/\(^Exer\)\([1-9]$\)/\10\2/'` 8 | if [ "$folder$file"x != "$folder$newfile"x ] 9 | then 10 | mv $folder$file $folder$newfile 11 | else 12 | echo "obj folder has existed" 13 | fi 14 | done 15 | done 16 | -------------------------------------------------------------------------------- /chap09/Exer09_15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::vector; 4 | int main() 5 | { 6 | vector v1 = {2, 4, 6, 8, 9, 11}; 7 | vector v2 = {2, 4, 6, 8, 2}; 8 | if(v1 == v2) 9 | std::cout << "v1 == v2" << std::endl; 10 | else if(v1 < v2) 11 | std::cout << "v1 < v2" << std::endl; 12 | else 13 | std::cout << "v1 > v2" << std::endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap12/Exer12_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::shared_ptr; 6 | void process(shared_ptr ptr) 7 | { 8 | cout << ptr.use_count() << endl; 9 | } 10 | int main() 11 | { 12 | shared_ptr p(new int(42)); 13 | cout << p.use_count() << endl; 14 | process(shared_ptr(p)); 15 | cout << p.use_count() << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap16/Exer16_06_begin_end.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER16_06_H 2 | #define EXER16_06_H 3 | template 4 | inline T* begin(T (&arr)[N]) // the parameter cannot be const, or we cannot make T* bound to arr 5 | { 6 | return arr; 7 | } 8 | template 9 | inline T* end(T (&arr)[N]) // the parameter cannot be const, or we cannot make T* bound to arr 10 | { 11 | return arr + N; 12 | } 13 | #endif 14 | -------------------------------------------------------------------------------- /chap19/Exer19_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_data.h" 3 | using std::cout; 4 | using std::endl; 5 | void debug_test(const Sales_data &item) 6 | { 7 | Sales_data::Avg price = &Sales_data::avg_price; 8 | cout << (item.*price)() << endl; 9 | } 10 | int main() 11 | { 12 | Sales_data item1("978-7-121-20038-0", 12, 128.0), item2("978-7-121-20038-0", 10, 98.0); 13 | debug_test(item1 + item2); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap06/Exer06_43.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for verification. It CANNOT be compiled successfully by both compilers. 2 | // The problem lies in line 9. 3 | #include 4 | #include 5 | #include "Page239_inline.h" 6 | using std::cout; 7 | using std::endl; 8 | // This is just for verification that ellipsis cannot be used this way. 9 | inline bool eq(const int&, const int&){...}; 10 | int main() 11 | { 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap01/4th/Exer1_16_4th.cpp: -------------------------------------------------------------------------------- 1 | // Expired: this is an exercise from 4th edition. 2 | #include 3 | int main() 4 | { 5 | std::cout << "Enter two numbers:" << std::endl; 6 | int v1, v2; 7 | std::cin >> v1 >> v2; 8 | int lower, upper; 9 | if(v1 <= v2) 10 | { 11 | std::cout << v2 << std::endl; 12 | } 13 | else 14 | { 15 | std::cout << v1 << std::endl; 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap12/Page473_weak_ptr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::make_shared; 3 | using std::shared_ptr; 4 | using std::weak_ptr; 5 | int main() 6 | { 7 | shared_ptr sp = make_shared(42); 8 | weak_ptr wp(sp); 9 | if(shared_ptr np = wp.lock()) //true if np is not null, an artful way to use condition 10 | {// inside if, np shares its object with sp 11 | // do something 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap06/Exer06_27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::initializer_list; 6 | int getSum(initializer_list lst) 7 | { 8 | int sum = 0; 9 | for(const auto &elem : lst) 10 | sum += elem; 11 | return sum; 12 | } 13 | int main() 14 | { 15 | cout << "sum of 1, 2, 3, 4, 5, 6: "; 16 | cout << getSum({1, 2, 3, 4, 5, 6}) << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap10/Exer10_04.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. Don't use accumulate this way. 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | int main() 9 | { 10 | vector vi = { 7, 19.8, 3.9, .429, 34.5, 4.40, 6.6, 43.2, 1.7, 5.00, 3.31 }; 11 | auto sum = accumulate(vi.cbegin(), vi.cend(), 0); 12 | cout << sum << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap14/Exer14_45.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer14_45_Sales_data.h" 3 | int main() 4 | { 5 | Sales_data item1("9-999999-99"); 6 | Sales_data item2("9-999999-99", 10, 28.0); 7 | if(item1 == string("9-999999-99")) // convert item to string 8 | std::cout << "True" << std::endl; 9 | if(item1 != item2) // call overloaded == operator of Sales_data 10 | std::cout << "False" << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap06/Exer06_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | void swap(int *pa, int *pb) 6 | { 7 | int temp; 8 | temp = *pa; 9 | *pa = *pb; 10 | *pb = temp; 11 | } 12 | int main() 13 | { 14 | int a, b; 15 | cin >> a >> b; 16 | cout << "Before swap: " << a << " " << b << endl; 17 | swap(&a, &b); 18 | cout << "After swap: " << a << " " << b << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap08/Exer08_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::istream; 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | istream& read(istream& is) 9 | { 10 | string word; 11 | while(!is.eof()) 12 | { 13 | is >> word; 14 | cout << word << endl; 15 | } 16 | is.clear(); 17 | return is; 18 | } 19 | int main() 20 | { 21 | read(cin); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap16/Exer16_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer16_05_print.h" 3 | #include "Exer16_07_size.h" 4 | using std::cout; 5 | using std::endl; 6 | int main() 7 | { 8 | int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 9 | const char *str[] = {"Never", "mind", "I", "will", "find", "someone", "like", "you"}; 10 | cout << size(arr) << endl; 11 | print(arr); 12 | cout << size(str) << endl; 13 | print(str); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap17/data/paragraph: -------------------------------------------------------------------------------- 1 | char ch; // using a char here invites disaster! 2 | // the return from cin.get is converted to char and then compared to an int 3 | while ((ch = cin.get()) != EOF) 4 | cout.put(ch); 5 | 6 | The problem is that when get returns EOF, that value will be converted to 7 | an unsigned char value. That converted value is no longer equal to the 8 | int value of EOF, and the loop will continue forever. Such errors are likely to be 9 | caught in testing. -------------------------------------------------------------------------------- /chap03/Exer03_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | string s; 10 | cin >> s; 11 | if(!s.empty()) 12 | { 13 | // If type of c is changed from 'auto &' to 'char', characters in s won't be changed. 14 | for(char c : s) 15 | c = 'X'; 16 | cout << s << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap04/Page148_increment.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for verification. It CANNOT be compiled successfully by every compiler. 2 | // The problem lies in line 10. 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | int main() 7 | { 8 | int i = 0; 9 | ++i = 5; // legal, prefix ++ returns i itself as a lvalue 10 | i++ = 5; // illegal, postfix ++ returns copy of origin i as an rvalue 11 | cout << i << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap06/Page241_assert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cin; 5 | using std::string; 6 | int main() 7 | { 8 | string::size_type threshold = 5; 9 | string word; 10 | while(cin >> word) 11 | { 12 | // a dreadful "Your program has stopped working" pops up if you input a 13 | // string shorter than 5 14 | assert(word.size() > threshold); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap03/Exer03_06.cpp: -------------------------------------------------------------------------------- 1 | // Make sure the string is not empty. That it doesn't matter here doesn't mean it's unnecessary. 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string s; 11 | cin >> s; 12 | if(!s.empty()) 13 | { 14 | for(auto &c : s) 15 | c = 'X'; 16 | cout << s << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap02/Exer02_35.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | const int i =42; 5 | auto j = i; // int 6 | const auto &k = i; // const reference(or say, reference to const) 7 | auto *p = &i; // pointer to const, const of i is not ignored, so it's const int * 8 | const auto j2 = i, &k2 = i; // j2, const int; i, const reference 9 | std::cout << j << " " << k << " " << *p << " " << j2 << " " << k2 << " " << std::endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /chap17/Exer17_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | using std::bitset; 9 | int main() 10 | { 11 | bitset<64> bitvec(32); 12 | bitset<32> bv(1010101); 13 | string bstr; 14 | cin >> bstr; 15 | bitset<8> bvs(bstr); 16 | cout << bitvec << endl; 17 | cout << bv << endl; 18 | cout << bvs << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap17/Page745_random_engine.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::size_t; 5 | using std::cout; 6 | using std::endl; 7 | using std::default_random_engine; 8 | int main() 9 | { 10 | default_random_engine e; // generates random unsigned integers 11 | for (size_t i = 0; i < 10; ++i) 12 | // e() "calls" the object to produce the next random number 13 | cout << e() << " "; 14 | cout << endl; 15 | } 16 | -------------------------------------------------------------------------------- /chap03/Exer03_25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | vector scores(11, 0); 10 | unsigned grade; 11 | while(cin >> grade) 12 | { 13 | if(grade <= 100) 14 | ++(*(scores.begin() + grade / 10)); 15 | } 16 | for(auto i : scores) 17 | cout << i << " "; 18 | cout << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap09/Exer09_19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cin; 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | using std::list; 9 | int main() 10 | { 11 | string word; 12 | list text; 13 | while(cin >> word) 14 | text.emplace_back(word); 15 | for(auto iter = text.begin(); iter != text.end(); ++iter) 16 | cout << *iter << " "; 17 | cout << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap11/Exer11_19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_data.h" 3 | using std::multiset; 4 | bool compareIsbn(const Sales_data& lhs, const Sales_data& rhs) 5 | { 6 | return lhs.isbn() < rhs.isbn(); 7 | } 8 | int main() 9 | { 10 | multiset bookstore(compareIsbn); 11 | multiset::iterator bookstore_it = bookstore.begin(); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap14/Exer14_36.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Exer14_35_ReadString.h" 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | using std::vector; 9 | int main() 10 | { 11 | ReadString reader; 12 | string s; 13 | vector vs; 14 | while((s = reader()) != ""){ 15 | vs.push_back(s); 16 | } 17 | for(const auto &s : vs) 18 | cout << s << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap17/Page749_seed_time.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::size_t; 6 | using std::cout; 7 | using std::endl; 8 | using std::default_random_engine; 9 | using std::uniform_int_distribution; 10 | int main() 11 | { 12 | default_random_engine e1(time(0)); // a somewhat random seed 13 | for (size_t i = 0; i < 10; ++i) 14 | cout << e1() << " "; 15 | cout << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap03/Exer03_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | using std::vector; 9 | int main() 10 | { 11 | vector v; 12 | string s; 13 | while(cin >> s) 14 | v.push_back(s); 15 | for(auto s : v) // auto can also decuce string 16 | if(!s.empty()) 17 | cout << s << " "; 18 | cout << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap04/Exer04_29.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | int x[10]; 7 | int *p = x; 8 | cout << sizeof(x)/sizeof(*x) << endl; 9 | cout << sizeof(p)/sizeof(*p) << endl; 10 | return 0; 11 | } 12 | 13 | // Note: on 64bit and 32 bit, result of 14 | // cout << sizeof(p)/sizeof(*p) << endl; 15 | // is different. This result depends on machine. 16 | // On 32bit machine, it's 1; 17 | // on 64bit machine, it's 2. 18 | -------------------------------------------------------------------------------- /chap09/Exer09_18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cin; 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | using std::deque; 9 | int main() 10 | { 11 | string word; 12 | deque text; 13 | while(cin >> word) 14 | text.emplace_back(word); 15 | for(auto iter = text.begin(); iter != text.end(); ++iter) 16 | cout << *iter << " "; 17 | cout << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap12/Exer12_24.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | size_t sz = 8; 11 | string str; 12 | while(getline(cin, str)) 13 | { 14 | char* ch_ptr = new char[str.size() + 1]; 15 | strcpy(ch_ptr, str.c_str()); 16 | cout << ch_ptr << endl; 17 | delete[] ch_ptr; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap16/Exer16_39.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer16_02_compare.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | // compare, the parameter is a const reference to pointer to a char 10 | cout << compare("this", "winter") << endl; 11 | // compare, recommended way, better than the last 12 | cout << compare("hi", "world") << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap16/Exer16_61.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer16_61_shared_ptr.h" 4 | using std::cout; 5 | using std::cerr; 6 | using std::endl; 7 | using std::vector; 8 | int main() 9 | { 10 | // call move constructor of vector to construct an object 11 | auto sp = make_shared>(vector{1, 2, 3, 4, 5}); 12 | for(auto p = sp->begin(); p != sp->end(); ++p) 13 | cout << *p << " "; 14 | cout << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap02/Exer02_42_1.cpp: -------------------------------------------------------------------------------- 1 | // Rewrite of Exer1.20 2 | #include 3 | #include "Sales_data.h" 4 | int main() 5 | { 6 | Sales_data book; 7 | double price = 0; 8 | while(std::cin >> book.bookNo >> book.units_sold >> price) 9 | { 10 | book.revenue = price * book.units_sold; 11 | std::cout << book.bookNo << " " 12 | << book.units_sold << " " 13 | << book.revenue << std::endl; 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap06/Exer06_22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | void swap(int* &a, int* &b) 5 | { 6 | int *tmp = a; 7 | a = b; 8 | b = tmp; 9 | tmp = 0; 10 | } 11 | int main() 12 | { 13 | int i = 3, j = 8; 14 | int *pi = &i, *pj = &j; 15 | cout << "Before swap: "; 16 | cout << *pi << " " << *pj << endl; 17 | swap(pi, pj); 18 | cout << "After swap: "; 19 | cout << *pi << " " << *pj << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap14/Exer14_34.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer14_34.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | FinalGrade fg; 10 | vector v = {99, 92, 60, 83, 76, 0, 11, 22, 73, 90, 56}; 11 | for(auto p = v.begin(); p != v.end(); ++p) 12 | { 13 | cout << "student " << p - v.begin() << ":\t"; 14 | cout << fg(*p > 60, "pass", "fail") << endl; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap02/Exer02_09.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for verification. It cannot be compiled successfully. 2 | #include 3 | int main() 4 | { 5 | // (a), it's illegal. 'Cause input_value isn't defined when the program tries to wite to it. 6 | std::cin >> int input_value; 7 | // (b), illegal, precision lost 8 | int i1 = { 3.14 }; 9 | // (c), illegal, wage not defined 10 | double salary = wage = 9999.9; 11 | // (d), legal 12 | int i2 = 3.14; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap01/4th/Exer1_17_4th.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | std::cout << "Enter some integers separated by white " << std::endl 5 | << "space, and end inputting by pressing enter:" << std::endl; 6 | int total = 0; 7 | int val; 8 | do{ 9 | std::cin >> val; 10 | if(val < 0) 11 | total += 1; 12 | }while(std::cin.get() != '\n'); 13 | std::cout << "Total number of negative is: " << total << std::endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap03/Exer03_21_1.cpp: -------------------------------------------------------------------------------- 1 | // Page 105, exercise 3.16 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::string; 9 | using std::vector; 10 | int main() 11 | { 12 | vector v; 13 | string s; 14 | while(cin >> s) 15 | v.push_back(s); 16 | auto item = v.begin(); 17 | for(; item != v.end(); ++item) 18 | cout << *item << " "; 19 | cout << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap03/Exer03_23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | vector v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 20}; 10 | for(auto i: v) 11 | cout << i << " "; 12 | cout << endl; 13 | for(auto item = v.begin(); item != v.end(); ++item) 14 | *item = *item << 1; 15 | for(auto i : v) 16 | cout << i << " "; 17 | cout << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap06/Exer06_25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::string; 6 | int main(int argc, char *argv[]) 7 | { 8 | string argStr; 9 | for(int i = 0; i != argc; ++i) 10 | argStr = argStr + argv[i] + " "; 11 | // Note: argv[0] contains program's name or is empty 12 | // Here we can take a glimpse of how those magnificant linux commands are 13 | // built. 14 | cout << argStr << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap01/4th/Exer1_18_4th.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int lower,upper; 5 | int v1, v2; 6 | std::cout << "Input two numbers:" << std::endl; 7 | std::cin >> v1 >> v2; 8 | if(v1 <= v2) 9 | { 10 | lower = v1; 11 | upper = v2; 12 | } 13 | else 14 | { 15 | lower = v2; 16 | upper = v1; 17 | } 18 | for(int i = lower; i <= upper; i++) 19 | { 20 | std::cout << i << " "; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap09/Page366_loopsearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::string; 6 | int main() 7 | { 8 | string numbers("0123456789"), name("r2d2 C-3PO"); 9 | string::size_type pos = 0; 10 | while((pos = name.find_first_of(numbers, pos)) != string::npos) 11 | { 12 | cout << "found number at index: " << pos 13 | << " element is " << name[pos] << endl; 14 | ++pos; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap06/Exer06_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | // here, reference is better than pointer. It's easy to see. 6 | void swap(int &a, int &b) 7 | { 8 | int temp; 9 | temp = a; 10 | a = b; 11 | b = temp; 12 | } 13 | int main() 14 | { 15 | int a, b; 16 | cin >> a >> b; 17 | cout << "Before swap: " << a << " " << b << endl; 18 | swap(a, b); 19 | cout << "After swap: " << a << " " << b << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap09/Page349_erase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::list; 6 | int main() 7 | { 8 | list lst = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 9 | auto it = lst.begin(); 10 | while(it != lst.end()) 11 | { 12 | if(*it % 2) 13 | it = lst.erase(it); 14 | else 15 | ++it; 16 | } 17 | for(const auto &ele : lst) 18 | cout << ele << " "; 19 | cout << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap16/Exer16_36.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for test. It cannot compile. 2 | // The problem lies in line 10. 3 | template f1(T, T) {} 4 | int main() 5 | { 6 | int i = 0, j = 42, *p1 = &i; 7 | const int *cp1 = &i, *cp2 = &j; 8 | int *const pc1 = &i; 9 | f1(cp1, pc1); // illegal: top-level const of pc1 is ignored, but cp1 is deduced as const int*, pc1 is deduced as int*, their types are different 10 | f1(pc1, p1); // legal: pc1 is deduced as int*, so is p1 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap03/Page130_typealias.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | int main() 6 | { 7 | using int_array = int[4]; 8 | typedef int int_array[4]; 9 | int_array ia[3] = {0}; 10 | // ia is an array of 3 elements, every of which is an array 4 ints. 11 | for(int_array *p = ia; p != ia + 3; ++p) 12 | { 13 | for(int *q = *p; q != *p + 4; ++q) 14 | cout << *q << ' '; 15 | cout << endl; 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap16/Exer16_56.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Exer16_56_errorMsg.h" 6 | using std::cerr; 7 | using std::endl; 8 | using std::string; 9 | using std::vector; 10 | using std::make_shared; 11 | int main() 12 | { 13 | int i = 2, *p = &i; 14 | string other = "other"; 15 | // calls print(os, const char[7]&, const int&, const int*&, const string&) 16 | errorMsg(cerr, "fcnName", __LINE__, p, other) << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap17/Exer17_28.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::size_t; 5 | using std::cout; 6 | using std::endl; 7 | using std::default_random_engine; 8 | using std::uniform_int_distribution; 9 | unsigned getNum() 10 | { 11 | static default_random_engine e; 12 | static uniform_int_distribution u(0, 100); 13 | return u(e); 14 | } 15 | int main() 16 | { 17 | for(size_t i = 0; i < 10; ++i) 18 | cout << getNum() << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap02/Page056_void.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int iobj = 0, *pi = &iobj; 5 | double dobj = 3.14, *pd = &dobj; 6 | void *pv = &dobj; 7 | // compare void pointer with another type. 8 | if(pv == pd) 9 | std::cout << "void pointer can be compared with any pointer." << std::endl; 10 | pv = pi; 11 | if(pv == pd) 12 | std::cout << "pv is equal to pd." << std::endl; 13 | else 14 | std::cout << "pv isn't equal to pd." << std::endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap03/Exer03_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | string word, line1, line2; 10 | while(cin >> word) 11 | { 12 | line1 += word; // concatenated without space 13 | line2 = line2 + word + " "; // separated with space 14 | } 15 | if(!line1.empty()) 16 | cout << line1 << endl; 17 | if(!line2.empty()) 18 | cout << line2 << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap09/Exer09_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::list; 5 | using std::vector; 6 | int main() 7 | { 8 | list lst1 = {2, 4, 6, 8, 9, 11}; 9 | vector v1 = {2, 4, 6, 8, 2}; 10 | vector v2(lst1.begin(), lst1.end()); 11 | if(v1 == v2) 12 | std::cout << "lst1 == v1" << std::endl; 13 | else if(v1 < v2) 14 | std::cout << "lst1 < v1" << std::endl; 15 | else 16 | std::cout << "lst1 > v1" << std::endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap13/Page532_rvalue_reference.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. It cannot compile. 2 | // The problem lies in line 7 and 8. 3 | int main() 4 | { 5 | int i = 42; 6 | int &r = i; // ok: r refers to i 7 | int &&rr = i;// error: cannot bind an rvalue reference to an lvalue 8 | int &r2 = i * 42; // error: i * 42 is an rvalue 9 | const int &r3 = i * 42; // ok: we can bind a reference to const to an rvalue 10 | int &&rr2 = i * 42; // ok: bind rr2 to the result of the multiplication 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /chap14/Exer14_30.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer14_30_StrBlob.h" 4 | using std::size_t; 5 | int main() 6 | { 7 | StrBlob v = {"Like", "a", "thunderbolt", "he", "falls"}; 8 | StrBlobPtr p1(v), p2 = p1, p3 = p1; 9 | ConstStrBlobPtr pc(v); 10 | for(size_t i = 0; i != v.size(); ++i) 11 | std::cout << *(p1 + i) << " "; 12 | std::cout << std::endl; 13 | p2 += 2; 14 | std::cout << *pc << std::endl; 15 | std::cout << pc->size() << std::endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap16/Exer16_41.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer16_41.h" 4 | using std::cout; 5 | using std::endl; 6 | int main() 7 | { 8 | int i = 2; 9 | // std::numeric_limits::max gets the maximum value of type T, see 10 | // http://en.cppreference.com/w/cpp/types/numeric_limits 11 | long long lng = std::numeric_limits::max() - 20; 12 | cout << "i = " << i << endl; 13 | cout << "lng = " << lng << endl; 14 | cout << "i + lng = " << sum(i, lng) << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap16/Exer16_45.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for illustration. Template declarations and definitions should be put into header files. 2 | // Warning: this program cannot compile. The problem lies in line 12. 3 | #include 4 | using std::vector; 5 | template 6 | void g(T&& val) 7 | { vector v; } 8 | int main() 9 | { 10 | int i = 42; 11 | g(42); // ok: T is int; val is int&; define a vector of int 12 | g(i); // error: T is int&; val is int&&; cannot define a vector of reference 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap05/Exer05_24.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::runtime_error; 7 | int main() 8 | { 9 | int i1, i2; 10 | while(cin >> i1 >> i2) 11 | { 12 | if(i2 == 0) 13 | throw runtime_error("Divider mustn't be zero!"); 14 | cout << i1 / i2 << endl; 15 | } 16 | return 0; 17 | } 18 | // If there is no catch block, program just terminates with a dreadful tip 19 | // saying that "Your program has stopped working." 20 | -------------------------------------------------------------------------------- /chap06/Exer06_33.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::vector; 6 | void printVec(const vector &vec, int beg, int size) 7 | { 8 | if(vec.empty()) 9 | return; 10 | if(size - beg > 0) 11 | { 12 | cout << vec[beg] << " "; 13 | printVec(vec, ++beg, size); 14 | } 15 | } 16 | int main() 17 | { 18 | vector vi = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 19 | printVec(vi, 0, vi.size()); 20 | cout << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap14/Exer14_35_ReadString.h: -------------------------------------------------------------------------------- 1 | #ifndef READ_STRING_H 2 | #define READ_STRING_H 3 | #include 4 | #include 5 | using std::cin; 6 | using std::istream; 7 | using std::string; 8 | class ReadString { 9 | public: 10 | ReadString(istream &i = cin) : is(i) {} 11 | string operator()() const; 12 | private: 13 | istream &is; 14 | }; 15 | string ReadString::operator()() const 16 | { 17 | string s; 18 | if(getline(is, s)) 19 | return s; 20 | else { 21 | s = ""; 22 | } 23 | } 24 | #endif 25 | -------------------------------------------------------------------------------- /chap17/Exer17_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | using std::bitset; 8 | int main() 9 | { 10 | vector vec = { 1, 2, 3, 5, 8, 13, 21 }; 11 | unsigned u = 0U; 12 | for(auto i : vec) 13 | u |= (1U << i); 14 | bitset<22> bitvec1(u); 15 | bitset<22> bitvec2; 16 | for(auto i : vec) 17 | bitvec2[i] = 1; 18 | cout << bitvec1 << endl; 19 | cout << bitvec2 << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap17/Exer17_01_02.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. It cannot compile. 2 | // The problem lies in line 14. 3 | #include 4 | #include 5 | #include 6 | #include 7 | using std::string; 8 | using std::vector; 9 | using std::pair; 10 | using std::tuple; 11 | int main() 12 | { 13 | tuple threeI(10, 20, 30); 14 | tuple threeI2 = {10, 20, 30}; // error, explicit constructor 15 | tuple, pair> strFamily; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap07/Exer07_32.cpp: -------------------------------------------------------------------------------- 1 | // Note: this exercise requires careful structuring. See notes in header file 2 | // for details. 3 | #include 4 | #include "Exer07_32_Screen.h" 5 | int main() 6 | { 7 | Screen s(24, 80, 'x'); 8 | Window_mgr win(s); // initialize win with s; 9 | std::cout << "The original win:" << std::endl; 10 | win.get(0).display(std::cout); 11 | std::cout << std::endl; 12 | win.clear(0); 13 | std::cout << "The cleared win:" << std::endl; 14 | win.get(0).display(std::cout); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap11/Exer11_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::string; 9 | using std::vector; 10 | using std::pair; 11 | int main() 12 | { 13 | string str; 14 | int i; 15 | vector> data; 16 | while(cin >> str >> i) 17 | { 18 | data.push_back(make_pair(str, i)); 19 | } 20 | for(auto &p : data) 21 | cout << p.first << " " << p.second << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap01/4th/Exer1_17_1_4th.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | std::cout << "Enter some integers separated by white space," << std::endl 5 | << "and end inputting by pressing enter:" << std::endl; 6 | int total = 0; 7 | int val; 8 | while(std::cin >> val)//In this way, only when meeting end-of-line(ctrl+Z in Windows) does the loop exit 9 | { 10 | if(val < 0) 11 | total += 1; 12 | } 13 | std::cout << "Total number of negative is: " << total << std::endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap13/Exer13_18.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | class Employee { 4 | public: 5 | Employee() : id(++count) {} 6 | Employee(const std::string& s) : name(s), id(++count){} 7 | Employee(const Employee& e) : name(e.name), id(++count) {} 8 | Employee& operator=(const Employee&); 9 | ~Employee() { --count; } 10 | private: 11 | std::string name; 12 | unsigned id; 13 | static unsigned count; 14 | }; 15 | Employee& Employee::operator=(const Employee& e) 16 | { 17 | name = e.name; 18 | id = ++count; 19 | } 20 | -------------------------------------------------------------------------------- /chap15/Exer15_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer15_01_Quote.h" 3 | #include "Exer15_05_Bulk_quote.h" 4 | using std::cout; 5 | int main() 6 | { 7 | Quote item1("978-7-121-20038-0", 128.0); 8 | Bulk_quote item2("978-7-121-20038-0", 128.0, 6, 0.05); 9 | print_total(cout, item1, 10); // calls Quote::net_price 10 | print_total(cout, item2, 10); // calls Bulk_quote::net_price 11 | print_total(cout, item1, 5); // calls Quote::net_price 12 | print_total(cout, item2, 5); // calls Bulk_quote::net_price 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap08/Page316_tie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::istream; 3 | using std::ostream; 4 | using std::cin; 5 | using std::cout; 6 | using std::cerr; 7 | using std::endl; 8 | int main() 9 | { 10 | ostream *old_tie = cin.tie(nullptr); 11 | ostream *new_tie = cin.tie(&cerr); 12 | // tie(ostream *) will return an ostream pointer that points to the 13 | // previously tied object 14 | if(!new_tie) 15 | (*old_tie) << "cin is now tied to nothing" << endl; // old_tie points to cout 16 | cin.tie(old_tie); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap12/Page460_newdelete.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. It cannot compile by both compilers. 2 | // The problems lie in line 7 to 11. 3 | int main() 4 | { 5 | int i, *pi1 = &i, *pi2 = nullptr; 6 | double *pd(new double(33)), *pd2 = pd; 7 | delete i; // error: i is not a pointer 8 | delete pi1; // undefined: pi1 refers to a local 9 | delete pd; // ok 10 | delete pd2; // undefined: the momery pointed to by pd2 was already deletd 11 | delete pi2; // ok: it is always ok to delete a null pointer 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /chap16/Exer16_11.cpp: -------------------------------------------------------------------------------- 1 | template class ListItem; 2 | template class List { 3 | public: 4 | List(); 5 | List(const List &); 6 | List& operator=(const List &); 7 | ~List(); 8 | void insert(ListItem *ptr, elemType value); 9 | // we may use the name of the template itself without arguments inside class 10 | // but we can never omit template arguments of other class templates 11 | private: 12 | ListItem *front, *end; 13 | }; 14 | -------------------------------------------------------------------------------- /chap16/Exer16_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer16_16_Vec.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | Vec v = {'H', 'e', 'l', 'l', 'o', ',', 'w', 'o', 'r', 'l', 'd', '!'}; 8 | for(const auto &c : v) 9 | cout << c; 10 | cout << endl; 11 | v.push_back(':'); 12 | v.push_back(')'); 13 | cout << v.size() << endl; 14 | cout << v.capacity() << endl; 15 | for(auto iter = v.begin(); iter != v.end(); ++iter) 16 | cout << *iter; 17 | cout << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap16/Exer16_21_DebugDelete.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER16_21_DEBUG_DELETE_H 2 | #define EXER16_21_DEBUG_DELETE_H 3 | #include 4 | // function-object class that calls delete on a given pointer 5 | class DebugDelete { 6 | public: 7 | DebugDelete(std::ostream &s = std::cerr) : os(s) {} 8 | // as with any function template, the type of T is deduced by the compiler 9 | template 10 | void operator()(T *p) const 11 | { os << "deleting shared_ptr" << std::endl; delete p; } 12 | private: 13 | std::ostream &os; 14 | }; 15 | #endif 16 | -------------------------------------------------------------------------------- /chap19/Exer19_17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer19_17_Screen.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | Screen::Action1 get1 = &Screen::get; 8 | Screen::Action2 get2 = &Screen::get; 9 | Screen::Action3 mv = &Screen::move; 10 | Screen myScreen(20, 30, 'o'), *pScreen = &myScreen; 11 | cout << (myScreen.*get1)() << endl; // call get() 12 | cout << (pScreen->*get2)(0, 0) << endl; // call get(pos, pos) 13 | (myScreen.*mv)(0, 0); // call move(pos, pos) 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap19/Page823_placement_new.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::string; 6 | int main() 7 | { 8 | string* sp = new string("a value"); // allocate and initialize a string 9 | cout << *sp << endl; 10 | sp->~string(); // destroy but not deallocate the string 11 | new (sp) string("another value"); // reconstruct the string 12 | cout << *sp << endl; 13 | delete sp; // destroy and deallocate the string 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap07/Exer07_53_Debug.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER07_53_H 2 | #define EXER07_53_H 3 | #include 4 | using std::string; 5 | class Debug { 6 | public: 7 | constexpr Debug(bool b = true) : hw(b), io(b), other(b) {} 8 | constexpr Debug(bool h, bool i, bool o) : hw(h), io(i), other(o) {} 9 | constexpr bool any() { return hw || io || other; } 10 | void set_hw(bool b) { hw = b; } 11 | void set_io(bool b) { io = b; } 12 | void set_other(bool b) { other = b; } 13 | private: 14 | bool hw; 15 | bool io; 16 | bool other; 17 | }; 18 | #endif 19 | -------------------------------------------------------------------------------- /chap13/Exer13_31.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Page511_540_HasPtr.h" 5 | using std::string; 6 | using std::vector; 7 | int main() 8 | { 9 | vector vec = { HasPtr("This"), HasPtr("is"), HasPtr("a"), HasPtr("tiger") }; 10 | sort(vec.begin(), vec.end()); 11 | for(const auto &h : vec) 12 | h.print(std::cout) << " "; 13 | std::cout << std::endl; 14 | return 0; 15 | } 16 | // Note: the code above doesn't use the swap of HasPtr version! It still uses the 17 | // default version. 18 | -------------------------------------------------------------------------------- /chap01/Exer01_19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int lower,upper; 5 | int v1, v2; 6 | std::cout << "Input two numbers:" << std::endl; 7 | std::cin >> v1 >> v2; 8 | if(v1 <= v2) 9 | { 10 | lower = v1; 11 | upper = v2; 12 | } 13 | else 14 | { 15 | lower = v2; 16 | upper = v1; 17 | } 18 | for(int i = lower; i <= upper; i++) 19 | { 20 | std::cout << i << " "; 21 | if((i - lower) % 10 == 9) 22 | std::cout << std::endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap15/Page618_name_collisions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | struct Base { 3 | Base() : mem(0) {} 4 | protected: 5 | int mem; 6 | }; 7 | struct Derived : Base { 8 | Derived(int i) : mem(i) {} // initializes Derived::mem to id 9 | // Base::mem is default initialized 10 | int get_mem() { return mem; } //returns Derived::mem 11 | protected: 12 | int mem; // hides mem in Base 13 | }; 14 | int main() 15 | { 16 | Derived d(42); 17 | std::cout << d.get_mem() << std::endl; // prints 42 rather than 0 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap02/Exer02_41_1.cpp: -------------------------------------------------------------------------------- 1 | // Rewrite of Exer1.20 2 | #include 3 | struct Sales_data { 4 | std::string bookNo; 5 | unsigned units_sold = 0; 6 | double revenue = 0.0; 7 | }; 8 | int main() 9 | { 10 | Sales_data book; 11 | double price = 0; 12 | while(std::cin >> book.bookNo >> book.units_sold >> price) 13 | { 14 | book.revenue = price * book.units_sold; 15 | std::cout << book.bookNo << " " 16 | << book.units_sold << " " 17 | << book.revenue << std::endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap08/Exer08_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::istream; 5 | using std::istringstream; 6 | using std::cout; 7 | using std::cin; 8 | using std::endl; 9 | using std::string; 10 | istream& read(istream& is) 11 | { 12 | string word; 13 | while(!is.eof()) 14 | { 15 | is >> word; 16 | cout << word << endl; 17 | } 18 | is.clear(); 19 | return is; 20 | } 21 | int main() 22 | { 23 | string str = "John has a book."; 24 | istringstream is(str); 25 | read(is); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chap18/Exer18_20_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | namespace primerLib { 3 | void compute() { std::cout << "compute()" << std::endl; } 4 | void compute(const void*) { std::cout << "compute(const void*)" << std::endl; } 5 | } 6 | using primerLib::compute; 7 | void compute(int) { std::cout << "compute(int)" << std::endl; } 8 | void compute(double, double = 3.4) { std::cout << "compute(double, double)" << std::endl; } 9 | void compute(char*, char* = 0) { std::cout << "compute(char*, char*)" << std::endl; } 10 | int main() 11 | { 12 | compute(0); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap05/Exer05_20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | string s, sLast; 10 | bool repeated = false; 11 | while(cin >> s && !s.empty()) 12 | { 13 | if(s == sLast) 14 | { 15 | cout << s << endl; 16 | repeated = true; 17 | break; 18 | } 19 | else 20 | sLast = s; 21 | } 22 | if(!repeated) 23 | cout << "No word was repeated." << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap18/Exer18_10_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer18_09_Sales_data.h" 3 | using std::cout; 4 | using std::cerr; 5 | using std::cin; 6 | using std::endl; 7 | int main() 8 | { 9 | Sales_data item1, item2, sum; 10 | while (cin >> item1 >> item2) { 11 | try { 12 | sum = item1 + item2; 13 | cout << sum << endl; 14 | } catch(const isbn_mismatch &e) { 15 | cerr << e.what() << ": left isbn(" << e.left 16 | << ") right isbn(" << e.right << ")" << endl; 17 | } 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap18/Exer18_20_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | namespace primerLib { 3 | void compute() { std::cout << "compute()" << std::endl; } 4 | void compute(const void*) { std::cout << "compute(const void*)" << std::endl; } 5 | } 6 | void compute(int) { std::cout << "compute(int)" << std::endl; } 7 | void compute(double, double = 3.4) { std::cout << "compute(double, double)" << std::endl; } 8 | void compute(char*, char* = 0) { std::cout << "compute(char*, char*)" << std::endl; } 9 | int main() 10 | { 11 | using primerLib::compute; 12 | compute(0); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /chap04/Exer04_31.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::vector; 6 | int main() 7 | { 8 | vector ivec(10); 9 | vector::size_type cnt = ivec.size(); 10 | // Here, whether you use 11 | // (++ix, --cnt), (++ix, cnt--), (ix++, --cnt) or (ix++, cnt--) 12 | // You are doomed to the same way. 13 | for(vector::size_type ix = 0; ix != ivec.size(); ++ix, --cnt) 14 | ivec[ix] = cnt; 15 | for(auto i : ivec) 16 | cout << i << " "; 17 | cout << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap09/Exer09_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cin; 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | using std::list; 9 | 10 | int main() 11 | { 12 | list li1{3, 6, 4, 5, 9}; 13 | vector vi1{8, 9, 10, 1, 2}; 14 | vector vd1(li1.begin(), li1.end()); 15 | vector vd2(vi1.begin(), vi1.end()); 16 | for(auto d : vd1) 17 | cout << d << " "; 18 | cout << endl; 19 | for(auto d : vd2) 20 | cout << d << " "; 21 | cout << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap16/Exer16_44_2.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. Template declarations and definitions should be put into header files. 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | template 6 | void g(const T& val) { T t; t = 42; } 7 | int main() 8 | { 9 | int i = 0; 10 | const int ci = i; 11 | g(i); // ok: T is int; val is const int& 12 | g(ci); // ok: T is int(const won't overlap); val is const int&; 13 | g(i * ci); // ok: T is int(although i * ci yields an rvalue, see page 687); val is const int& 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /chap10/Page405_ostreamiterator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | using std::ostream_iterator; 9 | int main() 10 | { 11 | vector ivec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 12 | ostream_iterator out_iter(cout, " "); 13 | for(auto e : ivec) 14 | *out_iter++ = e; // equivalent to out_iter = e, but more readable and general 15 | cout << endl; 16 | copy(ivec.begin(), ivec.end(), out_iter); 17 | cout << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap16/Exer16_62.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Exer16_62_Sales_data.h" 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | using std::unordered_set; 9 | int main() 10 | { 11 | unordered_set bookStore; 12 | bookStore.insert(Sales_data("978-7-121-20038-0", 20, 128.00)); 13 | bookStore.emplace("978-7-121-20038-0", 50, 99.00); 14 | bookStore.insert(Sales_data("978-7-87779-855-0", 20, 42.00)); 15 | for(const auto &item : bookStore) 16 | cout << item << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap02/Page061_const.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for verification. It cannot be compiled successfully. 2 | // The problem lies in line 12. 3 | #include 4 | int main() 5 | { 6 | int i = 42; 7 | const int &r1 = i; 8 | const int &r2 = 42; 9 | const int &r3 = r1 * 2; 10 | // illegal, r4 is not const, so it cannot be initialized by an expression 11 | int &r4 = i * 2; 12 | 13 | double dval = 3.14; 14 | //legal even if type doesn't match, because ri is actually bond to a temporary variable created by compiler. 15 | const int &ri = dval; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap09/Exer09_33.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for illustration. It has runtime error. 2 | // The problem lies in line 15. 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | int main() 9 | { 10 | vector v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 11 | auto begin = v.begin(); 12 | while(begin != v.end()) 13 | { 14 | ++begin; 15 | v.insert(begin,42); // lead to infinite loop 16 | ++begin; 17 | } 18 | for(auto &i : v) 19 | cout << i << " "; 20 | cout << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap10/Exer10_05_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | const char* test = "test it"; 9 | void *p = &test; 10 | const auto &refer = test; 11 | int i = 2; 12 | int *pi = &i; 13 | 14 | cout << test << "===" << refer << endl; 15 | cout << static_cast(test) << "\t" << &test << '\t' << p << endl; 16 | cout << static_cast(refer) << "\t" << &refer << endl; 17 | cout << i << "\t" << &i << endl; 18 | cout << *pi << "\t" << pi << "\t" << &pi << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap13/Page517_swap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Page511_540_HasPtr.h" 4 | using std::cout; 5 | using std::endl; 6 | class Foo { 7 | friend void swap(Foo &lhs, Foo &rhs); 8 | public: 9 | Foo(HasPtr p) : h(p) {} 10 | private: 11 | HasPtr h; 12 | }; 13 | void swap(Foo &lhs, Foo &rhs) 14 | { 15 | using std::swap; 16 | swap(lhs.h, rhs.h); // this way, friend function swap of HasPtr will be called 17 | } 18 | int main() 19 | { 20 | Foo f1(HasPtr("string1")); 21 | Foo f2(HasPtr("string2")); 22 | swap(f1, f2); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chap10/Exer10_36.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::list; 8 | using std::ostream_iterator; 9 | int main() 10 | { 11 | list lst = { 1, 2, 3, 0, 4, 5, 6, 7, 8, 9 }; 12 | ostream_iterator os_iter(cout, " "); 13 | auto last_zero = find(lst.crbegin(), lst.crend(), 0); 14 | // the input range must be both reverse_iterators or plain iterators, don't mix them together 15 | copy(lst.cbegin(), last_zero.base(), os_iter); 16 | cout << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap16/Exer16_19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Exer16_19_print_container.h" 6 | using std::cout; 7 | using std::endl; 8 | using std::string; 9 | using std::vector; 10 | using std::deque; 11 | int main() 12 | { 13 | vector vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 14 | deque dq = { "like", "a", "thunderbolt", "he", "falls" }; 15 | string s("Templates"); 16 | print(vec); 17 | cout << endl; 18 | print(dq); 19 | cout << endl; 20 | print(s); 21 | cout << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap12/Exer12_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::make_shared; 6 | using std::shared_ptr; 7 | void process(shared_ptr ptr) 8 | { 9 | cout << ptr.use_count() << endl; 10 | } 11 | int main() 12 | { 13 | auto sp = make_shared(42); 14 | cout << sp.use_count() << endl; 15 | auto p = sp.get(); 16 | cout << sp.use_count() << endl; 17 | delete p; // error: this frees memory sp points to 18 | cout << sp.use_count() << endl; 19 | *sp = 32; // error: sp becomes a dangling pointer 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap17/Exer17_29.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::size_t; 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::default_random_engine; 9 | using std::uniform_int_distribution; 10 | unsigned getNum(unsigned seed = 0) 11 | { 12 | static default_random_engine e(seed); 13 | static uniform_int_distribution u(0, 100); 14 | return u(e); 15 | } 16 | int main() 17 | { 18 | unsigned seed; 19 | cin >> seed; 20 | for(size_t i = 0; i < 10; ++i) 21 | cout << getNum(seed) << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap09/Exer09_46.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::string; 6 | string& address(string& name, const string& prefix, const string& suffix) 7 | { 8 | if(name.empty() && prefix.empty() && suffix.empty()) 9 | return name; 10 | name.insert(0, prefix); 11 | name.insert(name.size(),suffix); 12 | return name; 13 | } 14 | int main() 15 | { 16 | string lady = "Christie"; 17 | string gentleman; 18 | address(gentleman, "Mr.", ""); 19 | cout << lady << endl; 20 | cout << gentleman << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap15/Exer15_15_Disc_quote.h: -------------------------------------------------------------------------------- 1 | #ifndef DISC_QUOTE_H 2 | #define DISC_QUOTE_H 3 | #include 4 | #include 5 | #include "Exer15_15_Quote.h" 6 | class Disc_quote : public Quote { 7 | public: 8 | Disc_quote() = default; 9 | Disc_quote(const std::string &book, double price, std::size_t qty, double disc): 10 | Quote(book, price), quantity(qty), discount(disc) {} 11 | double net_price(std::size_t) const = 0; 12 | protected: 13 | std::size_t quantity = 0; // purchase size for the discount to apply 14 | double discount = 0.0; // fractional discount to apply 15 | }; 16 | #endif 17 | -------------------------------------------------------------------------------- /chap19/Exer19_01_new_delete.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER19_01_MEMORY_H 2 | #define EXER19_01_MEMORY_H 3 | #include 4 | #include 5 | #include 6 | #include 7 | inline void *operator new(std::size_t size) 8 | { 9 | std::cout << "You are using self-defined version of new!" << std::endl; 10 | if(void* mem = std::malloc(size)) 11 | return mem; 12 | else 13 | throw std::bad_alloc(); 14 | } 15 | inline void operator delete(void* mem) noexcept 16 | { 17 | std::cout << "You are using self-defined version of delete!" << std::endl; 18 | free(mem); 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /chap05/Exer05_17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::vector; 6 | int main() 7 | { 8 | vector v1 = {0, 1, 3, 2}, v2 = {0, 1, 1, 2, 3, 5, 8}; 9 | bool match = false; 10 | auto len = (v1.size() <= v2.size()) ? v1.size() : v2.size(); 11 | decltype(len) i = 0; 12 | for(; i < len && v1[i] == v2[i]; ++i) 13 | { /* No operation is needed. */} 14 | match = !(i < len); 15 | if(match) 16 | cout << "v1 and v2 match." << endl; 17 | else 18 | cout << "v1 and v2 don't match." << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap18/Exer18_23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | class A { 5 | public: 6 | A() { cout << "A()" << endl; } 7 | }; 8 | class B : public A { 9 | public: 10 | B() : A() { cout << "B()" << endl; } 11 | }; 12 | class C : public B { 13 | public: 14 | C() : B() { cout << "C()" << endl; } 15 | }; 16 | class X { 17 | public: 18 | X() { cout << "X()" << endl; } 19 | }; 20 | class D : public X, public C {}; 21 | int main() 22 | { 23 | D *pd = new D; 24 | X *px = pd; 25 | A *pa = pd; 26 | B *pb = pd; 27 | C *pc = pd; 28 | delete pd; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /chap04/Exer04_22_1.cpp: -------------------------------------------------------------------------------- 1 | // conditional version 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | using std::vector; 9 | int main() 10 | { 11 | vector v = {99, 92, 60, 83, 76, 0, 11, 22, 73, 90, 56}; 12 | string result; 13 | for(auto p = v.begin(); p != v.end(); ++p) 14 | { 15 | result = *p > 90 ? "high pass" 16 | : (*p > 75) ? "pass" 17 | : (*p > 60) ? "low pass" : "fail"; 18 | cout << result << " "; 19 | } 20 | cout << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap10/Exer10_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | using std::list; 9 | int main() 10 | { 11 | vector ivec(10); 12 | fill_n(ivec.begin(), ivec.size(), 9); // use a container that has nonzero size 13 | list ilst; 14 | fill_n(back_inserter(ilst), 10, 5); // use inserter iterator 15 | for(const auto &i : ivec) 16 | cout << i << " "; 17 | cout << endl; 18 | for(const auto &i : ilst) 19 | cout << i << " "; 20 | cout << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap15/Exer15_39/Exer15_39_OrQuery.h: -------------------------------------------------------------------------------- 1 | #ifndef OR_QUERY_H 2 | #define OR_QUERY_H 3 | #include 4 | #include "Exer15_39_TextQuery.h" 5 | #include "Exer15_39_BinaryQuery.h" 6 | #include "Exer15_39_Query.h" 7 | class OrQuery : public BinaryQuery { 8 | friend Query operator|(const Query&, const Query&); 9 | OrQuery(const Query &left, const Query &right) : 10 | BinaryQuery(left, right, "|") {} 11 | QueryResult eval(const TextQuery&) const; 12 | }; 13 | inline Query operator|(const Query &lhs, const Query &rhs) 14 | { 15 | return std::shared_ptr(new OrQuery(lhs, rhs)); 16 | } 17 | #endif 18 | -------------------------------------------------------------------------------- /chap16/Exer16_18.h: -------------------------------------------------------------------------------- 1 | template void f1(T, U, V); 2 | // every template parameter must be preceded by keyword typename or class 3 | template T f2(int &T); 4 | // can not reuse the name that are declared as a template parameter name 5 | inline template T foo(T, unsigned int*); 6 | // inline should be put between parameter list and return type 7 | template f4(T, T); 8 | // no return type 9 | typedef char Ctype(); 10 | template Ctype f5(Ctype a); 11 | // ok; but Ctype in template is not an alias of char; it'll be instantiated when f5 is used 12 | -------------------------------------------------------------------------------- /chap06/Exer06_55_56.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | int add(int a, int b) 8 | { 9 | return a + b; 10 | } 11 | int sub(int a, int b) 12 | { 13 | return a - b; 14 | } 15 | int mul(int a, int b) 16 | { 17 | return a * b; 18 | } 19 | int divi(int a, int b) 20 | { 21 | assert(b != 0); 22 | return a / b; 23 | } 24 | int main() 25 | { 26 | using PF = int(*)(int, int); 27 | vector vfun = {add, sub, mul, divi}; 28 | for(auto f : vfun) 29 | cout << f(48, 6) << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /chap09/Exer09_45.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::string; 6 | string& address(string& name, const string& prefix, const string& suffix) 7 | { 8 | if(name.empty() && prefix.empty() && suffix.empty()) 9 | return name; 10 | name.insert(name.begin(), prefix.begin(), prefix.end()); 11 | name.append(suffix); 12 | return name; 13 | } 14 | int main() 15 | { 16 | string lady = "Christie"; 17 | string gentleman; 18 | address(gentleman, "", ""); 19 | cout << lady << endl; 20 | cout << gentleman << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap14/Exer14_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_data.h" 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | int main() 7 | { 8 | Sales_data item(cin); 9 | cout << item << endl; 10 | return 0; 11 | } 12 | // ******result****** 13 | // 0-201-99999-9 10 24.95 14 | // 0-201-99999-9 10 249.5 24.95 15 | // 16 | // 10 24.95 0-201-99999-9 17 | // 10 24 22.8 0.95 18 | 19 | // Note: as the result above shows, the second input is illegal but still yield 20 | // an unexpected result. Because istream interpret 0-201-99999-9 as a double. 21 | // This implies just checking if istream fails is not enough. 22 | -------------------------------------------------------------------------------- /chap19/Exer19_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::type_info; 6 | class A { 7 | public: 8 | A() = default; 9 | virtual ~A() = default; 10 | }; 11 | class B : public A {}; 12 | class C : public B {}; 13 | int main() 14 | { 15 | A *pa = new C; 16 | cout << typeid(pa).name() << endl; 17 | C cobj; 18 | A &ra = cobj; 19 | cout << typeid(&ra).name() << endl; 20 | B *px = new B; 21 | A &ra1 = *px; 22 | cout << typeid(ra1).name() << endl; 23 | } 24 | // ******result of cl****** 25 | // class A * 26 | // class A * 27 | // class B 28 | -------------------------------------------------------------------------------- /chap01/Exer01_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i1, i2, upper, lower; 5 | std::cout << "Input two numbers separated by space:" << std::endl; 6 | std::cin >> i1 >> i2; 7 | if(i1 < i2) 8 | { 9 | lower = i1; 10 | upper = i2; 11 | } 12 | else 13 | { 14 | upper = i1; 15 | lower = i2; 16 | } 17 | //if you don't compare v1 and v2, result may be wrong. 18 | // while loop 19 | int iwhile = lower; 20 | while(iwhile <= upper) 21 | { 22 | std::cout << iwhile << std::endl; 23 | ++iwhile; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /chap06/Exer06_42.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | string make_plural(size_t ctr, const string &word, const string &ending = "s") 8 | { 9 | return (ctr > 1) ? word + ending : word; 10 | } 11 | int main() 12 | { 13 | string s1 = "success"; 14 | string s2 = "failure"; 15 | cout << make_plural(1, s1) << endl; 16 | cout << make_plural(2, s1, "es") << endl; // use non default argument 17 | cout << make_plural(1, s2) << endl; 18 | cout << make_plural(2, s2) << endl; // use default argument 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap09/Exer09_50.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | using std::vector; 8 | int main() 9 | { 10 | vector si{ "7", "198", "39", "429", "345", "440", "66", "432", "17", "500", "331" }; 11 | vector sd{ "-3.0016", "2.4498", "3.6968", "8.5594", "-0.9706", "-10.6917", "-4.1979" }; 12 | int sumi = 0; 13 | double sumd = 0; 14 | for(auto i : si) 15 | sumi += stoi(i); 16 | for(auto d : sd) 17 | sumd += stod(d); 18 | cout << sumi << endl; 19 | cout << sumd << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap07/Exer07_49.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer07_49_Sales_data.h" 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | Sales_data item("978-7-121-20038-0", 10, 128); 11 | string s = "978-7-121-20038-0"; 12 | // for Sales_data(const string&) 13 | // combine #(a) and combine #(c) work, combine #(b) doesn't compile 14 | // for explicit Sales_data(const string&) 15 | // combine #(a) , combine #(b), combine #(c) won't compile 16 | item.combine(s); 17 | return 0; 18 | } 19 | // Note: see Exer07_49.h for combine #(a), #(b), #(c) 20 | -------------------------------------------------------------------------------- /chap10/Exer10_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | using std::vector; 9 | bool isLongWord(const string& str) 10 | { 11 | return str.size() >= 5; 12 | } 13 | int main() 14 | { 15 | vector words = { "the", "quick", "red", "fox", "jump", "over", "the", "slow", "red", "turtle" }; 16 | auto longer_words = partition(words.begin(), words.end(), isLongWord); 17 | for(auto iter = words.begin(); iter != longer_words; ++iter) 18 | cout << *iter << " "; 19 | cout << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap10/Exer10_30.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::vector; 9 | using std::istream_iterator; 10 | using std::ostream_iterator; 11 | int main() 12 | { 13 | istream_iterator in_iter(cin), eof; 14 | ostream_iterator out_iter(cout, " "); 15 | vector vec(in_iter, eof); 16 | // we cannot use sort for istream_iterator, so we have to store it at first 17 | sort(vec.begin(), vec.end()); 18 | copy(vec.begin(), vec.end(), out_iter); 19 | cout << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap04/Exer04_22_2.cpp: -------------------------------------------------------------------------------- 1 | // if version 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | vector v = {99, 92, 60, 83, 76, 0, 11, 22, 73, 90, 56}; 10 | for(auto p = v.begin(); p != v.end(); ++p) 11 | { 12 | if(*p > 90) 13 | cout << "high pass" << " "; 14 | if(*p > 75 && *p <= 90) 15 | cout << "pass" << " "; 16 | if(*p > 60 && *p <= 75) 17 | cout << "low pass" << " "; 18 | if(*p <= 60) 19 | cout << "fail" << " "; 20 | } 21 | cout << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap10/Exer10_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::vector; 8 | int main() 9 | { 10 | vector iv; 11 | int i, val; 12 | // read a sequence of ints 13 | while(cin >> i) 14 | { 15 | iv.push_back(i); 16 | } 17 | // read a value to be counted 18 | cin.clear(); // if not, we can't input anything, see page 313, 8.1.2 Condition States 19 | cin >> val; 20 | // count how often val appears 21 | auto cnt = count(iv.begin(), iv.end(), val); 22 | cout << cnt << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chap10/Exer10_17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Sales_data.h" 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::vector; 9 | int main() 10 | { 11 | vector sales_item; 12 | Sales_data item; 13 | while(read(cin, item)) 14 | { 15 | sales_item.push_back(item); 16 | } 17 | sort(sales_item.begin(), sales_item.end(), 18 | [](const Sales_data& sd1, const Sales_data& sd2){return sd1.isbn() < sd2.isbn();}); 19 | for(const auto &s : sales_item) 20 | { 21 | print(cout, s) << endl; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chap10/Exer10_31.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::vector; 9 | using std::istream_iterator; 10 | using std::ostream_iterator; 11 | int main() 12 | { 13 | istream_iterator in_iter(cin), eof; 14 | ostream_iterator out_iter(cout, " "); 15 | vector vec(in_iter, eof); 16 | // we cannot use sort with istream_iterator, so we have to store it at first 17 | sort(vec.begin(), vec.end()); 18 | unique_copy(vec.begin(), vec.end(), out_iter); 19 | cout << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap17/data/quiz: -------------------------------------------------------------------------------- 1 | 1. All trees except pine trees molt their leaves in the fall. 2 | 2. Water is less dense in its liquid state. 3 | 3. Dieting combined with consistent exercise will usually aid in weight loss. 4 | 4. More women suffer from anorexia than men because men don't worry about appearance as much. 5 | 5. In car technology the Japanese far exceed the Americans. 6 | 6. Shakespeare wrote the Bible. 7 | 7. Planets orbiting the sun sometimes eclipse other heavenly bodies. 8 | 8. Hospitals on occasion are good places for the spread of disease. 9 | 9. Most salmon die after spawning. 10 | 10. Stalactites suspend from cave roof tops as well as stalagmites. -------------------------------------------------------------------------------- /chap01/4th/Exer1_15_4th.cpp: -------------------------------------------------------------------------------- 1 | // Expired: this is an exercise from 4th edition. 2 | #include 3 | int main() 4 | { 5 | std::cout << "Enter two numbers:" << std::endl; 6 | int v1, v2; 7 | std::cin >> v1 >> v2; 8 | int lower, upper; 9 | if(v1 <= v2) 10 | { 11 | lower = v1; 12 | upper = v2; 13 | } 14 | else 15 | { 16 | lower = v2; 17 | upper = v1; 18 | } 19 | int sum = 0; 20 | for(int val = lower; val <= upper; ++val) 21 | sum += val; 22 | 23 | std::cout << "Sum of " << lower << " to " << upper << " inclusive is " << sum << std::endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap03/Exer03_22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | using std::vector; 9 | int main() 10 | { 11 | vector text; 12 | string word; 13 | while(getline(cin, word)) 14 | text.push_back(word); 15 | for(auto it = text.begin(); it != text.end() && !it->empty(); ++it) 16 | { 17 | for(auto ct = it->begin(); ct != it->end(); ++ct) 18 | { 19 | if(islower(*ct)) 20 | *ct = toupper(*ct); 21 | } 22 | cout << *it << " "; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap17/Page731_regex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | using std::regex; 9 | using std::smatch; 10 | int main() 11 | { 12 | // one or more alphanumeric characters followed by a '.' followed by "cpp" or "cxx" or "cc" 13 | regex r("[[:alnum:]]+\\.(cpp|cxx|cc)$", regex::icase); // case independent 14 | smatch results; 15 | string filename; 16 | while (cin >> filename) 17 | if (regex_search(filename, results, r)) 18 | cout << results.str() << endl; // print the current match 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap05/Exer05_21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | string s, sLast; 11 | bool repeated = false; 12 | while(cin >> s && !s.empty()) 13 | { 14 | if(!isupper(s[0])) 15 | continue; 16 | if(s == sLast) 17 | { 18 | cout << s << endl; 19 | repeated = true; 20 | break; 21 | } 22 | else 23 | sLast = s; 24 | } 25 | if(!repeated) 26 | cout << "No word was repeated." << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /chap15/Exer15_39/Exer15_39_WordQuery.h: -------------------------------------------------------------------------------- 1 | #ifndef WORD_QUERY_H 2 | #define WORD_QUERY_H 3 | #include 4 | #include "Exer15_39_TextQuery.h" 5 | #include "Exer15_39_Query_base.h" 6 | class WordQuery : public Query_base { 7 | friend class Query; // Query uses the WordQuery constructors 8 | WordQuery(const std::string &s) : query_word(s) {} 9 | // concrete class: WordQuery defines all inherited pure virtual functions 10 | QueryResult eval(const TextQuery &t) const 11 | { return t.query(query_word); } 12 | std::string rep() const { return query_word; } 13 | std::string query_word; // word for which to search 14 | }; 15 | #endif 16 | -------------------------------------------------------------------------------- /chap16/Exer16_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "Exer16_04_find.h" 6 | using std::cout; 7 | using std::endl; 8 | using std::string; 9 | using std::vector; 10 | using std::list; 11 | int main() 12 | { 13 | vector vec{1, 2, 5, 8, 0, 12, 98, 29}; 14 | list lst{"Gone", "are", "the", "days"}; 15 | string val = "day"; 16 | auto v = find(vec.cbegin(), vec.cend(), 8); 17 | auto l = find(lst.cbegin(), lst.cend(), val); 18 | cout << v - vec.begin() << endl; 19 | if(l == lst.end()) 20 | cout << val << " is not in lst" << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap05/Exer05_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int sum = 0, val = 1; 5 | while(val <= 10) 6 | sum += val, ++val; 7 | std::cout << "sum of 1 to 10 inclusive is " 8 | << sum << std::endl; 9 | return 0; 10 | } 11 | // Note: Substituting a comma expression for a block is okay. But if there are many 12 | // statements, a chain of comma expressions will make the program drastically 13 | // unreadable. It'll perplex others and the programmer himself. If there's any 14 | // bug in one gigantic and long-winded expression, the programmer cannot locate 15 | // the problem easily even if he uses a sophisticated debugger. 16 | -------------------------------------------------------------------------------- /chap15/Exer15_28.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer15_28_29_Bulk_quote.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | vector basket; 10 | basket.push_back(Bulk_quote("978-7-121-20038-0", 128.0, 6, 0.05)); 11 | basket.push_back(Bulk_quote("7-115-13771-4", 59.0, 10, 0.15)); 12 | basket.push_back(Bulk_quote("978-7-111-40701-0", 128.0, 15, 0.2)); 13 | basket.push_back(Bulk_quote("978-0-87779-855-2", 56.0, 5, 0.1)); 14 | double sum = 0.0; 15 | for(const auto &c : basket) 16 | sum += c.net_price(10); 17 | cout << sum << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap15/Exer15_39/Exer15_39_Query_base.h: -------------------------------------------------------------------------------- 1 | // abstract class acts as a base for concrete query types; all members are private 2 | #ifndef QUERY_BASE_H 3 | #define QUERY_BASE_H 4 | #include 5 | #include "Exer15_39_TextQuery.h" 6 | class Query_base { 7 | friend class Query; 8 | protected: 9 | using line_no = TextQuery::line_no; // used in eval functions 10 | virtual ~Query_base() = default; 11 | private: 12 | // eval returns the QueryResult that matches this Query 13 | virtual QueryResult eval(const TextQuery&) const = 0; 14 | // rep is a string representing of the query 15 | virtual std::string rep() const = 0; 16 | }; 17 | #endif 18 | -------------------------------------------------------------------------------- /chap16/Exer16_48.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer16_48_debug_rep.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | string s("hi"); 10 | const string *sp = &s; 11 | char a[] = "How are you?"; 12 | char *p = a; 13 | cout << debug_rep(s) << "\n" << endl; // call debug_rep(const string&) 14 | cout << debug_rep(sp) << "\n" << endl; // call debug_rep(T*), instantiated to debug_rep(const string*) 15 | cout << debug_rep("hi world!") << "\n" << endl; // call debug_rep(const char*) 16 | cout << debug_rep(p) << endl; // call debug_rep(char*) 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap03/Exer03_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | string s1, s2; 10 | int i = 0; 11 | cin >> s1 >> s2; 12 | if(!s1.empty()){ 13 | // Compared to ways below, range for is the most simplest. 14 | while(i != s1.size()) 15 | { 16 | s1[i] = 'X'; 17 | ++i; 18 | } 19 | cout << s1 << endl; 20 | } 21 | if(!s2.empty()) 22 | { 23 | for(i = 0; i < s2.size(); ++i) 24 | s2[i] = 'X'; 25 | cout << s2 << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /chap09/Exer09_41.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | using std::vector; 8 | int main() 9 | { 10 | vector vch = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '!', '!' }; 11 | char* cp = &vch[0]; 12 | // way1, don't forget sting can be initialized with common way of a container 13 | string str1(vch.cbegin(), vch.cend()); 14 | string str2(cp, vch.size()); // don't know why, even if we don't provide vch.size(), the result still 15 | // the same 16 | cout << str1 << endl; 17 | cout << str2 << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap16/Exer16_43.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. Template declarations and definitions should be put into header files. 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | template 6 | void g(T&& val) { val = 2; } 7 | int main() 8 | { 9 | int i = 0; 10 | const int ci = i; 11 | g(i = ci); 12 | // ok: T is int&(assignment returns reference to its left-hand operand, an lvalue; 13 | // pass an lvalue; T is reference to lvalue, int& &; collapses to int&); 14 | // val is int&(int& && collapses to int&) 15 | cout << i << endl; // i = 2(pass reference; any change on val will change i) 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap16/Exer16_65.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer16_65_debug_rep.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | string s("hi"); 10 | const string *sp = &s; 11 | char a[] = "How are you?"; 12 | char *p = a; 13 | cout << debug_rep(s) << "\n" << endl; // call debug_rep(const string&) 14 | // call debug_rep(T*), instantiated to debug_rep(const string*) 15 | cout << debug_rep(sp) << "\n" << endl; 16 | cout << debug_rep("hi world!") << "\n" << endl; // call debug_rep(const char*) 17 | cout << debug_rep(p) << endl; // call debug_rep(char*) 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap14/Exer14_43.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | using std::modulus; 9 | using namespace std::placeholders; 10 | int main() 11 | { 12 | vector vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 13 | vector vi; 14 | int val = 1000; 15 | auto cnt = count_if(vec.begin(), vec.end(), bind(modulus(), val, _1)); 16 | if(cnt == vec.size()) 17 | cout << val << " can't be divided by any value in vec." << endl; 18 | else 19 | cout << val << " can be divided by some values in vec." << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap09/Exer09_27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::forward_list; 6 | int main() 7 | { 8 | forward_list flst = { 12, 73, 35, 13, 65, 86, 0 }; 9 | auto prev = flst.before_begin(); 10 | auto curr = flst.begin(); 11 | while(curr != flst.end()) 12 | { 13 | if(*curr % 2) 14 | curr = flst.erase_after(prev); 15 | else 16 | { 17 | prev = curr; 18 | ++curr; 19 | } 20 | } 21 | for(const auto &ele : flst) // output the remaining elements 22 | cout << ele << " "; 23 | cout << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap17/Exer17_30.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::size_t; 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::default_random_engine; 9 | using std::uniform_int_distribution; 10 | unsigned getNum(unsigned seed = 0, unsigned min = 0, unsigned max = 32767) 11 | { 12 | static default_random_engine e(seed); 13 | static uniform_int_distribution u(min, max); 14 | return u(e); 15 | } 16 | int main() 17 | { 18 | unsigned seed, min, max; 19 | cin >> seed >> min >> max; 20 | for(size_t i = 0; i < 10; ++i) 21 | cout << getNum(seed, min, max) << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap13/Exer13_44.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer13_44_47_String.h" 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | String s1("And like a thunderbolt he falls."); 10 | cout << s1.size() << " " << s1.capacity() << endl; 11 | s1.push_back('M'); 12 | cout << s1.size() << " " << s1.capacity() << endl; 13 | s1.push_back('S'); 14 | cout << s1.size() << " " << s1.capacity() << endl; 15 | String s2 = "1"; 16 | cout << s2.size() << " " << s2.capacity() << endl; 17 | s2 = s2; 18 | String s3(s1); 19 | cout << s3.size() << " " << s3.capacity() << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap06/Page218_arrayref.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for verification. It CANNOT be compiled successfully by both compilers. 2 | // The problems lie in line 16, 17 and 19. 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | void print(int (&arr)[10]) 7 | { 8 | for(auto elem : arr) 9 | cout << elem << endl; 10 | } 11 | int main() 12 | { 13 | int i = 0, j[2] = {0, 1}; 14 | int k[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 15 | int *p = k; 16 | print(&i); // error: &i is not an array of ten 10 ints 17 | print(j); // error: j is an array of 2 ints 18 | print(k); // ok 19 | print(p); // error: this is equivalent of line 16 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap10/Exer10_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Sales_data.h" 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::vector; 9 | bool compareIsbn(const Sales_data& sd1, const Sales_data& sd2) 10 | { 11 | return sd1.isbn() < sd2.isbn(); 12 | } 13 | int main() 14 | { 15 | vector sales_item; 16 | Sales_data item; 17 | while(read(cin, item)) 18 | { 19 | sales_item.push_back(item); 20 | } 21 | sort(sales_item.begin(), sales_item.end(), compareIsbn); 22 | for(const auto &s : sales_item) 23 | { 24 | print(cout, s) << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chap10/Page406_streamiterator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Sales_item.h" 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::istream_iterator; 8 | using std::ostream_iterator; 9 | int main() 10 | { 11 | istream_iterator item_iter(cin), eof; 12 | ostream_iterator out_iter(cout, "\n"); 13 | Sales_item sum = *item_iter++; 14 | while(item_iter != eof) 15 | { 16 | if(item_iter->isbn() == sum.isbn()) 17 | sum += *item_iter++; 18 | else{ 19 | out_iter = sum; 20 | sum = *item_iter; 21 | } 22 | } 23 | out_iter = sum; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap03/Exer03_01_3.cpp: -------------------------------------------------------------------------------- 1 | // Page 13, exercise 11 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | int main() 7 | { 8 | int i1, i2, upper, lower; 9 | cout << "Input two numbers separated by space:" << endl; 10 | cin >> i1 >> i2; 11 | if(i1 < i2) 12 | { 13 | lower = i1; 14 | upper = i2; 15 | } 16 | else 17 | { 18 | upper = i1; 19 | lower = i2; 20 | } 21 | // if you don't compare v1 and v2, result may be wrong. 22 | // while loop 23 | int iwhile = lower; 24 | while(iwhile <= upper) 25 | { 26 | cout << iwhile << endl; 27 | ++iwhile; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /chap09/Exer09_34.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for illustration. It has runtime error. 2 | // The problem lies in line 12-15. 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | int main() 9 | { 10 | vector v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 11 | auto iter = v.begin(); 12 | while(iter != v.end()) // error: lacking curly braces 13 | if(*iter % 2) 14 | iter = v.insert(iter, *iter); // error: infinite loop, add iter by 2 after each insert 15 | ++iter; // error: add iter by 1 if if condition is false 16 | for(auto &i : v) 17 | cout << i << " "; 18 | cout << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /chap12/Exer12_26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | using std::allocator; 9 | int main() 10 | { 11 | string::size_type n = 10; 12 | string s; 13 | allocator alloc; 14 | auto const p = alloc.allocate(n); 15 | auto q = p; 16 | while(cin >> s && q != p + n) 17 | alloc.construct(q++, s); 18 | for(auto ps = p; ps != q; ++ps) 19 | cout << *ps << " "; 20 | cout << endl; 21 | // destroy the objects 22 | while(q != p) 23 | alloc.destroy(--q); 24 | // free memory 25 | alloc.deallocate(p, n); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chap15/Exer15_39/Exer15_39_BinaryQuery.h: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_QUERY_H 2 | #define BINARY_QUERY_H 3 | #include 4 | #include "Exer15_39_Query_base.h" 5 | #include "Exer15_39_Query.h" 6 | class BinaryQuery : public Query_base { 7 | protected: 8 | BinaryQuery(const Query &l, const Query &r, std::string s) : 9 | lhs(l), rhs(r), opSym(s) {} 10 | // abstract class: BinaryQuery doesn't define eval 11 | std::string rep() const { 12 | return "(" + lhs.rep() + " " 13 | + opSym + " " 14 | + rhs.rep() + ")"; 15 | } 16 | Query lhs, rhs; // right and left-hand operand 17 | std::string opSym; // name of the operator 18 | }; 19 | #endif 20 | -------------------------------------------------------------------------------- /chap15/Exer15_39/Exer15_39_NotQuery.h: -------------------------------------------------------------------------------- 1 | #ifndef NOT_QUERY_H 2 | #define NOT_QUERY_H 3 | #include 4 | #include 5 | #include "Exer15_39_TextQuery.h" 6 | #include "Exer15_39_Query_base.h" 7 | #include "Exer15_39_Query.h" 8 | class NotQuery : public Query_base { 9 | friend Query operator~(const Query&); 10 | NotQuery(const Query &q) : query(q) {} 11 | // concrete class: NotQuery defines all inherited pure virtual functoins 12 | std::string rep() const {} 13 | QueryResult eval(const TextQuery&) const; 14 | Query query; 15 | }; 16 | inline Query operator~(const Query &operand) 17 | { 18 | return std::shared_ptr(new NotQuery(operand)); 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /chap04/Makefile: -------------------------------------------------------------------------------- 1 | # This makefile eliminates all of the source file names by using builtin 2 | # functions of make. It uses $@ to refer to the current target and $< the first 3 | # of corresponding prerequisites. The `-' before `g++' tells make to ignore 4 | # errors and keep going on. Except these, this one is the same as the one in 5 | # chapter 3. 6 | 7 | objdir := obj 8 | objects := $(addprefix $(objdir)/,$(patsubst %.cpp,%.exe,$(wildcard *.cpp))) 9 | 10 | all: $(objects) 11 | 12 | # add `-' before a command to ignore errors 13 | $(objdir)/%.exe: %.cpp 14 | -g++ -std=c++11 -Wall -o $@ $< 15 | 16 | $(objects): | $(objdir) 17 | 18 | $(objdir): 19 | mkdir $(objdir) 20 | 21 | clean: 22 | rm $(objdir)/* 23 | -------------------------------------------------------------------------------- /chap05/Exer05_25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::runtime_error; 7 | int main() 8 | { 9 | int i1, i2; 10 | while(cin >> i1 >> i2) 11 | { 12 | try{ 13 | if(i2 == 0) 14 | throw runtime_error("Divider mustn't be zero!"); 15 | cout << i1 / i2 << endl; 16 | } 17 | // with a catch, the program could move on peacefully. 18 | catch(runtime_error err){ 19 | cout << err.what() << "\nEnter another divider:" << endl; 20 | cin >> i2; 21 | cout << i1 / i2 << endl; 22 | } 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap16/Exer16_44_1.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. Template declarations and definitions should be put into header files. 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | template 6 | void g(T val) { val = 2; } 7 | int main() 8 | { 9 | int i = 0; 10 | const int ci = i; 11 | g(i); // ok: T is int; val is int 12 | cout << i << endl; // assignment to val doesn't affect i 13 | g(ci); // ok: T is int(top level const is discarde); val is int; 14 | cout << ci << endl; // assignment to val doesn't affect i 15 | g(i * ci); // ok: T is int; val is T 16 | cout << i << endl; // assignment to val affect change i 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap19/Exer19_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer19_12_Screen.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | void ptrTest() 8 | { 9 | const string Screen::*pdata; 10 | pdata = &Screen::contents; // must be in class or in a friend 11 | Screen myScreen(20, 20, 'x'), *pScreen = &myScreen; 12 | cout << myScreen.*pdata << endl; 13 | cout << pScreen->*pdata << endl; 14 | } 15 | int main() 16 | { 17 | ptrTest(); 18 | const string Screen::*pdata = Screen::data(); 19 | Screen myScreen(10, 20, 'a'), *pScreen = &myScreen; 20 | cout << myScreen.*pdata << endl; 21 | cout << pScreen->*pdata << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap09/Exer09_20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cin; 5 | using std::cout; 6 | using std::endl; 7 | using std::deque; 8 | using std::list; 9 | int main() 10 | { 11 | list nums = {1, 2, 4, 5, 9, 20, 21, 35, 485, 491, 20, 28, 79}; 12 | deque even, odd; 13 | for(auto iter = nums.begin(); iter != nums.end(); ++iter) 14 | { 15 | if(*iter % 2 == 0) 16 | even.emplace_back(*iter); 17 | else 18 | odd.emplace_back(*iter); 19 | } 20 | for(auto e : even) 21 | cout << e << " "; 22 | cout << endl; 23 | for(auto o : odd) 24 | cout << o << " "; 25 | cout << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /chap10/Exer10_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::list; 9 | using std::string; 10 | int main() 11 | { 12 | list lst; 13 | string str, str_val; 14 | // read a sequence of strings 15 | while(cin >> str) 16 | { 17 | lst.push_back(str); 18 | } 19 | // read a value to be counted 20 | cin.clear(); // if not, we can't input anything, see page 313, 8.1.2 Condition States 21 | cin >> str_val; 22 | // count how often val appears 23 | auto cnt = count(lst.begin(), lst.end(), str_val); 24 | cout << cnt << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /chap13/Exer13_28_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer13_28_BinStrTree_point.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | TreeNode t1("t1"); 8 | TreeNode t2 = t1; 9 | t1.read(cout) << endl; 10 | t2.write("t2"); 11 | t2.read(cout) << endl; 12 | { 13 | TreeNode t3(t2); 14 | t3.read(cout) << endl; 15 | t3.write("t3"); 16 | t3.read(cout) << endl;; 17 | } 18 | TreeNode t4("t4"); 19 | TreeNode t5("t5"); 20 | t4.read(cout) << endl;; 21 | t5.read(cout) << endl; 22 | t4 = t4; 23 | BinStrTree r1; 24 | BinStrTree r2(r1); 25 | BinStrTree r3; 26 | r3 = r1; 27 | r3 = r3; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /chap13/Exer13_28_value.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer13_28_BinStrTree_value.h" 3 | using std::cout; 4 | using std::endl; 5 | int main() 6 | { 7 | TreeNode t1("t1"); 8 | TreeNode t2 = t1; 9 | t1.read(cout) << endl; 10 | t2.write("t2"); 11 | t2.read(cout) << endl; 12 | { 13 | TreeNode t3(t2); 14 | t3.read(cout) << endl; 15 | t3.write("t3"); 16 | t3.read(cout) << endl;; 17 | } 18 | TreeNode t4("t4"); 19 | TreeNode t5("t5"); 20 | t4.read(cout) << endl;; 21 | t5.read(cout) << endl; 22 | t4 = t4; 23 | BinStrTree r1; 24 | BinStrTree r2(r1); 25 | BinStrTree r3; 26 | r3 = r1; 27 | r3 = r3; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /chap15/Exer15_39/Exer15_39_AndQuery.h: -------------------------------------------------------------------------------- 1 | #ifndef AND_QUERY_H 2 | #define AND_QUERY_H 3 | #include 4 | #include "Exer15_39_TextQuery.h" 5 | #include "Exer15_39_BinaryQuery.h" 6 | #include "Exer15_39_Query.h" 7 | class AndQuery : public BinaryQuery { 8 | friend Query operator&(const Query&, const Query&); 9 | AndQuery(const Query &left, const Query &right) : 10 | BinaryQuery(left, right, "&") {} 11 | // concrete class: AndQuery inherits rep and defines the remaining pure virtual 12 | QueryResult eval(const TextQuery&) const; 13 | }; 14 | inline Query operator&(const Query &lhs, const Query &rhs) 15 | { 16 | return std::shared_ptr(new AndQuery(lhs, rhs)); 17 | } 18 | #endif 19 | -------------------------------------------------------------------------------- /chap15/Exer15_41_42/README.md: -------------------------------------------------------------------------------- 1 | * Exercise 15.41 2 | 3 | This exercise uses pointer-like assignment and copy, so we need substitute ```new```, ```delete``` and reference count for ```shared_ptr```. We have to define all of the copy-control members. 4 | 5 | * Exercise 15.42 6 | * (a) 7 | - [x] To be solved. 8 | * (b) 9 | We can use a ```vector``` to store query history. But this might not be the best way. There could be some enhancement. 10 | * (c) 11 | We can add another two parameters to the function ```print```: 12 | ```cpp 13 | std::ostream& print(std::ostream &os, const QueryResult &qr, TextQuery::line_no min, TextQuery::line_no max); 14 | ``` 15 | Thus the user is allowed to limit the range of output. -------------------------------------------------------------------------------- /chap02/Exer02_39.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for verification. It cannot be compiled successfully by every compiler. 2 | // The problem lies in line 5. 3 | #include 4 | struct Foo {} //no semicolon 5 | int main() 6 | { 7 | return 0; 8 | } 9 | // ******compile info under g++****** 10 | // Exer2_39.cpp:3:13: error: expected ';' after struct definition 11 | // struct Foo {} //no semicolon 12 | // ^ 13 | 14 | // ******compile info under cl****** 15 | // Exer2_39.cpp 16 | // Exer2_39.cpp(4) : error C2628: “Foo”后面接“int”是非法的(是否忘记了“;”?) 17 | // Exer2_39.cpp(5) : error C3874: “main”的返回类型应为“int”而非“Foo” 18 | // Exer2_39.cpp(6) : error C2440: “return”: 无法从“int”转换为“Foo” 19 | // 无构造函数可以接受源类型,或构造函数重载决策不明确 20 | -------------------------------------------------------------------------------- /chap03/Page128_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::endl; 7 | int main() 8 | { 9 | constexpr size_t rowCnt = 3, colCnt = 4; 10 | size_t cnt = 0; 11 | int ia[rowCnt][colCnt]; 12 | // Warning: in range for, be sure to use reference! 13 | // Or the program won't compile 14 | for(auto &row : ia) 15 | for(auto &col : row) 16 | { 17 | col = cnt; 18 | ++cnt; 19 | } 20 | for(auto &row : ia) 21 | { 22 | for(auto &col : row) 23 | { 24 | cout << col << "\t"; 25 | } 26 | cout << endl; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /chap06/Exer06_23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | void print(const int (&arr)[10]) 5 | { 6 | for(auto elem : arr) 7 | cout << elem << " "; 8 | cout << endl; 9 | } 10 | void print(const int (&arr)[2]) 11 | { 12 | for(auto elem : arr) 13 | cout << elem << " "; 14 | cout << endl; 15 | } 16 | void print(const int i) 17 | { 18 | cout << i << endl; 19 | } 20 | int main() 21 | { 22 | int i = 0, j[2] = {0, 1}; 23 | int k[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 24 | cout << "int i:" << endl; 25 | print(i); 26 | cout << "array j[2]:" << endl; 27 | print(j); 28 | cout << "array k[10]:" << endl; 29 | print(k); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /chap11/Exer11_26.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. It can't compile. 2 | // The problem lies in line 19. 3 | #include 4 | #include 5 | #include 6 | using std::cout; 7 | using std::endl; 8 | using std::map; 9 | using std::string; 10 | int main() 11 | { 12 | string s = "Anna"; 13 | const string &sref = s; 14 | map m; 15 | m[sref] = 3; // here, we can use both const and non const 16 | map::iterator map_iter = m.begin(); 17 | cout << map_iter->first << " " << map_iter->second << endl; 18 | // because key is const string, we cannot initialize a nonconst reference with it 19 | string& pref_nc = &(map_iter->first); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap18/Exer18_03_2.cpp: -------------------------------------------------------------------------------- 1 | // Note: see the link below for further discussion: 2 | // https://stackoverflow.com/questions/34258836/ways-to-avoid-memory-leak-when-exception-thrown 3 | #include 4 | #include 5 | #include 6 | using std::ifstream; 7 | using std::vector; 8 | using std::unique_ptr; 9 | void exercise(int *b, int *e) 10 | { 11 | vector v(b, e); 12 | unique_ptr p(new int[v.size()]); 13 | ifstream in("ints"); 14 | // exception occurs here 15 | } 16 | int main() 17 | { 18 | int a[] = { 0, 1, 2, 3, 4, 5, 6, 7 }; 19 | int *b = a, *e = a + 7; 20 | exercise(b, e); 21 | return 0; 22 | } 23 | // Way 2: use smart pointer and then we can solve the problem. 24 | -------------------------------------------------------------------------------- /chap17/Page746_random_engine.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::size_t; 5 | using std::cout; 6 | using std::endl; 7 | using std::default_random_engine; 8 | using std::uniform_int_distribution; 9 | int main() 10 | { 11 | // uniformly distributed from 0 to 0 inclusive 12 | uniform_int_distribution u(0, 9); 13 | default_random_engine e; // generates random unsigned integers 14 | for (size_t i = 0; i < 10; ++i) 15 | // u uses e as a source of numbers 16 | // e() "calls" the object to produce the next random number 17 | cout << u(e) << " "; 18 | cout << endl; 19 | cout << "min: " << e.min() << " max: " << e.max() << endl; 20 | } 21 | -------------------------------------------------------------------------------- /chap03/Page095_subscript.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | const string hexdigits = "0123456789ABCDEF"; 10 | cout << "Enter a series of numbers between 0 and 15" 11 | << " separated by spaces. Hit ENTER when finished: " 12 | << endl; 13 | string result; 14 | string::size_type n; 15 | while(cin >> n) 16 | if(n < hexdigits.size()) 17 | result += hexdigits[n]; 18 | cout << "Your hex number is: " << result << endl; 19 | return 0; 20 | } 21 | // In fact, just hit ENTER won't stop input. 22 | // You have to hit ENTER and press CTRL+Z to complete input. 23 | -------------------------------------------------------------------------------- /chap07/Exer07_21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_data.h" 3 | using std::cout; 4 | using std::cin; 5 | using std::cerr; 6 | using std::endl; 7 | int main() 8 | { 9 | Sales_data total(cin); 10 | if(!total.isbn().empty()) 11 | { 12 | Sales_data trans; 13 | while(read(cin, trans)) 14 | { 15 | if(total.isbn() == trans.isbn()) 16 | total.combine(trans); 17 | else 18 | { 19 | print(cout, total) << endl; 20 | total = trans; 21 | } 22 | } 23 | print(cout, total) << endl; 24 | } 25 | else 26 | { 27 | cerr << "No data?!" << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /chap10/Exer10_27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using std::cout; 7 | using std::endl; 8 | using std::list; 9 | using std::vector; 10 | int main() 11 | { 12 | vector ivec = { 1, 2, 4, 4, 90, 2, 32, 1, 20, -20, 42}; 13 | list ilst; 14 | // if not sort at first, the unique_copy doesn't work, because it only works on adjacent elements 15 | sort(ivec.begin(), ivec.end()); 16 | unique_copy(ivec.cbegin(), ivec.cend(), back_inserter(ilst)); 17 | for(auto i : ivec) 18 | cout << i << " "; 19 | cout << endl; 20 | for(auto i : ilst) 21 | cout << i << " "; 22 | cout << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chap07/Exer07_57.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Account { 3 | public: 4 | Account() = default; 5 | Account(const std::string& s, double d) : owner(s), amount(d) {} 6 | void calculate() { amount += amount * interestRate; } 7 | static double rate() { return interestRate; } 8 | static void rate(double); 9 | private: 10 | static constexpr int period = 30; // period is a const expression 11 | double daily_tbl[period] = {0}; 12 | std::string owner; 13 | double amount; 14 | static double interestRate; 15 | }; 16 | void Account::rate(double d) 17 | { 18 | interestRate = d; 19 | } 20 | constexpr int Account::period; 21 | double Account::interestRate = 0.0; 22 | int main() 23 | { 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap14/Exer14_32.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer14_30_StrBlob.h" 3 | class HasStrBlobPtr { 4 | public: 5 | HasStrBlobPtr() : p(nullptr) {} 6 | HasStrBlobPtr(StrBlobPtr &a) : p(&a) {} // can only point to nonconst StrBlobPtr 7 | StrBlobPtr& operator*() const { 8 | return *p; 9 | } 10 | StrBlobPtr* operator->() const { 11 | return &this->operator*(); // return a pointer 12 | } 13 | private: 14 | StrBlobPtr *p; 15 | }; 16 | int main() 17 | { 18 | StrBlob v = {"Like", "a", "thunderbolt", "he", "falls"}; 19 | StrBlobPtr p1(v); 20 | HasStrBlobPtr hp(p1); 21 | std::cout << hp->deref() << std::endl; 22 | std::cout << hp->operator[](2) << std::endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chap16/Page669_template_declaration.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for illustration. Template declarations and definitions should be put into header files. 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | // we can omit a type name, if the name is not used as function parameter or return type 6 | template void compare(const M&); 7 | // template name can be different across definitions and declarations 8 | // but the number and kind(i.e., type and nontype) of type name should keep the same 9 | template void compare(const U &u) 10 | { 11 | T t; 12 | cout << t << " " << u << endl; 13 | } 14 | int main() 15 | { 16 | compare(2.0); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap09/Exer09_24.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for verification. It can be compiled successfully but doesn't work. 2 | // The problems lie in line 11 to 14. 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | int main() 9 | { 10 | vector iv; 11 | cout << iv.at(0) << endl; 12 | cout << iv[0] << endl; 13 | cout << iv.front() << endl; 14 | cout << iv.back() << endl; 15 | return 0; 16 | } 17 | // Note: if we don't judge if a vector is empty, the access operation above just 18 | // make the program crash. But at operation will throw an exception, reminding 19 | // us of the stupid error. 20 | // WARNING: JUDGE if a container is empty before accessing its elements. 21 | -------------------------------------------------------------------------------- /chap13/Page545_rvaluereference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void foo(int&&) {std::cout << "void foo(int&&)" << std::endl;} 3 | void foo(int&) {std::cout << "void foo(int&)" << std::endl;} 4 | int main() 5 | { 6 | int i = 0; 7 | foo(2); 8 | foo(i); 9 | foo(std::move(i)); 10 | return 0; 11 | } 12 | // Note: lvalue and rvalue reference can be used to overload function. Because 13 | // they DO match different arguments. Compare this with Exer13_53_overload.cpp, 14 | // we can find that overloaded functions must be able to be passed different 15 | // arguments. If two functions with the same name can be passed completely 16 | // identical arguments, they will cause ambiguous call. Thus match rules on page 17 | // 245 won't work. 18 | -------------------------------------------------------------------------------- /chap15/Page617_Disc_quote.h: -------------------------------------------------------------------------------- 1 | #ifndef DISC_QUOTE_H 2 | #define DISC_QUOTE_H 3 | #include 4 | #include 5 | #include 6 | #include "Exer15_15_Quote.h" 7 | class Disc_quote : public Quote { 8 | public: 9 | Disc_quote() = default; 10 | Disc_quote(const std::string &book, double price, std::size_t qty, double disc): 11 | Quote(book, price), quantity(qty), discount(disc) {} 12 | double net_price(std::size_t) const = 0; 13 | std::pair discount_policy() const 14 | { return {quantity, discount}; } 15 | protected: 16 | std::size_t quantity = 0; // purchase size for the discount to apply 17 | double discount = 0.0; // fractional discount to apply 18 | }; 19 | #endif 20 | -------------------------------------------------------------------------------- /chap18/Page794_using_directive.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for illustration. It cannot compile. 2 | namespace blip { 3 | int i = 16, j = 15, k = 23; 4 | } 5 | int j = 0; // ok: j inside blip is hidden inside a namespace 6 | int main() 7 | { 8 | // using directive; the name in blip are "added" to the global scope 9 | using namespace blip; // clash between ::j and blip::j 10 | // detected only if j is used 11 | ++i; // sets blip i = 17 12 | ++j; // error ambiguous: global j or blip::j? 13 | ++::j; // ok: sets global j to 1 14 | ++blip::j; // ok: sets blip::j to 16 15 | int k = 97; // local k hides blip::k 16 | ++k; // sets local k to 98 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap04/Exer04_37.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::string; 6 | int main() 7 | { 8 | char c = 'z'; 9 | string s = "this is a string."; 10 | 11 | int i; 12 | double d; 13 | const string *ps = &s; 14 | char *pc = &c; 15 | void *pv; 16 | // As long as we cast away const, we can assign a pointer to void*. 17 | pv = const_cast(ps); 18 | // Implicit conversion gets the same result as below. 19 | i = static_cast(*pc); 20 | // Assigning a pointer to void* directly is okay. 21 | pv = &d; 22 | // Getting original pointer type can be performed by static_cast. 23 | pc = static_cast(pv); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap03/Exer03_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | string s1, s2; 10 | cin >> s1 >> s2; 11 | if(s1 == s2) 12 | cout << "Two strings are equal." << endl; 13 | else if(s1 > s2) 14 | cout << "The first string is larger." << endl; 15 | else 16 | cout << "The second string is larger." << endl; 17 | 18 | if(s1.size() == s2.size()) 19 | cout << "Two strings have the same length." << endl; 20 | else if(s1.size() > s2.size()) 21 | cout << "The first string is longer." << endl; 22 | else 23 | cout << "The second string is longer." << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap07/Exer07_31.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | class Y; // class declaration, incomplete class type 3 | class X { 4 | public: 5 | const X& print() const; 6 | private: 7 | Y* y = nullptr; // define a pointer to an incomplete class type 8 | }; 9 | class Y { 10 | public: 11 | const Y& print() const; 12 | private: 13 | X x; // already defined class, complete, use its object. 14 | }; 15 | const X& X::print() const 16 | { 17 | std::cout << "This is class X" << std::endl; 18 | return *this; 19 | } 20 | const Y& Y::print() const 21 | { 22 | std::cout << "This is class Y" << std::endl; 23 | return *this; 24 | } 25 | int main() 26 | { 27 | X xP; 28 | xP.print(); 29 | Y yP; 30 | yP.print(); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /chap09/Exer09_51.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Exer09_51.h" 5 | using std::cin; 6 | using std::cerr; 7 | using std::endl; 8 | using std::string; 9 | using std::invalid_argument; 10 | int main() 11 | { 12 | string d; 13 | while(getline(cin, d)) 14 | { 15 | try 16 | { 17 | Date date(d); 18 | date.print_date(); 19 | } 20 | catch(invalid_argument inv_arg) 21 | { 22 | cerr << inv_arg.what() 23 | << "\nTry again? Enter y or n" << endl; 24 | char c; 25 | cin >> c; 26 | if(!cin || c == 'n') 27 | break; 28 | } 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /chap12/Exer12_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::vector; 7 | vector* vec_create(const vector &v = {}) 8 | { 9 | vector *vp = new vector(v); 10 | return vp; 11 | } 12 | vector* vec_read(vector *vp) 13 | { 14 | int i; 15 | while(cin >> i) 16 | { 17 | vp->push_back(i); 18 | } 19 | return vp; 20 | } 21 | void vec_print(vector *vp) 22 | { 23 | for(const auto &i : *vp) 24 | { 25 | cout << i << " "; 26 | } 27 | cout << endl; 28 | delete vp; 29 | } 30 | int main() 31 | { 32 | auto vp = vec_create(); 33 | vec_read(vp); 34 | vec_print(vp); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /chap03/Exer03_17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | using std::vector; 9 | int main() 10 | { 11 | string word; 12 | vector text; 13 | while(cin >> word) 14 | text.push_back(word); 15 | for(string &s : text) 16 | { 17 | if(!s.empty()) 18 | { 19 | for(char &c : s) 20 | { 21 | if(isalpha(c) && !isupper(c)) 22 | c = toupper(c); // !attention: toupper() doesn't change it's parameter 23 | } 24 | } 25 | } 26 | for(auto s : text) 27 | cout << s << " "; 28 | cout << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /chap04/Exer04_24.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for verification. It CANNOT be compiled successfully by every compiler. 2 | // The problem lies in line 15. 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | int main() 9 | { 10 | int grade = 80; 11 | // if ? : is left associative, result is something like below. 12 | // It's a wrong expression for type of two expressions of the first 13 | // conditional operator is neither the same type nor able to be converted to 14 | // the same type. 15 | string finalgrade = ((grade > 90) ? "high pass" : (grade < 60)) 16 | ? "fail" : "pass"; 17 | cout << finalgrade << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /chap05/Exer05_18.cpp: -------------------------------------------------------------------------------- 1 | // Warning: This is for verification. It CANNOT be compiled successfully by every compiler. 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | int main() 7 | { 8 | // (a), there should be a { after do and a } before while 9 | do 10 | int v1, v2; 11 | cout << "Please enter two numbers to sum:"; 12 | if(cin >> v1 >> v2) 13 | cout << "Sum is: " << v1 + v2 << endl; 14 | while(cin); 15 | // (b), define ival outside the condition. 16 | do { 17 | int ival = 0; 18 | }while(int ival = 0); 19 | // (c), define ival outside the body of do while loop. 20 | do{ 21 | int ival = 2; 22 | }while(ival); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chap14/Exer14_28.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for test. It cannot compile. 2 | // The problems lie in line 18 and 19. 3 | #include 4 | #include 5 | #include "Exer14_28_StrBlob.h" 6 | using std::size_t; 7 | int main() 8 | { 9 | StrBlob v = {"Like", "a", "thunderbolt", "he", "falls"}; 10 | StrBlobPtr p1(v), p2 = p1, p3 = p1; 11 | ConstStrBlobPtr pc(v); 12 | for(size_t i = 0; i != v.size(); ++i) 13 | std::cout << (p1 + i).deref() << " "; 14 | std::cout << std::endl; 15 | p2 += 2; 16 | std::cout << p2.deref() << std::endl; 17 | p3[4] = "falls."; 18 | pc[4] = "fall"; // wrong, const pointer 19 | p2 = 5 + p2; // throw exception 20 | std::cout << p2.deref() << std::endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap16/Exer16_20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "Exer16_20_print_iter.h" 7 | using std::cout; 8 | using std::endl; 9 | using std::string; 10 | using std::vector; 11 | using std::deque; 12 | using std::list; 13 | int main() 14 | { 15 | vector vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 16 | deque dq = { "like", "a", "thunderbolt", "he", "falls" }; 17 | list lst = { "like", "a", "thunderbolt", "he", "falls" }; 18 | string s("Templates"); 19 | print(vec); 20 | cout << endl; 21 | print(dq); 22 | cout << endl; 23 | print(lst); 24 | cout << endl; 25 | print(s); 26 | cout << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /chap16/Exer16_58.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Sales_data.h" 4 | #include "Exer16_58_Vec.h" 5 | #include "Exer16_58_StrVec.h" 6 | using std::cout; 7 | using std::endl; 8 | using std::string; 9 | int main() 10 | { 11 | Vec v; 12 | // here, emplace_back uses move constructor of Sales_data to construct object 13 | v.emplace_back("978-7-121-20038", 20, 122); 14 | v.emplace_back("978-7-121-20038", 50, 120); 15 | for(const auto &item : v) 16 | cout << item << endl; 17 | StrVec str; 18 | string s = "world"; 19 | str.emplace_back("hi"); 20 | str.emplace_back(s); 21 | for(const auto &item : str) 22 | cout << item << " "; 23 | cout << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap15/Exer15_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer15_01_Quote.h" 3 | #include "Exer15_05_Bulk_quote.h" 4 | #include "Exer15_07_Limit_quote.h" 5 | using std::cout; 6 | int main() 7 | { 8 | Quote item1("978-7-121-20038-0", 128.0); 9 | Bulk_quote item2("978-7-121-20038-0", 128.0, 6, 0.05); 10 | Limit_quote item3("978-7-121-20038-0", 128.0, 2, 0.05); 11 | print_total(cout, item1, 10); // calls Quote::net_price 12 | print_total(cout, item2, 10); // calls Bulk_quote::net_price 13 | print_total(cout, item3, 1); // calls Limit_quote::net_price 14 | print_total(cout, item1, 5); // calls Quote::net_price 15 | print_total(cout, item2, 5); // calls Bulk_quote::net_price 16 | print_total(cout, item3, 5); // calls Limit_quote::net_price 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /tools/InsertAnswer.h: -------------------------------------------------------------------------------- 1 | #ifndef TOOL_INSERT_ANSWER_H 2 | #define TOOL_INSERT_ANSWER_H 3 | #ifndef UNICODE 4 | #define UNICODE 5 | #endif 6 | #ifndef _UNICODE 7 | #define _UNICODE 8 | #endif 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #pragma comment(lib, "User32.lib") 15 | #ifndef UNICODE 16 | typedef std::string String; 17 | #else 18 | typedef std::wstring String; 19 | #endif 20 | typedef std::map> ExerSet; 21 | void DisplayErrorBox(LPTSTR lpszFunction); 22 | bool iscpp(const String&); 23 | int get_source(const TCHAR *path, ExerSet&); 24 | std::vector analyse_filename(const String&); 25 | String format(const std::vector &); 26 | #endif 27 | -------------------------------------------------------------------------------- /chap18/Exer18_22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | class A { 5 | public: 6 | A() { cout << "A()" << endl; } 7 | }; 8 | class B : public A { 9 | public: 10 | B() : A() { cout << "B()" << endl; } 11 | }; 12 | class C : public B { 13 | public: 14 | C() : B() { cout << "C()" << endl; } 15 | }; 16 | class X { 17 | public: 18 | X() { cout << "X()" << endl; } 19 | }; 20 | class Y { 21 | public: 22 | Y() { cout << "Y()" << endl; } 23 | }; 24 | class Z : public X, public Y { 25 | public: 26 | Z() : X(), Y() { cout << "Z()" << endl; } 27 | }; 28 | class MI : public C, public Z {}; 29 | int main() 30 | { 31 | MI mi; 32 | return 0; 33 | } 34 | // Chain of call: 35 | // MI() -> C() -> B() -> A() -> Z() -> X() -> Y() 36 | -------------------------------------------------------------------------------- /chap02/Page046_underline.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | // a variable named as an underline is legal 5 | // !!!variable name only containing one or more underlines is legal , but it's never recommended!!! 6 | // variable name containing two consistent underlines belongs to standard library, if you define 7 | // a variable with the same name as that in standard library, unexpected things would happen. 8 | int _ = 1; 9 | int __ = 2; 10 | int ___ = 3; 11 | std::cout << _ << __<< ___ << std::endl; 12 | // data type won't influence result because of a implicit type conversion. So use it freely as at home. 13 | int compareIntAndFloat = (10 == 10.0); 14 | std::cout << compareIntAndFloat << std::endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /chap09/Exer09_47.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::string; 6 | int main() 7 | { 8 | string str = "ab2c3d7R4E6"; 9 | string numbers = "0123456789"; 10 | string::size_type pos = 0; 11 | while((pos = str.find_first_of(numbers, pos)) != string::npos) 12 | { 13 | cout << "found digit at index: " << pos 14 | << " element is " << str[pos] << endl; 15 | ++pos; 16 | } 17 | cout << endl; 18 | pos = 0; 19 | while((pos = str.find_first_not_of(numbers, pos)) != string::npos) 20 | { 21 | cout << "found alphabet at index: " << pos 22 | << " element is " << str[pos] << endl; 23 | ++pos; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /chap15/Page603_no_conversion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer15_01_Quote.h" 3 | #include "Exer15_05_Bulk_quote.h" 4 | #include "Exer15_07_Limit_quote.h" 5 | using std::cout; 6 | int main() 7 | { 8 | Quote item1("978-7-121-20038-0", 128.0); 9 | Bulk_quote item2("978-7-121-20038-0", 128.0, 6, 0.05); 10 | Limit_quote item3("978-7-121-20038-0", 128.0, 2, 0.05); 11 | print_total(cout, item1, 10); // calls Quote::net_price 12 | print_total(cout, item2, 10); // calls Bulk_quote::net_price 13 | print_total(cout, item3, 1); // calls Limit_quote::net_price 14 | print_total(cout, item1, 5); // calls Quote::net_price 15 | print_total(cout, item2, 5); // calls Bulk_quote::net_price 16 | print_total(cout, item3, 5); // calls Limit_quote::net_price 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap17/Exer17_27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::string; 9 | using std::regex; 10 | using std::smatch; 11 | int main() 12 | { 13 | string zipcode = "(\\d{5})(-)?(\\d{4})?"; 14 | regex r(zipcode); 15 | smatch m; 16 | string s; 17 | string fmt = "$1-$3 "; 18 | while (cin >> s) { 19 | if (regex_match(s, m, r)) { 20 | if (m[3].matched) 21 | cout << "valid: " << m.format(fmt) << endl; 22 | else 23 | cout << "valid: " << m.str() << endl; 24 | } 25 | else 26 | cout << "invalid zip code: " << s << endl; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /chap17/Exer17_35.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::scientific; 6 | using std::fixed; 7 | using std::hexfloat; 8 | using std::defaultfloat; 9 | using std::uppercase; 10 | using std::nouppercase; 11 | using std::sqrt; 12 | int main() 13 | { 14 | // specifying the notation of floating numbers 15 | cout << "default format: " << 100 * sqrt(2.0) << '\n' 16 | << "scientific: " << scientific << 100 * sqrt(2.0) << '\n' 17 | << "fixed decimal: " << fixed << 100 * sqrt(2.0) << '\n' 18 | << "hexadecimal: " << uppercase << hexfloat << 100 * sqrt(2.0) << nouppercase << '\n' 19 | << "use defaults: " << defaultfloat << 100 * sqrt(2.0) 20 | << "\n\n"; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap07/Exer07_23_Screen.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER07_23_H 2 | #define EXER07_23_H 3 | #include 4 | class Screen{ 5 | public: 6 | typedef std::string::size_type pos; 7 | Screen() = default; 8 | Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) {} 9 | char get() const { return contents[cursor]; } 10 | inline char get(pos ht, pos wd) const; 11 | Screen &move(pos r, pos c); 12 | private: 13 | pos cursor = 0; 14 | pos height = 0, width = 0; 15 | std::string contents; 16 | }; 17 | char Screen::get(pos r, pos c) const 18 | { 19 | pos row = r * width; 20 | return contents[row + c]; 21 | } 22 | Screen& Screen::move(pos r, pos c) 23 | { 24 | pos row = r * width; 25 | cursor = row + c; 26 | return *this; 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /chap10/Page404_istreamiterator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::vector; 8 | using std::istream_iterator; 9 | int main() 10 | { 11 | istream_iterator in_iter(cin); 12 | istream_iterator eof; 13 | vector vec1; 14 | while(in_iter != eof) 15 | vec1.push_back(*in_iter++); 16 | cin.clear(); // clear flags for latter input 17 | istream_iterator in_iter2(cin); // now in_iter == eof, so we have to define another 18 | vector vec2(in_iter2, eof); 19 | for(auto i : vec1) 20 | cout << i << " "; 21 | cout << endl; 22 | for(auto i : vec2) 23 | cout << i << " "; 24 | cout << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /chap02/Exer02_42_2.cpp: -------------------------------------------------------------------------------- 1 | // Rewrite of Exer1.21 2 | #include 3 | #include "Sales_data.h" 4 | int main() 5 | { 6 | Sales_data book1, book2; 7 | double price1 = 0, price2 = 0; 8 | std::cin >> book1.bookNo >> book1.units_sold >> price1; 9 | std::cin >> book1.bookNo >> book1.units_sold >> price2; 10 | book1.revenue = price1 * book1.units_sold; 11 | book2.revenue = price2 * book2.units_sold; 12 | if(book1.bookNo == book2.bookNo) 13 | std::cout << book1.bookNo << " " 14 | < 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | struct NoDefault { 8 | NoDefault(int i) : nodef_mem(i) { cout << "NoDefault(int i)" << endl; } 9 | private: 10 | int nodef_mem; 11 | }; 12 | struct C { 13 | // way 1: delegate another nondefault constructor 14 | C() : C(0) { cout << "C()" << endl; }; 15 | C(int ival) : c_member(ival) { cout << "C(int ival)" << endl; } 16 | private: 17 | NoDefault c_member; 18 | }; 19 | int main() 20 | { 21 | C cobj; 22 | // vector vec(10); // error, no default constructor 23 | vector vec_C(10); // okay 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /chap11/Exer11_20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using std::cout; 7 | using std::endl; 8 | using std::ifstream; 9 | using std::size_t; 10 | using std::string; 11 | using std::map; 12 | int main(int argc, char *argv[]) 13 | { 14 | if(argc != 2) 15 | return -1; 16 | ifstream is(argv[1]); 17 | map word_count; 18 | string word; 19 | while(is >> word) 20 | { 21 | auto ret = word_count.insert({word, 1}); 22 | if(!ret.second) 23 | ++ret.first->second; 24 | } 25 | for(const auto &w : word_count) 26 | cout << w.first << " occurs " << w.second 27 | << ((w.second > 1) ? " times" : " time") << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /chap15/Exer15_15_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Exer15_15_Quote.h" 3 | #include "Exer15_15_Bulk_quote.h" 4 | #include "Exer15_16_Limit_quote.h" 5 | using std::cout; 6 | int main() 7 | { 8 | Quote item1("978-7-121-20038-0", 128.0); 9 | Bulk_quote item2("978-7-121-20038-0", 128.0, 6, 0.05); 10 | Limit_quote item3("978-7-121-20038-0", 128.0, 2, 0.15); 11 | print_total(cout, item1, 10); // calls Quote::net_price 12 | print_total(cout, item2, 10); // calls Bulk_quote::net_price 13 | print_total(cout, item3, 1); // calls Limit_quote::net_price 14 | print_total(cout, item1, 5); // calls Quote::net_price 15 | print_total(cout, item2, 5); // calls Bulk_quote::net_price 16 | print_total(cout, item3, 5); // calls Limit_quote::net_price 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /chap16/Page684_trailing_return.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for illustration. Template declarations and definitions should be put into header files. 2 | // a trailing return lets us declare the return type after the parameter list is seen 3 | #include 4 | #include 5 | #include 6 | using std::cout; 7 | using std::endl; 8 | using std::vector; 9 | using std::string; 10 | template 11 | auto fcn(It beg) -> decltype(*beg) 12 | { 13 | // process the range 14 | return *beg; // return a reference to an element from the range 15 | } 16 | int main() 17 | { 18 | vector vec = { "old", "man", "and", "the", "sea" }; 19 | for(auto b = vec.begin(); b != vec.end(); ++b) 20 | cout << fcn(b) << " "; 21 | cout << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap17/Page728_grading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::bitset; 3 | int main() 4 | { 5 | bool status; 6 | // version using bitwise operators 7 | unsigned long quizA = 0; // this value is used as a collection of bits 8 | quizA |= 1UL << 27; // indicate student number 27 passed 9 | status = quizA & (1UL << 27); // check how student number 27 did 10 | quizA &= ~(1UL << 27); // student number 27 failed 11 | // equivalent actions using the bitset library 12 | bitset<30> quizB; // allocate one bit per student; all bits initialized to 0 13 | quizB.set(27); // indicate student number 27 passed 14 | status = quizB[27]; // check how student number 27 did 15 | quizB.reset(27); // student number 27 failed 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /chap05/Exer05_19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | int main() 8 | { 9 | do{ 10 | string s1, s2; 11 | cin >> s1 >> s2; 12 | // To be honest, judge if s1 and s2 are empty is surplus here. Just I 13 | // remind myself that checking boundary conditions is essential. 14 | if(!s1.empty() && !s2.empty()){ 15 | if(s1 < s2) 16 | cout << "The first string is less than the second." << endl; 17 | else if(s1 > s2) 18 | cout << "The second string is less than the first." << endl; 19 | else 20 | cout << "Two strings are equal." << endl; 21 | } 22 | }while(cin); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chap15/Exer15_29.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "Exer15_28_29_Bulk_quote.h" 5 | using std::cout; 6 | using std::endl; 7 | using std::vector; 8 | using std::shared_ptr; 9 | using std::make_shared; 10 | int main() 11 | { 12 | vector> basket; 13 | basket.push_back(make_shared("978-7-121-20038-0", 128.0, 6, 0.05)); 14 | basket.push_back(make_shared("7-115-13771-4", 59.0, 10, 0.15)); 15 | basket.push_back(make_shared("978-7-111-40701-0", 128.0, 10, 0.2)); 16 | basket.push_back(make_shared("978-0-87779-855-2", 56.0, 5, 0.1)); 17 | double sum = 0.0; 18 | for(const auto &c : basket) 19 | sum += c->net_price(10); 20 | cout << sum << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /chap01/Exer01_24.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_item.h" 3 | int main() 4 | { 5 | Sales_item bookSum; 6 | //defects: if two books with identical isbn aren't input continuously, 7 | //this program cannot find it. 8 | if(std::cin >> bookSum) 9 | { 10 | Sales_item book; 11 | while(std::cin >> book) 12 | { 13 | if(bookSum.isbn() == book.isbn()) 14 | bookSum = bookSum + book; 15 | else 16 | { 17 | std::cout << bookSum << std::endl; 18 | bookSum = book; 19 | } 20 | } 21 | std::cout << bookSum << std::endl; 22 | } 23 | else 24 | { 25 | std::cerr << "No data?!" << std::endl; 26 | return -1; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /chap01/Exer01_25.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_item.h" 3 | int main() 4 | { 5 | Sales_item bookSum; 6 | //detects: if two books with identical isbn aren't input continuously, 7 | //this program cannot find it. 8 | if(std::cin >> bookSum) 9 | { 10 | Sales_item book; 11 | while(std::cin >> book) 12 | { 13 | if(bookSum.isbn() == book.isbn()) 14 | bookSum = bookSum + book; 15 | else 16 | { 17 | std::cout << bookSum << std::endl; 18 | bookSum = book; 19 | } 20 | } 21 | std::cout << bookSum << std::endl; 22 | } 23 | else 24 | { 25 | std::cerr << "No data?!" << std::endl; 26 | return -1; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /chap17/Exer17_24.cpp: -------------------------------------------------------------------------------- 1 | // Note: this program uses ./data/person_info. 2 | #include 3 | #include 4 | #include 5 | #include 6 | using std::cout; 7 | using std::endl; 8 | using std::ifstream; 9 | using std::string; 10 | using std::regex; 11 | using std::smatch; 12 | int main(int argc, char* argv[]) 13 | { 14 | if (argc != 2) 15 | return -1; 16 | ifstream is(argv[1]); 17 | string phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})"; 18 | regex r(phone); // a regex to find our pattern 19 | smatch m; 20 | string s; 21 | string fmt = "$2.$5.$7"; // reformat numbers to ddd.ddd.dddd 22 | // read each record from the input file 23 | while (getline(is, s)) 24 | cout << regex_replace(s, r, fmt) << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /chap01/Exer01_18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | // currVal is the number we're counting; we'll read new values into val 5 | int currVal = 0, val = 0; 6 | // read first number and ensure that we have data to process 7 | if(std::cin >> currVal) 8 | { 9 | int cnt = 1; 10 | while(std::cin >> val) 11 | { 12 | if(val == currVal) 13 | ++cnt; 14 | else 15 | { 16 | std::cout << currVal << " occurs " 17 | << cnt << " times" << std::endl; 18 | currVal = val; 19 | cnt = 1; 20 | } 21 | } 22 | std::cout << currVal << " occurs " 23 | << cnt << " times" << std::endl; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /chap18/Exer18_17_1.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. It cannot compile. 2 | // The problem lie in line 9. 3 | namespace Exercise { 4 | int ivar = 0; 5 | double dvar = 0; 6 | const int limit = 1000; 7 | } 8 | int ivar = 0; 9 | using Exercise::ivar; // conficts with ::ivar 10 | using Exercise::dvar; 11 | using Exercise::limit; 12 | int main() 13 | { 14 | double dvar = 3.1416; // hides Exercise::dvar 15 | int iobj = limit + 1; // uses Exercise::limit 16 | ++ivar; // conflicts above, if comment using Exercise::ivar, this uses ::ivar 17 | ++::ivar; // uses ::ivar 18 | return 0; 19 | } 20 | // ******compile info of g++****** 21 | // Exer18_17_1.cpp:9:17: error: 'ivar' is already declared in this scope 22 | // using Exercise::ivar; // conficts with ::ivar 23 | // ^ 24 | -------------------------------------------------------------------------------- /chap05/Exer05_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | int main() 6 | { 7 | unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0; 8 | char ch; 9 | while(cin >> ch) 10 | { 11 | if(ch == 'a') 12 | ++aCnt; 13 | if(ch == 'e') 14 | ++eCnt; 15 | if(ch == 'i') 16 | ++iCnt; 17 | if(ch == 'o') 18 | ++oCnt; 19 | if(ch == 'u') 20 | ++uCnt; 21 | } 22 | cout << "Number of vowel a: \t" << aCnt << endl; 23 | cout << "Number of vowel e: \t" << eCnt << endl; 24 | cout << "Number of vowel i: \t" << iCnt << endl; 25 | cout << "Number of vowel o: \t" << oCnt << endl; 26 | cout << "Number of vowel u: \t" << uCnt << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /chap09/Exer09_14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::list; 8 | using std::string; 9 | using std::vector; 10 | int main() 11 | { 12 | list detectives = {"Holmes", "Watson", "Hudson", "Poirot", "Hastings", "Marple"}; 13 | vector names; // assign doesn't require identical container type 14 | list names2; // swap requires identical container type 15 | names.assign(detectives.begin(), detectives.end()); 16 | swap(names2, detectives); 17 | for(const auto &s : names) 18 | cout << s << " "; 19 | cout << endl; 20 | for(auto itr = names2.begin(); itr != names2.end(); ++ itr) 21 | cout << *itr << " "; 22 | cout << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chap11/Exer11_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::string; 9 | using std::vector; 10 | using std::pair; 11 | int main() 12 | { 13 | string str; 14 | int i; 15 | vector> data; 16 | while(cin >> str >> i) 17 | { 18 | data.push_back(make_pair(str, i)); // way # 1 19 | } 20 | for(auto &p : data) 21 | cout << p.first << " " << p.second << endl; 22 | return 0; 23 | } 24 | // Other ways to create the pairs: 25 | // # way 2 26 | // pair new_pair(str, i); 27 | // data.push_back(new_pair); 28 | // 29 | // # way 3 30 | // data.push_back({str, i}); 31 | // # way 1 is the easiest, because we needn't define a temporary local object 32 | -------------------------------------------------------------------------------- /chap12/Exer12_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "StrBlob.h" 4 | using std::cout; 5 | using std::endl; 6 | using std::out_of_range; 7 | int main() 8 | { 9 | StrBlob b1; 10 | cout << b1.use_count() << endl; 11 | { 12 | StrBlob b2 = {"a", "an", "the"}; 13 | b1 = b2; 14 | b2.push_back("about"); 15 | cout << b2.use_count() << endl; 16 | } 17 | cout << b1.use_count() << endl; 18 | const StrBlob b3({"and", "or", "not"}); 19 | cout << b3.front() << " "<< b3.back() << endl; 20 | StrBlob b4 = b3; 21 | cout << b4.use_count() << endl; 22 | try 23 | { 24 | StrBlob b5; 25 | cout << b5.front(); 26 | } 27 | catch(out_of_range err) 28 | { 29 | cout << err.what() << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /chap03/Exer03_21_2.cpp: -------------------------------------------------------------------------------- 1 | // Page 105, exercise 3.17 2 | #include 3 | #include 4 | #include 5 | using std::cout; 6 | using std::cin; 7 | using std::endl; 8 | using std::string; 9 | using std::vector; 10 | int main() 11 | { 12 | string word; 13 | vector text; 14 | while(cin >> word) 15 | text.push_back(word); 16 | for(auto item = text.begin(); item != text.end() && !item->empty(); ++item) 17 | { 18 | for(auto c = item->begin(); c != item->end(); ++c) 19 | { 20 | if(isalpha(*c) && !isupper(*c)) 21 | { 22 | *c = toupper(*c); // !attention: toupper() doesn't change it's parameter 23 | } 24 | } 25 | } 26 | for(auto s : text) 27 | cout << s << " "; 28 | cout << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /chap14/Page588_function_matching.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. It cannot compile. 2 | #include 3 | class SmallInt { 4 | friend SmallInt operator+(const SmallInt&, const SmallInt&); 5 | public: 6 | SmallInt(int = 0); // conversion from int 7 | operator int() const { return val; } // conversion to int 8 | private: 9 | std::size_t val; 10 | }; 11 | SmallInt operator+(const SmallInt &lhs, const SmallInt &rhs) 12 | { 13 | return lhs.val + rhs.val; 14 | } 15 | int main() 16 | { 17 | SmallInt s1, s2; 18 | // uses overloaded operator+ 19 | SmallInt s3 = s1 + s2; 20 | // error: ambiguous call; whether to convert s3 to int and then call built-in addition operator or 21 | // convert 0 SmallInt and then to call overloaded addition operator? 22 | int i = s3 + 0; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /chap03/Exer03_45.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | int ia[3][4] = {{0, 1, 2, 3}, 7 | {4, 5, 6, 7}, 8 | {8, 9, 10, 11}}; 9 | // range for 10 | for(auto &p : ia) 11 | { 12 | for(auto &q : p) 13 | cout << q << ' '; 14 | cout << endl; 15 | } 16 | cout << endl; 17 | // for with pointer 18 | for(int i = 0; i < 3; ++i) 19 | { 20 | for(int j = 0; j < 4; ++j) 21 | cout << ia[i][j] << ' '; 22 | cout << endl; 23 | } 24 | cout << endl; 25 | // for with pointer 26 | for(auto p = ia; p != ia + 3; ++p) 27 | { 28 | for(auto q = *p; q != *p + 4; ++q) 29 | cout << *q << ' '; 30 | cout << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /chap12/Exer12_17.cpp: -------------------------------------------------------------------------------- 1 | // Warning: this is for verification. It cannot compile. 2 | // The problems lie between line 11 and 18. 3 | #include 4 | #include 5 | using std::string; 6 | using std::unique_ptr; 7 | int main() 8 | { 9 | int ix = 1024, *pi = &ix, *pi2 = new int(2048); 10 | typedef unique_ptr IntP; 11 | IntP p0(ix); // error: no constructor takes an int or compatible type 12 | IntP p1(pi); // error: must be a dynamically allocated pointer 13 | { 14 | // error: the object pi2 points to will be destroyed at the end of this scope, pi2 will be dangling 15 | IntP p2(pi2); 16 | IntP p5(p2.get()); // error: no copy for unique_ptr 17 | } 18 | IntP p3(&ix); // error, ix is not a dynamically allocated object 19 | IntP p4(new int(2048)); // legal 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /chap03/Exer03_43.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | int main() 5 | { 6 | int ia[3][4] = {{0, 1, 2, 3}, 7 | {4, 5, 6, 7}, 8 | {8, 9, 10, 11}}; 9 | // range for 10 | for(int (&p)[4] : ia) 11 | { 12 | for(int &q : p) 13 | cout << q << ' '; 14 | cout << endl; 15 | } 16 | cout << endl; 17 | // for with pointer 18 | for(int i = 0; i < 3; ++i) 19 | { 20 | for(int j = 0; j < 4; ++j) 21 | cout << ia[i][j] << ' '; 22 | cout << endl; 23 | } 24 | cout << endl; 25 | // for with pointer 26 | for(int (*p)[4] = ia; p != ia + 3; ++p) 27 | { 28 | for(int *q = *p; q != *p + 4; ++q) 29 | cout << *q << ' '; 30 | cout << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /chap17/Exer17_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | using std::regex; 9 | using std::smatch; 10 | int main() 11 | { 12 | regex r("[^c]ei"); // construct an object to find pattern, only match 3-character words 13 | smatch results; // define an object to hold the results of a search 14 | // define a string that has text that does and doesn't match pattern 15 | string test_str; 16 | cout << "input a word: " << endl; 17 | while (cin >> test_str) { 18 | // use r to find a match to pattern in test_str 19 | if(regex_search(test_str, results, r)) // if there is a match 20 | cout << "wrong spelling! \"ei\" is reversed." << endl; // print the matching word 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /chap07/Exer07_24_Screen.h: -------------------------------------------------------------------------------- 1 | #ifndef EXER07_23_H 2 | #define EXER07_23_H 3 | #include 4 | class Screen{ 5 | public: 6 | typedef std::string::size_type pos; 7 | Screen() = default; 8 | Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) {} 9 | Screen(pos ht, pos wd) : height(ht), width(wd), contents(ht * wd, ' ') {} 10 | char get() const { return contents[cursor]; } 11 | inline char get(pos ht, pos wd) const; 12 | Screen &move(pos r, pos c); 13 | private: 14 | pos cursor = 0; 15 | pos height = 0, width = 0; 16 | std::string contents; 17 | }; 18 | char Screen::get(pos r, pos c) const 19 | { 20 | pos row = r * width; 21 | return contents[row + c]; 22 | } 23 | Screen& Screen::move(pos r, pos c) 24 | { 25 | pos row = r * width; 26 | cursor = row + c; 27 | return *this; 28 | } 29 | #endif 30 | -------------------------------------------------------------------------------- /chap08/Exer08_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using std::istream; 6 | using std::ostream; 7 | using std::ifstream; 8 | using std::cout; 9 | using std::endl; 10 | using std::string; 11 | using std::vector; 12 | istream& restoreData(ifstream& is, const string &ifile, vector &v) 13 | { 14 | string str; 15 | is.open(ifile); 16 | if(is) 17 | { 18 | while(getline(is, str)) 19 | { 20 | v.push_back(str); 21 | } 22 | } 23 | return is; 24 | } 25 | int main(int argc, char* argv[]) 26 | { 27 | ifstream in; 28 | vector vec; 29 | if(argc == 2) 30 | { 31 | restoreData(in, argv[1], vec); 32 | } 33 | if(!vec.empty()) 34 | for(const auto &s : vec) 35 | cout << s << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /chap17/data/story: -------------------------------------------------------------------------------- 1 | A Fox once saw a Crow fly off with a piece of cheese in its beak and settle on a 2 | branch of a tree. 3 | 4 | "That's for me, as I am a Fox," said Master Reynard, and he walked up to the foot 5 | of the tree. 6 | 7 | "Good day, Mistress Crow," he cried. "How well you are looking today: how glossy 8 | your feathers; how bright your eye. I feel sure your voice must surpass that of 9 | other birds, just as your figure does; let me hear but one song from you that I 10 | may greet you as the Queen of Birds." 11 | 12 | The Crow lifted up her head and began to caw her best, but the moment she opened 13 | her mouth the piece of cheese fell to the ground, only to be snapped up by Master 14 | Fox. 15 | 16 | "That will do," said he. "That was all I wanted. In exchange for your cheese I 17 | will give you a piece of advice for the future: "Do not trust flatterers." -------------------------------------------------------------------------------- /chap06/Exer06_47.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cerr; 6 | using std::endl; 7 | using std::vector; 8 | void printVec(const vector &vec, int beg, int size) 9 | { 10 | if(vec.empty()) 11 | return; 12 | if(size - beg > 0) 13 | { 14 | #ifndef NDEBUG 15 | cerr << "\n" << __FUNCTION__ << ":\n" 16 | << "The size of the vector:" << size - beg << endl; 17 | #endif 18 | cout << vec[beg] << " "; 19 | printVec(vec, ++beg, size); 20 | } 21 | } 22 | int main() 23 | { 24 | vector vi = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 25 | printVec(vi, 0, vi.size()); 26 | cout << endl; 27 | return 0; 28 | } 29 | // Note: for visual studio compiler, substitute __FUNCTION__ for __func__, 30 | // for g++, both __FUNCTION__ and __func__ are okay. 31 | -------------------------------------------------------------------------------- /chap13/Exer13_34_36_37.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "Exer13_34_36_37_Folder.h" 4 | using std::cout; 5 | using std::endl; 6 | int main() 7 | { 8 | Folder f1("Inbox"); 9 | Folder f2("Send Box"); 10 | Folder f3("Important Mails"); 11 | Message m1("To Mr. John"); 12 | Message m2 = m1; 13 | Message m3("To Mrs. King"); 14 | m1.save(f1); 15 | m2.save(f1); 16 | m3.save(f2); 17 | cout << "f1" << endl; 18 | f1.show(); 19 | cout << "f2" << endl; 20 | f2.show(); 21 | f3 = f2; 22 | cout << "f3" << endl; 23 | f3.show(); 24 | cout << "f3" << endl; 25 | m3.save(f3); 26 | m1.save(f3); 27 | m2.save(f3); 28 | f3.show(); 29 | cout << "f3" << endl; 30 | m1.remove(f3); 31 | f3.show(); 32 | cout << "f2" << endl; 33 | f2 = f3; 34 | f2.show(); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /chap03/Exer03_36_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cin; 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | int main() 8 | { 9 | vector v1 = {2, 3, 5, 7, 8}; 10 | vector v2 = {2, 3, 5, 7, 8}; 11 | auto pv1 = v1.begin(); 12 | auto pv2 = v2.begin(); 13 | if(v1.end() - v1.begin() == v2.end() - v2.begin()) 14 | { 15 | while(pv1 != v1.end() && pv2 != v2.end()) 16 | { 17 | if(*pv1 != *pv2) 18 | { 19 | cout << "a1 and a2 are NOT equal!" << endl; 20 | return -1; 21 | } 22 | ++pv1; 23 | ++pv2; 24 | } 25 | cout << "a1 and a2 are equal!" << endl; 26 | } 27 | else 28 | { 29 | cout << "a1 and a2 are NOT equal!" << endl; 30 | return -1; 31 | } 32 | return 0; 33 | } 34 | --------------------------------------------------------------------------------