├── 10 ├── 10 │ └── README.md ├── 11 │ └── README.md ├── 14 │ └── README.md ├── 15 │ └── README.md ├── 17 │ └── README.md ├── 21 │ └── README.md ├── 22 │ └── README.md ├── 23 │ └── README.md ├── 26 │ └── README.md ├── 27 │ └── README.md ├── 29 │ └── README.md ├── 30 │ ├── README.md │ └── 30.cc ├── 31 │ ├── README.md │ └── 31.cc ├── 32 │ └── README.md ├── 34 │ ├── README.md │ └── 34.cc ├── 35 │ ├── README.md │ └── 35.cc ├── 36 │ ├── README.md │ └── 36.cc ├── 37 │ ├── README.md │ └── 37.cc ├── 39 │ └── README.md ├── 40 │ └── README.md ├── 42 │ └── README.md ├── 02 │ ├── README.md │ └── 2.cc ├── 06 │ └── README.md ├── 03 │ └── README.md ├── 09 │ └── README.md ├── 04 │ └── README.md ├── 01 │ ├── 1.cc │ └── README.md └── 08 │ └── README.md ├── 11 ├── 10 │ └── README.md ├── 11 │ └── README.md ├── 12 │ ├── README.md │ └── 12.cc ├── 14 │ └── README.md ├── 15 │ └── README.md ├── 16 │ └── README.md ├── 18 │ └── README.md ├── 19 │ └── README.md ├── 22 │ └── README.md ├── 23 │ └── README.md ├── 24 │ └── README.md ├── 25 │ └── README.md ├── 28 │ └── README.md ├── 29 │ └── README.md ├── 31 │ └── README.md ├── 32 │ └── README.md ├── 33 │ ├── in.txt │ ├── README.md │ └── rules.txt ├── 34 │ └── README.md ├── 38 │ ├── in.txt │ ├── README.md │ └── rules.txt ├── 03 │ └── README.md ├── 04 │ └── README.md ├── 09 │ └── README.md ├── 01 │ └── README.md ├── 07 │ └── README.md ├── 08 │ └── README.md └── 06 │ └── README.md ├── 12 ├── 13 │ └── README.md ├── 16 │ └── 16.cc ├── 18 │ └── README.md ├── 19 │ └── README.md ├── 20 │ └── README.md ├── 21 │ └── README.md ├── 22 │ └── README.md ├── 23 │ └── README.md ├── 24 │ └── 24.cc ├── 25 │ └── README.md ├── 26 │ └── README.md ├── 27 │ └── README.md ├── 28 │ └── README.md ├── 30 │ ├── test.txt │ └── README.md ├── 32 │ └── README.md ├── 33 │ └── README.md ├── 07 │ └── README.md ├── 02 │ └── README.md ├── 04 │ └── README.md ├── 01 │ └── README.md └── 06 │ └── README.md ├── 13 ├── 10 │ └── README.md ├── 11 │ └── README.md ├── 13 │ └── X.h ├── 16 │ └── README.md ├── 17 │ ├── README.md │ └── 13_14.cc ├── 21 │ └── README.md ├── 23 │ └── README.md ├── 26 │ └── README.md ├── 27 │ └── README.md ├── 29 │ └── README.md ├── 30 │ ├── 30.cc │ └── README.md ├── 31 │ ├── README.md │ └── 31.cc ├── 32 │ └── README.md ├── 33 │ └── README.md ├── 34 │ └── README.md ├── 35 │ └── README.md ├── 36 │ └── README.md ├── 37 │ └── README.md ├── 39 │ └── README.md ├── 40 │ └── README.md ├── 42 │ └── README.md ├── 44 │ └── README.md ├── 46 │ └── README.md ├── 47 │ └── README.md ├── 48 │ ├── README.md │ └── 48.cc ├── 49 │ └── README.md ├── 50 │ ├── README.md │ └── 50.cc ├── 51 │ └── README.md ├── 54 │ ├── 54.cc │ └── README.md ├── 55 │ └── README.md ├── 57 │ └── README.md ├── 58 │ ├── 58.cc │ └── README.md ├── 02 │ └── README.md ├── 03 │ └── README.md ├── 01 │ └── README.md └── 07 │ └── README.md ├── 14 ├── 12 │ └── README.md ├── 14 │ └── README.md ├── 15 │ └── README.md ├── 16 │ └── README.md ├── 17 │ └── README.md ├── 18 │ └── README.md ├── 19 │ └── README.md ├── 20 │ └── README.md ├── 22 │ └── README.md ├── 23 │ └── README.md ├── 25 │ └── README.md ├── 26 │ └── README.md ├── 27 │ └── README.md ├── 28 │ └── README.md ├── 29 │ └── README.md ├── 30 │ └── README.md ├── 31 │ └── README.md ├── 32 │ └── README.md ├── 33 │ └── README.md ├── 36 │ └── README.md ├── 37 │ └── README.md ├── 38 │ ├── input.txt │ └── README.md ├── 39 │ ├── README.md │ └── input.txt ├── 43 │ └── README.md ├── 44 │ └── README.md ├── 45 │ └── README.md ├── 46 │ └── README.md ├── 47 │ └── README.md ├── 49 │ └── README.md ├── 51 │ └── README.md ├── 06 │ └── README.md ├── 09 │ └── README.md ├── 08 │ └── README.md └── 07 │ └── README.md ├── 15 ├── 11 │ └── README.md ├── 12 │ └── README.md ├── 15 │ └── README.md ├── 16 │ └── README.md ├── 17 │ └── 17.cc ├── 19 │ └── README.md ├── 26 │ └── 26.cc ├── 27 │ └── README.md ├── 28 │ └── README.md ├── 30 │ └── 30.cc ├── 32 │ └── README.md ├── 33 │ └── README.md ├── 35 │ └── README.md ├── 36 │ ├── 36.cc │ └── README.md ├── 37 │ └── README.md ├── 39 │ └── 39.cc ├── 41 │ └── README.md ├── 42 │ └── 39.cc ├── 05 │ └── README.md ├── 03 │ └── README.md ├── 01 │ └── README.md ├── 06 │ ├── README.md │ └── 6.cc └── 02 │ └── README.md ├── 16 ├── 01 │ └── README.md ├── 02 │ └── README.md ├── 07 │ └── README.md ├── 04 │ └── README.md ├── 05 │ └── README.md └── 03 │ └── 3.cc ├── 01 ├── 10 │ ├── 10.cc │ └── README.md ├── 11 │ ├── 11.cc │ └── README.md ├── 12 │ └── README.md ├── 13 │ ├── 1.10.cc │ ├── README.md │ ├── 1.09.cc │ └── 1.11.cc ├── 15 │ ├── README.md │ └── 15.cc ├── 16 │ ├── README.md │ └── 16.cc ├── 18 │ └── README.md ├── 19 │ ├── README.md │ └── 19.cc ├── 20 │ ├── 20.cc │ └── README.md ├── 21 │ ├── README.md │ └── 21.cc ├── 22 │ ├── README.md │ └── 22.cc ├── 23 │ └── README.md ├── 24 │ ├── out.txt │ ├── in.txt │ └── README.md ├── 25 │ └── README.md ├── 01 │ ├── main.cc │ └── README.md ├── 02 │ ├── main.cc │ └── README.md ├── 07 │ ├── 7.cc │ └── README.md ├── 03 │ ├── 3.cc │ └── README.md ├── 09 │ ├── README.md │ └── 9.cc ├── 05 │ ├── README.md │ └── 5.cc ├── 04 │ ├── README.md │ └── 4.cc └── 08 │ └── README.md ├── cover.jpg ├── 02 ├── 10 │ └── README.md ├── 11 │ └── README.md ├── 12 │ └── README.md ├── 13 │ └── README.md ├── 17 │ └── README.md ├── 18 │ └── README.md ├── 19 │ └── README.md ├── 20 │ └── README.md ├── 22 │ └── README.md ├── 23 │ └── README.md ├── 24 │ └── README.md ├── 32 │ └── README.md ├── 34 │ └── README.md ├── 36 │ └── README.md ├── 39 │ ├── 39.cc │ └── README.md ├── 40 │ └── README.md ├── 41 │ ├── 1.24.out.txt │ ├── 1.24.in.txt │ └── README.md ├── 42 │ ├── 1.24.out.txt │ ├── 1.24.in.txt │ ├── Sales_data.h │ └── README.md ├── 09 │ └── test.cc ├── 08 │ ├── 8.cc │ └── README.md ├── 04 │ ├── README.md │ └── 4.cc ├── 02 │ └── README.md ├── 07 │ └── README.md └── 06 │ └── README.md ├── 03 ├── 10 │ ├── README.md │ └── 10.cc ├── 11 │ └── README.md ├── 14 │ ├── README.md │ └── 14.cc ├── 15 │ ├── README.md │ └── 15.cc ├── 16 │ └── README.md ├── 17 │ └── README.md ├── 20 │ ├── README.md │ └── 20.cc ├── 21 │ └── README.md ├── 23 │ ├── README.md │ └── 23.cc ├── 24 │ ├── README.md │ └── 24.cc ├── 25 │ └── README.md ├── 26 │ └── README.md ├── 29 │ └── README.md ├── 31 │ ├── README.md │ └── 31.cc ├── 32 │ ├── README.md │ ├── 32b.cc │ └── 32.cc ├── 35 │ ├── README.md │ └── 35.cc ├── 36 │ └── README.md ├── 37 │ └── README.md ├── 39 │ ├── README.md │ └── 39.cc ├── 40 │ ├── README.md │ └── 40.cc ├── 41 │ ├── README.md │ └── 41.cc ├── 42 │ ├── README.md │ └── 42.cc ├── 43 │ ├── 43.cc │ ├── 43b.cc │ └── 43c.cc ├── 44 │ ├── README.md │ ├── 44.cc │ └── 44c.cc ├── 45 │ ├── README.md │ ├── 45.cc │ ├── 45b.cc │ └── 45c.cc ├── 01 │ ├── 1.24.out.txt │ ├── 1.24.in.txt │ ├── 1.10.cc │ ├── Sales_data.h │ ├── 1.09.cc │ ├── README.md │ └── 1.11.cc ├── 06 │ ├── README.md │ └── 6.cc ├── 02 │ ├── README.md │ ├── 2b.cc │ └── 2.cc ├── 07 │ ├── 7.cc │ └── README.md ├── 05 │ ├── 5.cc │ ├── README.md │ └── 5b.cc ├── 09 │ └── README.md ├── 08 │ ├── 8b.cc │ ├── 8.cc │ └── README.md ├── 04 │ └── README.md └── 03 │ └── README.md ├── 07 ├── 11 │ ├── README.md │ └── 11.cc ├── 12 │ └── README.md ├── 13 │ └── README.md ├── 14 │ └── README.md ├── 15 │ └── README.md ├── 17 │ └── README.md ├── 18 │ └── README.md ├── 20 │ └── README.md ├── 21 │ └── README.md ├── 22 │ └── README.md ├── 23 │ └── README.md ├── 25 │ └── README.md ├── 26 │ └── README.md ├── 27 │ ├── 27.cc │ └── README.md ├── 28 │ └── README.md ├── 29 │ ├── 29.cc │ └── README.md ├── 31 │ ├── 31.h │ └── README.md ├── 32 │ └── README.md ├── 34 │ └── README.md ├── 38 │ └── README.md ├── 41 │ └── 41.cc ├── 43 │ └── README.md ├── 44 │ └── README.md ├── 45 │ └── README.md ├── 50 │ └── README.md ├── 54 │ └── README.md ├── 55 │ └── README.md ├── 57 │ └── README.md ├── 04 │ ├── person.cc │ └── README.md ├── 03 │ └── README.md ├── 07 │ └── README.md ├── 09 │ └── README.md ├── 01 │ └── README.md ├── 05 │ ├── person.cc │ └── README.md └── 08 │ └── README.md ├── 04 ├── 10 │ └── README.md ├── 11 │ └── README.md ├── 12 │ └── README.md ├── 13 │ └── README.md ├── 15 │ └── README.md ├── 17 │ └── README.md ├── 18 │ └── README.md ├── 21 │ ├── README.md │ └── 21.cc ├── 22 │ └── 22.cc ├── 25 │ └── README.md ├── 26 │ └── README.md ├── 27 │ └── README.md ├── 28 │ └── README.md ├── 30 │ └── README.md ├── 32 │ └── README.md ├── 33 │ └── README.md ├── 36 │ └── README.md ├── 01 │ └── README.md ├── 06 │ └── README.md ├── 04 │ ├── 4.cc │ └── README.md ├── 05 │ └── README.md ├── 02 │ └── README.md └── 07 │ └── README.md ├── 06 ├── 10 │ ├── README.md │ └── 10.cc ├── 11 │ ├── README.md │ └── 11.cc ├── 12 │ ├── README.md │ └── 12.cc ├── 13 │ └── README.md ├── 21 │ └── README.md ├── 22 │ └── README.md ├── 25 │ ├── README.md │ └── 25.cc ├── 26 │ ├── README.md │ └── 26.cc ├── 27 │ └── README.md ├── 28 │ └── README.md ├── 29 │ └── README.md ├── 30 │ └── README.md ├── 33 │ └── README.md ├── 34 │ └── README.md ├── 36 │ └── README.md ├── 38 │ └── README.md ├── 40 │ └── README.md ├── 42 │ └── README.md ├── 44 │ └── README.md ├── 46 │ └── README.md ├── 47 │ └── README.md ├── 49 │ └── README.md ├── 51 │ └── README.md ├── 52 │ └── README.md ├── 54 │ └── README.md ├── 56 │ └── README.md ├── 03 │ ├── README.md │ └── fact.cc ├── 08 │ ├── Chapter6.h │ └── README.md ├── 09 │ ├── Chapter6.h │ ├── factMain.cc │ ├── fact.cc │ └── README.md ├── 04 │ ├── README.md │ └── 4.cc ├── 05 │ └── README.md ├── 07 │ └── README.md └── 01 │ └── README.md ├── 08 ├── 10 │ └── README.md ├── 11 │ └── README.md ├── 12 │ └── README.md ├── 13 │ └── README.md ├── 14 │ └── README.md ├── 02 │ ├── README.md │ └── 2.cc ├── 09 │ ├── README.md │ └── 9.cc ├── 06 │ └── README.md ├── 07 │ └── README.md ├── 08 │ └── README.md └── 03 │ └── README.md ├── 09 ├── 10 │ └── README.md ├── 13 │ └── README.md ├── 14 │ ├── README.md │ └── 14.cc ├── 15 │ ├── README.md │ └── 15.cc ├── 16 │ └── README.md ├── 18 │ ├── README.md │ └── 18.cc ├── 19 │ ├── README.md │ └── 19.cc ├── 20 │ └── README.md ├── 21 │ └── README.md ├── 23 │ └── README.md ├── 24 │ ├── 24.cc │ └── README.md ├── 27 │ └── README.md ├── 29 │ └── README.md ├── 30 │ └── README.md ├── 32 │ └── README.md ├── 33 │ ├── 33.cc │ └── README.md ├── 34 │ └── 34.cc ├── 35 │ └── README.md ├── 36 │ └── README.md ├── 37 │ └── README.md ├── 38 │ ├── README.md │ └── 38.cc ├── 39 │ └── README.md ├── 40 │ └── README.md ├── 41 │ ├── README.md │ └── 41.cc ├── 42 │ └── README.md ├── 43 │ └── README.md ├── 44 │ └── README.md ├── 45 │ ├── README.md │ └── 45.cc ├── 46 │ └── README.md ├── 47 │ └── README.md ├── 48 │ └── README.md ├── 49 │ └── README.md ├── 50 │ └── README.md ├── 07 │ └── README.md ├── 02 │ └── README.md ├── 08 │ └── README.md ├── 03 │ └── README.md └── 09 │ └── README.md └── 05 ├── 11 └── README.md ├── 12 └── README.md ├── 17 └── README.md ├── 19 └── README.md ├── 20 └── 20.cc ├── 21 └── README.md ├── 22 └── README.md ├── 23 ├── README.md └── 23.cc ├── 24 └── 24.cc ├── 25 └── README.md ├── 09 └── README.md ├── 02 └── README.md ├── 08 └── README.md └── 01 └── README.md /01/01/main.cc: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | return 0; 4 | } 5 | -------------------------------------------------------------------------------- /01/02/main.cc: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | return -1; 4 | } 5 | -------------------------------------------------------------------------------- /cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/williamgherman/cpp-solutions/HEAD/cover.jpg -------------------------------------------------------------------------------- /01/07/7.cc: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | /* This is /* incorrect */ // comment 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /13/58/58.cc: -------------------------------------------------------------------------------- 1 | #include "foo.h" 2 | 3 | int main() 4 | { 5 | Foo f1; 6 | f1.sorted(); 7 | } 8 | -------------------------------------------------------------------------------- /01/24/out.txt: -------------------------------------------------------------------------------- 1 | abc 13 1079.87 83.0669 2 | def 26 591.74 22.7592 3 | ghi 2 47.9 23.95 4 | jkl 11 2418.95 219.905 5 | -------------------------------------------------------------------------------- /02/09/test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | std::cin >> int in_val; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /02/41/1.24.out.txt: -------------------------------------------------------------------------------- 1 | abc 13 1079.87 83.0669 2 | def 26 591.74 22.7592 3 | ghi 2 47.9 23.95 4 | jkl 11 2418.95 219.905 5 | -------------------------------------------------------------------------------- /02/42/1.24.out.txt: -------------------------------------------------------------------------------- 1 | abc 13 1079.87 83.0669 2 | def 26 591.74 22.7592 3 | ghi 2 47.9 23.95 4 | jkl 11 2418.95 219.905 5 | -------------------------------------------------------------------------------- /03/01/1.24.out.txt: -------------------------------------------------------------------------------- 1 | abc 13 1079.87 83.0669 2 | def 26 591.74 22.7592 3 | ghi 2 47.9 23.95 4 | jkl 11 2418.95 219.905 5 | -------------------------------------------------------------------------------- /07/04/person.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Person { 4 | std::string name; 5 | std::string address; 6 | }; 7 | -------------------------------------------------------------------------------- /04/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.01 2 | 3 | What is the value returned by `5 + 10 * 20/2`? 4 | 5 | ### Solution 6 | 7 | `105`. 8 | -------------------------------------------------------------------------------- /01/03/3.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | std::cout << "Hello, World" << std::endl; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /06/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.03 2 | 3 | Write and tet your own version of `fact`. 4 | 5 | ### Solution 6 | 7 | See `fact.cc`. 8 | -------------------------------------------------------------------------------- /11/33/in.txt: -------------------------------------------------------------------------------- 1 | where r u 2 | y do u ask? im busy now 3 | cant talk? 4 | y dont u send me a pic 5 | k thx l8r 6 | im just gonna test this 7 | -------------------------------------------------------------------------------- /11/38/in.txt: -------------------------------------------------------------------------------- 1 | where r u 2 | y do u ask? im busy now 3 | cant talk? 4 | y dont u send me a pic 5 | k thx l8r 6 | im just gonna test this 7 | -------------------------------------------------------------------------------- /11/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.03 2 | 3 | Write your own version of the word-counting program. 4 | 5 | ### Solution 6 | 7 | See `3.cc`. 8 | -------------------------------------------------------------------------------- /01/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.07 2 | 3 | Compile a program that has incorrectly nested comments. 4 | 5 | ### Solution 6 | 7 | See `7.cc`. 8 | -------------------------------------------------------------------------------- /03/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.15 2 | 3 | Repeat the previous program but read `string`s this time. 4 | 5 | ### Solution 6 | 7 | See `15.cc`. 8 | -------------------------------------------------------------------------------- /06/56/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.56 2 | 3 | Call each element in the `vector` and print the result. 4 | 5 | ### Solution 6 | 7 | See `56.cc`. 8 | -------------------------------------------------------------------------------- /07/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.15 2 | 3 | Add appropriate constructors to your `Person` class. 4 | 5 | ### Solution 6 | 7 | See `person.cc`. 8 | -------------------------------------------------------------------------------- /07/22/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.22 2 | 3 | Update your `Person` class to hide its implementation. 4 | 5 | ### Solution 6 | 7 | See `person.h`. 8 | -------------------------------------------------------------------------------- /12/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.07 2 | 3 | Redo the previous exercise, this time using `shared_ptr`. 4 | 5 | ### Solution 6 | 7 | See `7.cc`. 8 | -------------------------------------------------------------------------------- /12/26/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.26 2 | 3 | Rewrite the program on page 481 using an `allocator`. 4 | 5 | ### Solution 6 | 7 | See `26.cc`. 8 | -------------------------------------------------------------------------------- /15/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.05 2 | 3 | Define your own version of the `Bulk_quote` class. 4 | 5 | ### Solution 6 | 7 | See program files. 8 | -------------------------------------------------------------------------------- /01/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.03 2 | 3 | Write a program to print `Hello, World` on the standard output. 4 | 5 | ### Solution 6 | 7 | See `3.cc`. 8 | -------------------------------------------------------------------------------- /07/26/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.26 2 | 3 | Define `Sales_data::avg_price` as an `inline` function. 4 | 5 | ### Solution 6 | 7 | See `Sales_data.h`. 8 | -------------------------------------------------------------------------------- /08/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.02 2 | 3 | Test your function by calling it, passing `cin` as an argument. 4 | 5 | ### Solution 6 | 7 | See `2.cc`. 8 | -------------------------------------------------------------------------------- /09/44/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.44 2 | 3 | Rewrite the previous function using an index and `replace`. 4 | 5 | ### Solution 6 | 7 | See `44.cc`. 8 | -------------------------------------------------------------------------------- /10/34/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.34 2 | 3 | Use `reverse_iterator`s to print a `vector` in reverse order. 4 | 5 | ### Solution 6 | 7 | See `34.cc`. 8 | -------------------------------------------------------------------------------- /13/27/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.27 2 | 3 | Define your own reference-counted version of `HasPtr`. 4 | 5 | ### Solution 6 | 7 | See `HasPtr.h`. 8 | -------------------------------------------------------------------------------- /13/34/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.34 2 | 3 | Write the `Message` class as described in this section. 4 | 5 | ### Solution 6 | 7 | See `message.h`. 8 | -------------------------------------------------------------------------------- /14/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.06 2 | 3 | Define an output operator for your `Sales_data` class. 4 | 5 | ### Solution 6 | 7 | See `Sales_data.h`. 8 | -------------------------------------------------------------------------------- /14/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.09 2 | 3 | Define an input operator for your `Sales_data` class. 4 | 5 | ### Solution 6 | 7 | See `Sales_data.h`. 8 | -------------------------------------------------------------------------------- /15/17/17.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "quote.h" 3 | 4 | int main() 5 | { 6 | Disc_quote *dq = new Disc_quote(); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /03/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.06 2 | 3 | Use a range `for` to change all the characters in a `string` to `X`. 4 | 5 | ### Solution 6 | 7 | See `6.cc`. 8 | -------------------------------------------------------------------------------- /03/41/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.41 2 | 3 | Write a program to initialize a `vector` from an array of `int`s. 4 | 5 | ### Solution 6 | 7 | See `41.cc`. 8 | -------------------------------------------------------------------------------- /04/28/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.28 2 | 3 | Write a program to print the size of each of the built-in types. 4 | 5 | ### Solution 6 | 7 | See `28.cc`. 8 | -------------------------------------------------------------------------------- /06/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.11 2 | 3 | Write and test your own version of `reset` that takes a reference. 4 | 5 | ### Solution 6 | 7 | See `11.cc`. 8 | -------------------------------------------------------------------------------- /09/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.15 2 | 3 | Write a program to determine whether two `vector`s are equal. 4 | 5 | ### Solution 6 | 7 | See `15.cc`. 8 | -------------------------------------------------------------------------------- /09/41/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.41 2 | 3 | Write a program that initializes a `string` from a `vector`. 4 | 5 | ### Solution 6 | 7 | See `41.cc`. 8 | -------------------------------------------------------------------------------- /10/35/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.35 2 | 3 | Now print the elements in reverse order using ordinary iterators. 4 | 5 | ### Solution 6 | 7 | See `35.cc`. 8 | -------------------------------------------------------------------------------- /15/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.15 2 | 3 | Define your own versions of `Disc_quote` and `Bulk_quote`. 4 | 5 | ### Solution 6 | 7 | See program files. 8 | -------------------------------------------------------------------------------- /02/39/39.cc: -------------------------------------------------------------------------------- 1 | struct Foo { /* empty */ } // Note: no semicolon 2 | // error: expected ‘;’ after struct definition 3 | int main() 4 | { 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /03/35/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.35 2 | 3 | Using pointers, write a program to set the elements in an array to zero. 4 | 5 | ### Solution 6 | 7 | See `35.cc`. 8 | -------------------------------------------------------------------------------- /03/42/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.42 2 | 3 | Write a program to copy a `vector` of `int`s into an array of `int`s. 4 | 5 | ### Solution 6 | 7 | See `42.cc`. 8 | -------------------------------------------------------------------------------- /10/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.02 2 | 3 | Repeat the previous program, but read values into a `list` of `string`s. 4 | 5 | ### Solution 6 | 7 | See `2.cc`. 8 | -------------------------------------------------------------------------------- /10/36/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.36 2 | 3 | Use `find` to find the last element in a `list` of `int`s with value 0. 4 | 5 | ### Solution 6 | 7 | See `36.cc`. 8 | -------------------------------------------------------------------------------- /13/55/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.55 2 | 3 | Add an rvalue reference version of `push_back` to your `StrBlob`. 4 | 5 | ### Solution 6 | 7 | See program files. 8 | -------------------------------------------------------------------------------- /14/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.55 2 | 3 | Add an rvalue reference version of `push_back` to your `StrBlob`. 4 | 5 | ### Solution 6 | 7 | See program files. 8 | -------------------------------------------------------------------------------- /15/27/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.27 2 | 3 | Redefine your `Bulk_quote` class to inherit its constructors. 4 | 5 | ### Solution 6 | 7 | See program files. 8 | -------------------------------------------------------------------------------- /01/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.09 2 | 3 | Write a program that uses a `while` loop to sum the numbers from 50 to 100. 4 | 5 | ### Solution 6 | 7 | See `9.cc`. 8 | -------------------------------------------------------------------------------- /01/13/1.10.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | for (int i = 10; i >= 0; i--) 6 | std::cout << i << std::endl; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /03/45/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.45 2 | 3 | Rewrite the programs again, this time using `auto`. 4 | 5 | ### Solution 6 | 7 | See `45.cc`, `45b.cc` and `45c.cc`. 8 | -------------------------------------------------------------------------------- /06/08/Chapter6.h: -------------------------------------------------------------------------------- 1 | #ifndef CHAPTER6_H 2 | #define CHAPTER6_H 3 | int fact(int val); 4 | int interact(void); 5 | int abs(int val); 6 | int callNo(void); 7 | #endif 8 | -------------------------------------------------------------------------------- /06/09/Chapter6.h: -------------------------------------------------------------------------------- 1 | #ifndef CHAPTER6_H 2 | #define CHAPTER6_H 3 | int fact(int val); 4 | int interact(void); 5 | int abs(int val); 6 | int callNo(void); 7 | #endif 8 | -------------------------------------------------------------------------------- /07/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.13 2 | 3 | Rewrite the program from page 255 to use the `istream` constructor. 4 | 5 | ### Solution 6 | 7 | See program files. 8 | -------------------------------------------------------------------------------- /14/27/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.27 2 | 3 | Add incremenet and decremenet operators to your `StrBlobPtr` class. 4 | 5 | ### Solution 6 | 7 | See program files. 8 | -------------------------------------------------------------------------------- /01/24/in.txt: -------------------------------------------------------------------------------- 1 | abc 1 19.99 2 | abc 2 29.99 3 | abc 10 99.99 4 | def 10 19.99 5 | def 1 1.99 6 | def 15 25.99 7 | ghi 2 23.95 8 | jkl 1 23.95 9 | jkl 10 239.50 10 | -------------------------------------------------------------------------------- /02/08/8.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | std::cout << "2M\n" << std::endl; 6 | std::cout << "2\tM\n" << std::endl; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /15/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.03 2 | 3 | Define your own versions of the `Quote` class and the `print_total` function. 4 | 5 | ### Solution 6 | 7 | See `quote.h`. 8 | -------------------------------------------------------------------------------- /02/41/1.24.in.txt: -------------------------------------------------------------------------------- 1 | abc 1 19.99 2 | abc 2 29.99 3 | abc 10 99.99 4 | def 10 19.99 5 | def 1 1.99 6 | def 15 25.99 7 | ghi 2 23.95 8 | jkl 1 23.95 9 | jkl 10 239.50 10 | -------------------------------------------------------------------------------- /02/42/1.24.in.txt: -------------------------------------------------------------------------------- 1 | abc 1 19.99 2 | abc 2 29.99 3 | abc 10 99.99 4 | def 10 19.99 5 | def 1 1.99 6 | def 15 25.99 7 | ghi 2 23.95 8 | jkl 1 23.95 9 | jkl 10 239.50 10 | -------------------------------------------------------------------------------- /03/01/1.24.in.txt: -------------------------------------------------------------------------------- 1 | abc 1 19.99 2 | abc 2 29.99 3 | abc 10 99.99 4 | def 10 19.99 5 | def 1 1.99 6 | def 15 25.99 7 | ghi 2 23.95 8 | jkl 1 23.95 9 | jkl 10 239.50 10 | -------------------------------------------------------------------------------- /06/09/factMain.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Chapter6.h" 3 | 4 | int main(void) 5 | { 6 | std::cout << "5! is " << fact(5) << std::endl; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /07/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.03 2 | 3 | Revice your transaction-processing program from § 7.1.1 to use these 4 | members. 5 | 6 | ### Solution 7 | 8 | See `3.cc`. 9 | -------------------------------------------------------------------------------- /12/30/test.txt: -------------------------------------------------------------------------------- 1 | this is a test 2 | try to find a good word 3 | there are many test words 4 | is this a good word 5 | maybe this is a bad word 6 | either way it is just a test 7 | -------------------------------------------------------------------------------- /06/09/fact.cc: -------------------------------------------------------------------------------- 1 | #include "Chapter6.h" 2 | 3 | int fact(int val) 4 | { 5 | int ret = 1; 6 | while (val > 1) 7 | ret *= val--; 8 | return ret; 9 | } 10 | -------------------------------------------------------------------------------- /09/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.16 2 | 3 | Repeat the previous program, but compare elements in a `list` to a 4 | `vector`. 5 | 6 | ### Solution 7 | 8 | See `16.cc`. 9 | -------------------------------------------------------------------------------- /09/27/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.27 2 | 3 | Write a program to find and remove the odd-valued elements in a 4 | `forward_list`. 5 | 6 | ### Solution 7 | 8 | See `27.cc`. 9 | -------------------------------------------------------------------------------- /13/26/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.26 2 | 3 | Write your own version of the `StrBlob` class described in the previous 4 | exercise. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /13/40/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.40 2 | 3 | Add a constructor that takes an `initializer_list` to your `StrVec` 4 | class. 5 | 6 | ### Solution 7 | 8 | See program file. 9 | -------------------------------------------------------------------------------- /13/54/54.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "HasPtr_bad.h" 3 | 4 | int main() 5 | { 6 | HasPtr hp; 7 | hp = HasPtr(); // ambiguous call 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /14/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.08 2 | 3 | Define an output operator for the class you chose in exercise 7.40 from § 4 | 7.5.1. 5 | 6 | ### Solution 7 | 8 | See `book.h` 9 | -------------------------------------------------------------------------------- /14/44/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.44 2 | 3 | Write your own version of a simple desk calculator that can handle binary 4 | operations. 5 | 6 | ### Solution 7 | 8 | See `44.cc`. 9 | -------------------------------------------------------------------------------- /01/10/10.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i = 10; 6 | 7 | while (i >= 0) 8 | std::cout << i-- << std::endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /01/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.16 2 | 3 | Write your own version of a program that prints the sum of a set of integers 4 | read from `cin`. 5 | 6 | ### Solution 7 | 8 | See `16.cc`. 9 | -------------------------------------------------------------------------------- /03/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.14 2 | 3 | Write a program to read a sequence of `int`s from `cin` and store those values 4 | in a `vector`. 5 | 6 | ### Solution 7 | 8 | See `14.cc`. 9 | -------------------------------------------------------------------------------- /04/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.06 2 | 3 | Write an expression to determine whether an `int` value is even or odd. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | i & 1; 9 | ``` 10 | -------------------------------------------------------------------------------- /09/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.07 2 | 3 | What type should be used as the index into a `vector` of `int`s? 4 | 5 | ### Solution 6 | 7 | It should be a `std::vector::size_type`. 8 | -------------------------------------------------------------------------------- /10/29/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.29 2 | 3 | Write a program using stream iterators to read a text file into a `vector` of 4 | `string`s. 5 | 6 | ### Solution 7 | 8 | See `29.cc`. 9 | -------------------------------------------------------------------------------- /14/20/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.20 2 | 3 | Define the addition and compound-assignment operators for your `Sales_data` 4 | class. 5 | 6 | ### Solution 7 | 8 | See `Sales_data.h`. 9 | -------------------------------------------------------------------------------- /14/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.23 2 | 3 | Define an `initializer_list` assignment operator for your version of the 4 | `StrVec` class. 5 | 6 | ### Solution 7 | 8 | See `strvec.h.` 9 | -------------------------------------------------------------------------------- /03/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.21 2 | 3 | Redo the first exercise from § 3.3.3 (p. 105) using iterators. 4 | 5 | ### Solution 6 | 7 | See `21.cc` for a rewrite of exercise 3.16. 8 | -------------------------------------------------------------------------------- /07/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.23 2 | 3 | Write your own version of the `Screen` class. 4 | 5 | ### Solution 6 | 7 | See `screen.h`. Note that the `access_ctr` member has not been added. 8 | -------------------------------------------------------------------------------- /10/22/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.22 2 | 3 | Rewrite the program to count words of size 6 or less using functions in place of 4 | the lambdas. 5 | 6 | ### Solution 7 | 8 | See `22.cc`. 9 | -------------------------------------------------------------------------------- /13/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.11 2 | 3 | Add a destructor to your `HasPtr` class from the previous exercises. 4 | 5 | ```cpp 6 | ~HasPtr() { 7 | delete ps; 8 | } 9 | ``` 10 | -------------------------------------------------------------------------------- /14/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.07 2 | 3 | Define an output operator for your `String` class you wrote for the exercises in 4 | § 13.5. 5 | 6 | ### Solution 7 | 8 | See `string.h`. 9 | -------------------------------------------------------------------------------- /14/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.18 2 | 3 | Define relational operators for you `StrBlob`, `StrBlobPtr`, `StrVec` and 4 | `String` classes. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /14/26/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.26 2 | 3 | Define subscript operators for you `StrVec`, `String`, `StrBlob` and 4 | `StrBlobPtr` classes. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /01/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.21 2 | 3 | Write a program that reads two `Sales_item` objects that have the same ISBN and 4 | produces their sum. 5 | 6 | ### Solution 7 | 8 | See `21.cc`. 9 | -------------------------------------------------------------------------------- /01/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.23 2 | 3 | Write a program that reads several transactions and counts how many transactions 4 | occur for each ISBN. 5 | 6 | ### Solution 7 | 8 | See `23.cc`. 9 | -------------------------------------------------------------------------------- /03/25/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.25 2 | 3 | Rewrite the grade clustering program from § 3.3.3 (p. 104) using iterators 4 | instead of subscripts. 5 | 6 | ### Solution 7 | 8 | See `25.cc`. 9 | -------------------------------------------------------------------------------- /05/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.09 2 | 3 | Write a program using a series of `if` statements to count the number of vowels 4 | in text read from `cin`. 5 | 6 | ### Solution 7 | 8 | See `9.cc`. 9 | -------------------------------------------------------------------------------- /07/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.11 2 | 3 | Add constructors to your `Sales_data` class, and write a program to use each of 4 | the constructors. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /08/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.13 2 | 3 | Rewrite the phone number program from this section to read from a named file 4 | rather than from `cin`. 5 | 6 | ### Solution 7 | 8 | See `13.cc`. 9 | -------------------------------------------------------------------------------- /11/33/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.33 2 | 3 | Implement your own version of the word-transformation program. 4 | 5 | ### Solution 6 | 7 | See `33.cc` with example `rules.txt` and `in.txt` files. 8 | -------------------------------------------------------------------------------- /13/39/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.39 2 | 3 | Write your own version of `StrVec`, including versions of `reserve`, `capacity` 4 | and `resize`. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /14/39/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.39 2 | 3 | Revise the previous program to report the count of words that are of sizes 1 4 | through 9 and 10 or more. 5 | 6 | ### Solution 7 | 8 | See `39.cc`. 9 | -------------------------------------------------------------------------------- /16/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 16.01 2 | 3 | Define instantiation. 4 | 5 | ### Solution 6 | 7 | Instantiation is when an object is actually created, i.e., an instance of a 8 | class is made. 9 | -------------------------------------------------------------------------------- /03/24/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.24 2 | 3 | Redo the last exercise from § 3.3.3 (p. 105) using iterators. 4 | 5 | ### Solution 6 | 7 | See `24.cc` and `24b.cc` for a rewrite of exercise 3.20. 8 | -------------------------------------------------------------------------------- /05/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.11 2 | 3 | Modify our vowel-counting program so that it also counts the number of blank 4 | spaces, tabs and newlines read. 5 | 6 | ### Solution 7 | 8 | See `11.cc`. 9 | -------------------------------------------------------------------------------- /07/31/31.h: -------------------------------------------------------------------------------- 1 | #ifndef 31_H_ 2 | #define 31_H_ 3 | #include 4 | 5 | struct Y; 6 | 7 | struct X { 8 | Y *py; 9 | }; 10 | 11 | struct Y { 12 | X myx; 13 | }; 14 | #endif 15 | -------------------------------------------------------------------------------- /07/31/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.31 2 | 3 | Define a pair of classes `X` and `Y`, in which `X` has a pointer to `Y`, and `Y` 4 | has an object of type `X`. 5 | 6 | ### Solution 7 | 8 | See `31.h`. 9 | -------------------------------------------------------------------------------- /09/38/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.38 2 | 3 | Write a program to explore how `vector`s grow in the library you use. 4 | 5 | ### Solution 6 | 7 | See `38.cc`. My machine doubles capacity each time. 8 | -------------------------------------------------------------------------------- /12/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.02 2 | 3 | Write your own version of the `StrBlob` class including the `const` versions of 4 | `front` and `back`. 5 | 6 | ### Solution 7 | 8 | See `StrBlob.h`. 9 | -------------------------------------------------------------------------------- /13/49/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.49 2 | 3 | Add a move constructor and move-assignment operator to your `StrVec`, `String` 4 | and `Message` classes. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /15/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.01 2 | 3 | What is a virtual member? 4 | 5 | ### Solution 6 | 7 | A virtual member is a member of a class which is expected to be overridden by a 8 | derived class. 9 | -------------------------------------------------------------------------------- /01/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.05 2 | 3 | We wrote the output in one large statement. Rewrite the program to use a 4 | separate statement to print each operand. 5 | 6 | ### Solution 7 | 8 | See `5.cc`. 9 | -------------------------------------------------------------------------------- /03/31/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.31 2 | 3 | Write a program to define an array of ten `int`s. Give each element the same 4 | value as its posiion in the array. 5 | 6 | ### Solution 7 | 8 | See `31.cc`. 9 | -------------------------------------------------------------------------------- /07/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.07 2 | 3 | Rewrite the transaction-processing program you wrote for the exercises in § 4 | 7.1.2 to use these new functions. 5 | 6 | ### Solution 7 | 8 | See `7.cc`. 9 | -------------------------------------------------------------------------------- /07/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.09 2 | 3 | Add operations to read and print `Person` objects to the code you wrote for the 4 | exercises in § 7.1.2. 5 | 6 | ### Solution 7 | 8 | See `person.cc`. 9 | -------------------------------------------------------------------------------- /09/36/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.36 2 | 3 | Can a container have a `capacity` less than its `size`? 4 | 5 | ### Solution 6 | 7 | No. The `capacity` must always be greater than or equial to its `size`. 8 | -------------------------------------------------------------------------------- /14/28/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.28 2 | 3 | Define addition and subtraction for `StrBlobPtr` so that these operators 4 | implement pointer arithmetic. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /15/36/36.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "query.h" 4 | 5 | int main() 6 | { 7 | Query q = Query("fiery") & Query("bird") | Query("wind"); 8 | std::cout << q << std::endl; 9 | } 10 | -------------------------------------------------------------------------------- /03/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.02 2 | 3 | Write a program to read the standard input a line at a time. Modify your program 4 | to read a word at a time. 5 | 6 | ### Solution 7 | 8 | See `2.cc` and `2b.cc`. 9 | -------------------------------------------------------------------------------- /03/36/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.36 2 | 3 | Write a program to compare two arrays for equality. Write a similar program to 4 | compare two `vector`s. 5 | 6 | ### Solution 7 | 8 | See `36.cc` and `36b.cc`. 9 | -------------------------------------------------------------------------------- /06/25/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.25 2 | 3 | Write a `main` function that takes two arguments. Concatenate the supplied 4 | arguments and print the resulting `string`. 5 | 6 | ### Solution 7 | 8 | See `25.cc`. 9 | -------------------------------------------------------------------------------- /08/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.09 2 | 3 | Use the function you wrote for the first exercise in § 8.1.2 to print the 4 | contents of an `istringstream` object. 5 | 6 | ### Solution 7 | 8 | See `9.cc`. 9 | -------------------------------------------------------------------------------- /09/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.02 2 | 3 | Define a `list` that holds elements that are `deque`s that hold `int`s. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | std::list> ldnums; 9 | ``` 10 | -------------------------------------------------------------------------------- /10/37/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.37 2 | 3 | Given a `vector` that has ten elements, copy the elements from positions 3 4 | through 7 in reverse order to a `list`. 5 | 6 | ### Solution 7 | 8 | See `37.cc`. 9 | -------------------------------------------------------------------------------- /14/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.32 2 | 3 | Define a class that holds a pointer to a `StrBlobPtr`. Define the overloaded 4 | arrow operator for that class. 5 | 6 | ### Solution 7 | 8 | See `StrBlobPtrPtr.h`. 9 | -------------------------------------------------------------------------------- /01/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.13 2 | 3 | Rewrite the exercises from § 1.4.1 (p. 13) using `for` loops. 4 | 5 | ### Solution 6 | 7 | See `1.9.cc`, `1.10.cc` and `1.11.cc` for rewrites of those exercises. 8 | -------------------------------------------------------------------------------- /01/22/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.22 2 | 3 | Write a program that reads several transactions for the same ISBN. Write the sum 4 | of all the transactions that were read. 5 | 6 | ### Solution 7 | 8 | See `22.c`. 9 | -------------------------------------------------------------------------------- /01/25/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.25 2 | 3 | Using the `Sales_item.h` header from the Web site, compile and execute the 4 | bookstore program presented in this section. 5 | 6 | ### Solution 7 | 8 | See `25.cc`. 9 | -------------------------------------------------------------------------------- /03/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.32 2 | 3 | Copy the array you defined in the previous exercise into another array. Rewrite 4 | your program to use `vector`s. 5 | 6 | ### Solution 7 | 8 | See `32.cc` and `32b.cc`. 9 | -------------------------------------------------------------------------------- /06/26/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.26 2 | 3 | Write a program that accepts the options presented in this section. Print the 4 | values of the arguments passed to `main`. 5 | 6 | ### Solution 7 | 8 | See `26.cc`. 9 | -------------------------------------------------------------------------------- /07/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.12 2 | 3 | Move the definition of the `Sales_data` constructor that takes an `istream` into 4 | the body of the `Sales_data` class. 5 | 6 | ### Solution 7 | 8 | See `Sales_data.h`. 9 | -------------------------------------------------------------------------------- /07/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.32 2 | 3 | Define your own versions of `Screen` and `Window_mgr` in which `clear` is a 4 | member of `Window_mgr` and a friend of `Screen`. 5 | 6 | ### Solution 7 | 8 | See `32.h`. 9 | -------------------------------------------------------------------------------- /10/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.14 2 | 3 | Write a lambda that takes two `int`s and returns their sum. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | [](const int &i, const int &j) { return i+j; } 9 | ``` 10 | -------------------------------------------------------------------------------- /11/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.12 2 | 3 | Write a program to read a sequence of `string`s and `int`s, storing each into a 4 | `pair`. Store the `pair`s in a `vector`. 5 | 6 | ### Solution 7 | 8 | See `12.cc`. 9 | -------------------------------------------------------------------------------- /13/42/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.42 2 | 3 | Test your `StrVec` class by using it in place of the `vector` in your 4 | `TextQuery` and `QueryResult` classes. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /14/36/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.36 2 | 3 | Use the class from the previous exercise to read the standard input, storing 4 | each line as an element in a `vector`. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /15/39/39.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "query.h" 4 | 5 | int main() 6 | { 7 | Query q = Query("fiery") & Query("bird") | Query("wind"); 8 | std::cout << q.rep() << std::endl; 9 | } 10 | -------------------------------------------------------------------------------- /15/42/39.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "query.h" 4 | 5 | int main() 6 | { 7 | Query q = Query("fiery") & Query("bird") | Query("wind"); 8 | std::cout << q.rep() << std::endl; 9 | } 10 | -------------------------------------------------------------------------------- /03/01/1.10.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | int i = 10; 8 | 9 | while (i >= 0) 10 | cout << i-- << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /09/46/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.46 2 | 3 | Rewrite the previous exercise using a position and length to manage the 4 | `string`s. This time only use the `insert` function. 5 | 6 | ### Solution 7 | 8 | See `46.cc`. 9 | -------------------------------------------------------------------------------- /14/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.12 2 | 3 | Define an input operator for the class you used in exercise 7.40 from § 4 | 7.5.1. Be sure the operator handles input errors. 5 | 6 | ### Solution 7 | 8 | See `book.h`. 9 | -------------------------------------------------------------------------------- /15/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.11 2 | 3 | Add a virtual `debug` function to your `Quote` class hierarchy that displays the 4 | data members of the respective classes. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /15/35/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.35 2 | 3 | Implement the `Query` and `Query_base` classes, including a definition of `rep` 4 | but omitting the definition of `eval`. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /01/20/20.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_item.h" 3 | 4 | int main() 5 | { 6 | Sales_item book; 7 | while (std::cin >> book) 8 | std::cout << book << std::endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /02/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.04 2 | 3 | Write a program to check whether your predictions were correct. If not, study 4 | this section until you understand what the problem is. 5 | 6 | ### Solution 7 | 8 | See `4.cc`. 9 | -------------------------------------------------------------------------------- /03/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.10 2 | 3 | Write a program that reads a string of characters including punctuation and 4 | writes what was read but with the punctuation removed. 5 | 6 | ### Solution 7 | 8 | See `10.cc`. 9 | -------------------------------------------------------------------------------- /03/39/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.39 2 | 3 | Write a program to compare two `string`s. Now write a program to compare the 4 | values of two C-style character strings. 5 | 6 | ### Solution 7 | 8 | See `39.cc` and `39b.cc`. 9 | -------------------------------------------------------------------------------- /05/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.23 2 | 3 | Write a program that reads two integers from the standard input and prints the 4 | result of dividing the first number by the second. 5 | 6 | ### Solution 7 | 8 | See `23.cc`. 9 | -------------------------------------------------------------------------------- /06/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.08 2 | 3 | Write a header file named `Chapter6.h` that contains declarations for the 4 | functions you wrote for the exercises in § 6.1. 5 | 6 | ### Solution 7 | 8 | See `Chapter6.h`. 9 | -------------------------------------------------------------------------------- /06/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.10 2 | 3 | Using pointers, write a function to swap the values of two `int`s. Test the 4 | function by calling it and printing the swapped values. 5 | 6 | ### Solution 7 | 8 | See `10.cc`. 9 | -------------------------------------------------------------------------------- /09/37/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.37 2 | 3 | Why don't `list` or `array` have a `capacity` member? 4 | 5 | ### Solution 6 | 7 | Because `list`s do not hold elements in a continuous area, and `array`s have a 8 | fixed size. 9 | -------------------------------------------------------------------------------- /10/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.23 2 | 3 | How many arguments does `bind` take? 4 | 5 | ### Solution 6 | 7 | `bind` takes at least one argument, a function, as well as the amount of 8 | arguments the passed function has. 9 | -------------------------------------------------------------------------------- /10/31/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.31 2 | 3 | Update the program from the previous exercise so that it prints only the unique 4 | elements. Your program should use `unique_copy`. 5 | 6 | ### Solution 7 | 8 | See `31.cc`. 9 | -------------------------------------------------------------------------------- /12/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.32 2 | 3 | Rewrite the `TextQuery` and `QueryResult` classes to use a `StrBlob` instead of 4 | a `vector` to hold the input file. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /13/30/30.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "HasPtr.h" 3 | 4 | using std::swap; 5 | 6 | int main() 7 | { 8 | HasPtr a("hello"); 9 | HasPtr b("world"); 10 | 11 | swap(a,b); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /14/38/input.txt: -------------------------------------------------------------------------------- 1 | Write a class that tests whether the length of a given string matches a given 2 | bound. Use that object to write a program to report how many words in an input 3 | file are of sizes 1 through 10 inclusive. 4 | -------------------------------------------------------------------------------- /14/39/input.txt: -------------------------------------------------------------------------------- 1 | Write a class that tests whether the length of a given string matches a given 2 | bound. Use that object to write a program to report how many words in an input 3 | file are of sizes 1 through 10 inclusive. 4 | -------------------------------------------------------------------------------- /07/45/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.45 2 | 3 | What if we defined the `vector` in the previous exercise to hold objects of type 4 | `C`? 5 | 6 | ### Solution 7 | 8 | The statement would successfully default-initialize and compile. 9 | -------------------------------------------------------------------------------- /12/16/16.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::unique_ptr p1(new int(42)); 7 | // std::unique_ptr p2(p1); 8 | std::unique_ptr p3; 9 | p3 = p1; 10 | } 11 | -------------------------------------------------------------------------------- /12/30/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.30 2 | 3 | Define your own versions of the `TextQuery` and `QueryResult` classes and 4 | execute the `runQueries` function from § 12.3.1. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /13/36/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.36 2 | 3 | Design and implement the corresponding `Folder` class. That class should hold a 4 | `set` that points to the `Message`s in that `Folder`. 5 | 6 | ### Solution 7 | 8 | See `folder.h`. 9 | -------------------------------------------------------------------------------- /13/58/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.58 2 | 3 | Write versions of class `Foo` with print statements in their `sorted` functions 4 | to test your answers to the previous two exercises. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /01/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.15 2 | 3 | Write programs that contain the common errors discussed in the box on page 16. 4 | Familiarize yourself with the message the compiler generates. 5 | 6 | ### Solution 7 | 8 | See `15.cc`. 9 | -------------------------------------------------------------------------------- /05/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.12 2 | 3 | Modify our vowel-counting program so that it counts the number of occurrences of 4 | the following two-character sequences: `ff`, `fl` and `fi`. 5 | 6 | ### Solution 7 | 8 | See `12.cc`. 9 | -------------------------------------------------------------------------------- /05/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.21 2 | 3 | Revise the program from the exercise in § 5.5.1 (p. 191) so that it looks 4 | only for duplicated words that start with an uppercase letter. 5 | 6 | ### Solution 7 | 8 | See `21.cc`. 9 | -------------------------------------------------------------------------------- /06/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.04 2 | 3 | Write a function that interacts with the user, asking for a number and 4 | generating the factorial of that number. Call this function from `main`. 5 | 6 | ### Solution 7 | 8 | See `4.cc`. 9 | -------------------------------------------------------------------------------- /07/41/41.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_data.h" 3 | 4 | int main() 5 | { 6 | Sales_data s1; 7 | Sales_data s3("Book3"); 8 | Sales_data s4("Book4", 24, 9.95); 9 | Sales_data s2(std::cin); 10 | } 11 | -------------------------------------------------------------------------------- /08/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.06 2 | 3 | Rewrite the bookstore program from § 7.1.1 to read its transactions from a 4 | file. Pass the name of the file as an argument to `main`. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /10/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.10 2 | 3 | Why do you think the algorithms don't change the size of containers? 4 | 5 | ### Solution 6 | 7 | Because not all containers support size modifications (`array`s, built-in 8 | arrays). 9 | -------------------------------------------------------------------------------- /11/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.04 2 | 3 | Extend your program to ignore case and punctuation. For example, "example.", 4 | "example," and "Example" should all increment the same counter. 5 | 6 | ### Solution 7 | 8 | See `4.cc`. 9 | -------------------------------------------------------------------------------- /13/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.17 2 | 3 | Write versions of `numbered` and `f` corresponding to the previous three 4 | exercises and check whether you correctly predicted the output. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /13/47/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.47 2 | 3 | Give the copy constructor and copy-assignment operator in your `String` class 4 | from exercise 13.44 in § 13.5 a statement that prints a message each time 5 | the function is executed. 6 | -------------------------------------------------------------------------------- /15/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.06 2 | 3 | Test your `print_total` function from the exercises in § 15.2.1 by passing 4 | both `Quote` and `Bulk_quote` objects to that function. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /02/42/Sales_data.h: -------------------------------------------------------------------------------- 1 | #ifndef SALES_DATA_H 2 | #define SALES_DATA_H 3 | #include 4 | struct Sales_data { 5 | std::string name = ""; 6 | unsigned int units_sold = 0; 7 | double revenue = 0.0; 8 | }; 9 | #endif 10 | -------------------------------------------------------------------------------- /03/01/Sales_data.h: -------------------------------------------------------------------------------- 1 | #ifndef SALES_DATA_H 2 | #define SALES_DATA_H 3 | #include 4 | struct Sales_data { 5 | std::string name = ""; 6 | unsigned int units_sold = 0; 7 | double revenue = 0.0; 8 | }; 9 | #endif 10 | -------------------------------------------------------------------------------- /04/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.21 2 | 3 | Write a program to use a conditional operator to find the elements in a 4 | `vector` that have odd value and double the value of each such element. 5 | 6 | ### Solution 7 | 8 | See `21.cc`. 9 | -------------------------------------------------------------------------------- /05/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.19 2 | 3 | Write a program that uses a `do while` loop to repetitively request two 4 | `string`s from the user and report which `string` is less than the other. 5 | 6 | ### Solution 7 | 8 | See `19.cc`. 9 | -------------------------------------------------------------------------------- /06/28/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.28 2 | 3 | In the second version of `error_msg` that has an `ErrCode` parameter, what is 4 | the type of `elem` in the `for` loop? 5 | 6 | ### Solution 7 | 8 | `elem` is a reference to a `const string`. 9 | -------------------------------------------------------------------------------- /07/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.01 2 | 3 | Write a version of the transaction-processing program from § 1.6 using the 4 | `Sales_data` class you defined for the exercises in § 2.6.1. 5 | 6 | ### Solution 7 | 8 | See `1.cc`. 9 | -------------------------------------------------------------------------------- /11/38/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.38 2 | 3 | Rewrite the word-counting (§ 11.1) and word-transformation (§ 11.3.6) 4 | programs to use an `unordered_map`. 5 | 6 | ### Solution 7 | 8 | see `word-count.cc`. and `transform.cc`. 9 | -------------------------------------------------------------------------------- /12/20/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.20 2 | 3 | Write a program that reads an input file a line at a time into a `StrBlob` and 4 | uses a `StrBlobPtr` to print each element in that `StrBlob`. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /13/30/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.30 2 | 3 | Write and test a `swap` function for your valuelike version of `HasPtr`. Give 4 | your `swap` a print statement that notes when it is executed. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /14/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.17 2 | 3 | Should the class you wrote for exercise 7.40 from § 7.5.1 define the 4 | equality operators? If so, implement them. If not explain why not. 5 | 6 | ### Solution 7 | 8 | Yes. See `book.h`. 9 | -------------------------------------------------------------------------------- /15/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.16 2 | 3 | Rewrite the class representing a limited discount strategy, which you wrote for 4 | the exercises in §15.2.2, to inherit from `Disc_quote`. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /01/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.04 2 | 3 | Our program used the addition operator, `+`, to add two numbers. Write a program 4 | that uses the multiplication operator, `*`, to print the product instead. 5 | 6 | ### Solution 7 | 8 | See `4.cc`. 9 | -------------------------------------------------------------------------------- /02/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.17 2 | 3 | What does the following code print? 4 | 5 | ```cpp 6 | int i, &ri = i; 7 | i = 5; ri = 10; 8 | std::cout << i << " " << ri << std::endl; 9 | ``` 10 | 11 | ### Solution 12 | 13 | `10 10` 14 | -------------------------------------------------------------------------------- /06/22/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.22 2 | 3 | Write a function to swap two `int` pointers. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | void swap(int *i, int *j) 9 | { 10 | int *temp = i; 11 | i = j; 12 | j = i; 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /07/05/person.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Person { 4 | std::string name; 5 | std::string address; 6 | const std::string getName() const { return name; } 7 | const std::string getAddress() const { return address; } 8 | }; 9 | -------------------------------------------------------------------------------- /08/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.07 2 | 3 | Revise the bookstore program from the previous section to write its output to a 4 | file. Pass the name of that file as a second argument to `main`. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /08/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.10 2 | 3 | Write a program to store each line from a file in a `vector`. Now use an 4 | `istringstream` to read each element from the `vector` a word at a time. 5 | 6 | ### Solution 7 | 8 | See `10.cc`. 9 | -------------------------------------------------------------------------------- /10/39/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.39 2 | 3 | What kind of iterator does a `list` have? What about a `vector`? 4 | 5 | ### Solution 6 | 7 | A `list` should have a bidirectional iterator, and a `vector` should have a 8 | random-access iterator. 9 | -------------------------------------------------------------------------------- /12/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.19 2 | 3 | Define your own version of `StrBlobPtr` and update your `StrBlob` class with the 4 | appropriate `friend` declaration and `begin` and `end` members. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /01/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.18 2 | 3 | Compile and run the program from this section giving it only equal values as 4 | input. Run it again giving it values in which no number is repeated. 5 | 6 | ### Solution 7 | 8 | See `18.cc` for source. 9 | -------------------------------------------------------------------------------- /02/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.08 2 | 3 | Using escape sequences, write a program to print `2M` followed by a newline. 4 | Modify the program to print `2`, then a tab, then an `M`, followed by a newline. 5 | 6 | ### Solution 7 | 8 | See `8.cc`. 9 | -------------------------------------------------------------------------------- /06/38/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.38 2 | 3 | Revise the `arrPtr` function to return a reference to the array. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | decltype(odd) &arrPtr(int i) 9 | { 10 | return (i % 2) ? odd : even 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /09/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.18 2 | 3 | Write a program to read a sequence of `string`s from the standard input into a 4 | `deque`. Use iterators to write a loop to print the elements in the `deque`. 5 | 6 | ### Solution 7 | 8 | See `18.cc`. 9 | -------------------------------------------------------------------------------- /09/20/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.20 2 | 3 | Write a program to copy elements from a `list` into two `deque`s. The 4 | even-valued elements should go into one `deque` and the odd ones into the other. 5 | 6 | ### Solution 7 | 8 | See `20.cc`. 9 | -------------------------------------------------------------------------------- /10/30/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.30 2 | 3 | Use stream iterators, `sort` and `copy` to read a sequence of integers from the 4 | standard input, sort them and then write them back to the standard output. 5 | 6 | ### Solution 7 | 8 | See `30.cc`. 9 | -------------------------------------------------------------------------------- /11/33/rules.txt: -------------------------------------------------------------------------------- 1 | brb be right back 2 | k okay 3 | y why 4 | r are 5 | u you 6 | pic picture 7 | thx thanks 8 | thk thanks 9 | l8r later 10 | dont don't 11 | cant can't 12 | lmk let me know 13 | omw on my way 14 | im I'm 15 | gonna going to 16 | -------------------------------------------------------------------------------- /11/38/rules.txt: -------------------------------------------------------------------------------- 1 | brb be right back 2 | k okay 3 | y why 4 | r are 5 | u you 6 | pic picture 7 | thx thanks 8 | thk thanks 9 | l8r later 10 | dont don't 11 | cant can't 12 | lmk let me know 13 | omw on my way 14 | im I'm 15 | gonna going to 16 | -------------------------------------------------------------------------------- /02/42/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.42 2 | 3 | Write your own version of the `Sales_data.h` header and use it to rewrite the 4 | exercise from § 2.6.2 (p. 76). 5 | 6 | ### Solution 7 | 8 | See program files for rewrites of exercise 1.20 through 1.25. 9 | -------------------------------------------------------------------------------- /11/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.18 2 | 3 | Write the type of `map_it` from the loop on page 430 without using `auto` or 4 | `decltype`. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | std::map::iterator map_it; 10 | ``` 11 | -------------------------------------------------------------------------------- /04/26/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.26 2 | 3 | In our grading example in this section, what would happen if we used `unsigned 4 | int` as the type for `quiz1`? 5 | 6 | ### Solution 7 | 8 | `quiz1` would not be guaranteed at least `32` bits in length, but `16`. 9 | -------------------------------------------------------------------------------- /10/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.06 2 | 3 | Using `fill_n`, write a program to set a sequence of `int` values to 0. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | std::vector v{1,2,3,4,5,6,7,8,9}; 9 | std::fill_n(v.begin(), v.size(), 0); 10 | ``` 11 | -------------------------------------------------------------------------------- /11/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.11 2 | 3 | Redefine `bookstore` without using `decltype`. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | std::multiset 9 | bookstore(compareIsbn); 10 | ``` 11 | -------------------------------------------------------------------------------- /12/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.18 2 | 3 | Why doesn't `shared_ptr` have a `release` member? 4 | 5 | ### Solution 6 | 7 | Becuase other `shared_ptr`s pointing to the same object could still delete the 8 | object, resulting in an illegal duplicate deletion. 9 | -------------------------------------------------------------------------------- /15/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.02 2 | 3 | How does the `protected` access specifier differ from `private`? 4 | 5 | ### Solution 6 | 7 | The `protected` access specifier allows derived classes to access its members, 8 | while `private` access does not. 9 | -------------------------------------------------------------------------------- /15/28/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.28 2 | 3 | Define a `vector` to hold `Quote` objects but put `Bulk_quote` objects into that 4 | `vector`. Compute the total `net_price` of all the elements in the `vector`. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | -------------------------------------------------------------------------------- /01/13/1.09.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int sum = 0; 6 | 7 | for (int count = 50; count <= 100; count++) 8 | sum += count; 9 | std::cout << "Sum of [50, 100]: " << sum << std::endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /01/21/21.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_item.h" 3 | 4 | int main() 5 | { 6 | Sales_item item1, item2; 7 | 8 | std::cin >> item1; 9 | std::cin >> item2; 10 | std::cout << item1 + item2 << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /03/29/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.29 2 | 3 | List some of the drawbacks of using an array instead of a `vector`. 4 | 5 | ### Solution 6 | 7 | Arrays are fixed-size, specified only by constant expressions and do not always 8 | default-initialize primitive arrays. 9 | -------------------------------------------------------------------------------- /09/38/38.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::vector v; 7 | while (v.size() < 100000) { 8 | std::cout << v.capacity() << std::endl; 9 | v.resize(v.capacity() + 1); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /10/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.03 2 | 3 | Use `accumulate` to sum the elements in a `vector` 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | int sum(const std::vector &v) 9 | { 10 | return accumulate(v.cbegin(), v.cend(), 0); 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /11/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.09 2 | 3 | Define a `map` that associates words with a `list` of line numbers on which the 4 | word might occur. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | std::map> word_locations; 10 | ``` 11 | -------------------------------------------------------------------------------- /16/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 16.02 2 | 3 | Write and test your own versions of the `compare` functions. 4 | 5 | ### Solution 6 | 7 | See `2.cc`. Although the pointer comparisons return identical results on my 8 | machine, only `compare2` is defined behavior. 9 | -------------------------------------------------------------------------------- /04/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.10 2 | 3 | Write the condition for a `while` loop that would read `int`s from the standard 4 | input and stop when the value read is equal to `42`. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | while (cin >> i && i != 42) 10 | ``` 11 | -------------------------------------------------------------------------------- /06/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.05 2 | 3 | Write a function to return the absolute value of its argument. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | int abs(int val) 9 | { 10 | if (val < 1) 11 | return -val; 12 | return val; 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /09/33/33.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | std::vector v{0,1,2,3,4,5}; 6 | auto begin = v.begin(); 7 | while (begin != v.end()) { 8 | ++begin; 9 | v.insert(begin, 42); 10 | ++begin; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /12/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.04 2 | 3 | In our `check` function we didn't check whether `i` was greater than zero. Why 4 | is it okay to omit this check? 5 | 6 | ### Solution 7 | 8 | The `size_type` alias for `vector::size_type` is an `unsigned` type. 9 | -------------------------------------------------------------------------------- /12/27/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.27 2 | 3 | The `TextQuery` and `QueryResult` classes use only capabilities that we have 4 | already covered. Without looking ahead, write your own versions of these 5 | classes. 6 | 7 | ### Solution 8 | 9 | See `Query.h`. 10 | -------------------------------------------------------------------------------- /09/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.19 2 | 3 | Rewrite the program from the previous exercise to use a `list`. List the changes 4 | you needed to make. 5 | 6 | ### Solution 7 | 8 | See `19.cc`. Find and replaced all instances of `deque` and replaces with 9 | `list`. 10 | -------------------------------------------------------------------------------- /10/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.09 2 | 3 | Implement your own version of `elimDups`. Test your program by printing the 4 | `vector` after you read the input, after the call to `unique` and after the call 5 | to `erase`. 6 | 7 | ### Solution 8 | 9 | See `9.cc`. 10 | -------------------------------------------------------------------------------- /11/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.01 2 | 3 | Describe the differences between a `map` and a `vector`. 4 | 5 | ### Solution 6 | 7 | A `map` stores elements as a key-value pair rather than a `vector`'s sequential 8 | approach (associative container vs. sequential container). 9 | -------------------------------------------------------------------------------- /14/29/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.29 2 | 3 | We did not define a `const` version of the increment and decrement operators. 4 | Why not? 5 | 6 | ### Solution 7 | 8 | Because those operations change the state of the object, therefore they cannot 9 | be `const`. 10 | -------------------------------------------------------------------------------- /01/24/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.24 2 | 3 | Test the previos program by giving multiple transactions representing multiple 4 | ISBNs. Te records for each ISBN should be grouped together. 5 | 6 | ### Solution 7 | 8 | See `in.txt` and `out.txt` for the complete input and output. 9 | -------------------------------------------------------------------------------- /04/04/4.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | cout << 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2 << endl; 8 | cout << (((12 / 3) * 4) + ((5 * 15) + ((24 % 4) / 2))) << endl; 9 | 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /09/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.21 2 | 3 | Explain how the loop from page 345 that used the return from `insert` to add 4 | elements to a `list` would work if we inserted into a `vector` instead. 5 | 6 | ### Solution 7 | 8 | It would work exactly the same at the cost of speed. 9 | -------------------------------------------------------------------------------- /10/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.11 2 | 3 | Write a program that uses `stable_sort` and `isShorter` to sort a `vector` 4 | passed to your version of `elimDups`. Print the `vector` to verify that your 5 | program is correct. 6 | 7 | ### Solution 8 | 9 | See `11.cc`. 10 | -------------------------------------------------------------------------------- /11/24/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.24 2 | 3 | What does the following program do? 4 | 5 | ```cpp 6 | map m; 7 | m[0] = 1; 8 | ``` 9 | 10 | ### Solution 11 | 12 | It creates an empty `map` of `int` to `int`, then adds an element with key 0 and 13 | value 1. 14 | -------------------------------------------------------------------------------- /13/37/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.37 2 | 3 | Add members into the `Message` class to insert or remove a given `Folder*` into 4 | `folders`. These members are analogous to `Folder`'s `addMsg` and `remMsg` 5 | operations. 6 | 7 | ### Solution 8 | 9 | See `message.h`. 10 | -------------------------------------------------------------------------------- /14/37/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.37 2 | 3 | Write a class that test whether two values are equal. Use that object and the 4 | library algorithms to write a program to replace all instances of a given value 5 | in a sequence. 6 | 7 | ### Solution 8 | 9 | See `37.cc`. 10 | -------------------------------------------------------------------------------- /15/06/6.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "quote.h" 3 | 4 | int main() 5 | { 6 | Quote q("123-4567-8910", 10.95); 7 | Bulk_quote b("321-7654-1098", 9.95, 20, 0.05); 8 | 9 | print_total(std::cout, q, 10); 10 | print_total(std::cout, b, 2); 11 | } 12 | -------------------------------------------------------------------------------- /02/40/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.40 2 | 3 | Write your own version of the `Sales_data` class. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | struct Sales_data { 9 | std::string name = ""; 10 | unsigned int units_sold = 0; 11 | double revenue = 0.0; 12 | }; 13 | ``` 14 | -------------------------------------------------------------------------------- /04/36/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.36 2 | 3 | Assuming `i` is an `int` and `d` is a `double` write the expression `i *= d` so 4 | that it does integral, rather than floating-point, multiplication. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | i *= static_cast(d); 10 | ``` 11 | -------------------------------------------------------------------------------- /06/44/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.44 2 | 3 | Rewrite the `isShorter` function from § 6.2.2 to be `inline`. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | inline bool isShorter(const string &s1, const string &s2) 9 | { 10 | return s1.size() < s2.size(); 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /07/55/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.55 2 | 3 | Is the `Data` class from § 7.5.5 a literal class? If not, why not? If so, 4 | explain why it is literal. 5 | 6 | ### Solution 7 | 8 | The `Data` class is not literal, because its `string` member is not a literal 9 | type. 10 | -------------------------------------------------------------------------------- /11/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.14 2 | 3 | Extend the `map` of children to their family name that you wrote for the 4 | exercises in § 11.2.1 by having the `vector` store a `pair` that holds a 5 | child's name and birthday. 6 | 7 | ### Solution 8 | 9 | See `14.cc`. 10 | -------------------------------------------------------------------------------- /14/33/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.33 2 | 3 | How many operands may an overloaded funcion-call operator take? 4 | 5 | ### Solution 6 | 7 | As many as necessary, given that they are of different types or different amount 8 | of operands than other overloaded function-call operators. 9 | -------------------------------------------------------------------------------- /14/49/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.49 2 | 3 | Regardless of whether it is a good idea to do so, define a conversion to `bool` 4 | for the class from the previous exercise. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | explicit operator bool() const { return dewey = 0; } 10 | ``` 11 | -------------------------------------------------------------------------------- /16/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 16.07 2 | 3 | Write a `constexpr` template that returns the size of a given array. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | template 9 | constexpr size_t length(T (&array)[M]) 10 | { 11 | return M; 12 | } 13 | ``` 14 | -------------------------------------------------------------------------------- /03/02/2b.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cin; 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | 8 | int main() 9 | { 10 | string word; 11 | while (cin >> word) 12 | cout << word << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /06/11/11.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void reset(int &i) 4 | { 5 | i = 0; 6 | } 7 | 8 | int main(void) 9 | { 10 | int i = 100; 11 | std::cout << "i = " << i << std::endl; 12 | reset(i); 13 | std::cout << "i = " << i << std::endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /06/30/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.30 2 | 3 | Compile the version of `str_subrange` as presented on page 223 to see what your 4 | compiler does with the indicated errors. 5 | 6 | ### Solution 7 | See `30.cc`. Compiler complained only about the `return` statement not returning 8 | a `bool`. 9 | -------------------------------------------------------------------------------- /06/42/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.42 2 | 3 | Give the second parameter of `make_plural` (§ 6.3.2) a default argument of 4 | `'s'`. Test your program by printing singular and plural versions of the words 5 | `success` and `failure`. 6 | 7 | ### Solution 8 | 9 | See `42.cc`. 10 | -------------------------------------------------------------------------------- /07/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.04 2 | Write a class named `Person` that represents the name and address of a person. 3 | Use a `string` to hold each of these elements. Subsequent exercises will 4 | incrementally add features to this class. 5 | 6 | ### Solution 7 | 8 | See `person.cc`. 9 | -------------------------------------------------------------------------------- /07/27/27.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "screen.h" 3 | 4 | int main(void) 5 | { 6 | Screen myScreen(5, 5, 'X'); 7 | myScreen.move(4, 0).set('#').display(std::cout); 8 | std::cout << "\n"; 9 | myScreen.display(std::cout); 10 | std::cout << "\n"; 11 | } 12 | -------------------------------------------------------------------------------- /07/54/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.54 2 | 3 | Should the members of `Debug` that begin with `set_` be declared as `constexpr`? 4 | If not, why not? 5 | 6 | ### Solution 7 | 8 | No, because they perform an action which is not a `return`, and therefore cannot 9 | be `constexpr`. 10 | -------------------------------------------------------------------------------- /09/30/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.30 2 | 3 | What, if any, restrictions does using the version of `resize` that takes a 4 | single argument place on the element type? 5 | 6 | ### Solution 7 | 8 | The element type must be able to be initialized with a default 9 | constructor. 10 | -------------------------------------------------------------------------------- /09/34/34.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | std::vector vi{0,1,2,3,4,5}; 6 | auto iter = vi.begin(); 7 | while (iter != vi.end()) { 8 | if (*iter % 2) 9 | iter = vi.insert(iter, *iter); 10 | ++iter; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /13/31/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.31 2 | 3 | Give your class a `<` operator and define a `vector` of `HasPtr`s. Give that 4 | `vector` some elements and then `sort` the `vector`. Note when `swap` is called. 5 | 6 | ### Solution 7 | 8 | See program files. `swap` was called three times. 9 | -------------------------------------------------------------------------------- /14/31/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.31 2 | 3 | Our `StrBlobPtr` class does not define the copy constructor, assignment operator 4 | or a destructor. Why? 5 | 6 | ### Solution 7 | 8 | Because we do not deal with dynamic allocation, the synthesized "Big Three" will 9 | be adequate. 10 | -------------------------------------------------------------------------------- /01/09/9.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int sum = 0, count = 50; 6 | 7 | while (count <= 100) 8 | { 9 | sum += count; 10 | count++; 11 | } 12 | std::cout << "Sum of [50, 100]: " << sum << std::endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /01/11/11.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int v1, v2; 6 | 7 | std::cout << "Enter two integers in ascending order:" << std::endl; 8 | std::cin >> v1 >> v2; 9 | while (v1 <= v2) 10 | std::cout << v1++ << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /03/02/2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cin; 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | 8 | int main() 9 | { 10 | string line; 11 | while (getline(cin, line)) 12 | cout << line << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /03/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.23 2 | 3 | Write a program to create a `vector` with ten `int` elements. Using an iterator, 4 | assign each element a value that is twice its current value. Test your program 5 | by printing the `vector`. 6 | 7 | ### Solution 8 | 9 | See `23.cc`. 10 | -------------------------------------------------------------------------------- /06/36/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.36 2 | 3 | Write the declaration for a function that returns a reference to an array of ten 4 | `string`s, without using either a trailing return, `decltype`, or a type alias. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | std::string (&f())[10]; 10 | ``` 11 | -------------------------------------------------------------------------------- /07/29/29.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "screen_faulty.h" 3 | 4 | int main(void) 5 | { 6 | Screen myScreen(5, 5, 'X'); 7 | myScreen.move(4, 0).set('#').display(std::cout); 8 | std::cout << "\n"; 9 | myScreen.display(std::cout); 10 | std::cout << "\n"; 11 | } 12 | -------------------------------------------------------------------------------- /08/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.08 2 | 3 | Revise the program from the previous exercise to append its output to its given 4 | file. Run the program on the same output file at least twice to ensure that the 5 | data are preserved. 6 | 7 | ### Solution 8 | 9 | See program files. 10 | -------------------------------------------------------------------------------- /09/29/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.29 2 | 3 | Given that `vec` holds 25 elements, what does `vec.resize(100)` do? What if we 4 | wrote `vec.resize(10)`? 5 | 6 | ### Solution 7 | 8 | `vec.resize(100)` would add 75 value-initialized elements, and `vec.resize(10)` 9 | would remove 15. 10 | -------------------------------------------------------------------------------- /11/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.07 2 | 3 | Define a `map` for which the key is the family's last name and the value is a 4 | `vector` of the children's names. Write code to add new families and to add new 5 | children to an existing family. 6 | 7 | ### Solution 8 | 9 | See `7.cc`. 10 | -------------------------------------------------------------------------------- /01/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.11 2 | 3 | Write a program that prompts the user for two integers. Print each number in the 4 | range specified by those two integers. 5 | 6 | ### Solution 7 | 8 | See `11.cc`. 9 | 10 | NB: Program will fail if given non-ascending integers as input. 11 | -------------------------------------------------------------------------------- /01/22/22.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_item.h" 3 | 4 | int main() 5 | { 6 | Sales_item item, total; 7 | 8 | std::cin >> total; 9 | 10 | while (std::cin >> item) 11 | total += item; 12 | std::cout << total << std::endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /04/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.11 2 | 3 | Write an expression that tests four values, `a`, `b`, `c` and `d` and ensures 4 | that `a` is greater than `b`, which is greater than `c`, which is greater than 5 | `d`. 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | a > b && b > c && c > d 11 | ``` 12 | -------------------------------------------------------------------------------- /07/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.17 2 | 3 | What, if any, are the differences between using `class` or `struct`. 4 | 5 | ### Solution 6 | 7 | The only difference between the two keywords are that a `class` will default to 8 | `private` members, while a `struct` will default to `public` members. 9 | -------------------------------------------------------------------------------- /09/48/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.48 2 | 3 | Given the definitions of `name` and `numbers` on page 365, what does 4 | `numbers.find(name)` return? 5 | 6 | ### Solution 7 | 8 | It should return `std::string::npos` because there is no match in `numbers` for 9 | the `string` `"AnnaBelle"`. 10 | -------------------------------------------------------------------------------- /11/25/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.25 2 | 3 | Contrast the following program with the one in the previous exercise. 4 | 5 | ```cpp 6 | vector v; 7 | v[0] = 1; 8 | ``` 9 | 10 | ### Solution 11 | 12 | This will fail, since there are no elements in `v`, so the subscript is illegal. 13 | -------------------------------------------------------------------------------- /12/22/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.22 2 | 3 | What changes would need to be made to `StrBlobPtr` to create a class that can be 4 | used with a `const StrBlob`? Define a class named `ConstStrBlobPtr` that can 5 | point to a `const StrBlob`. 6 | 7 | ### Solution 8 | 9 | See program files. 10 | -------------------------------------------------------------------------------- /13/57/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.57 2 | 3 | What if we defined `sorted` as: 4 | 5 | ```cpp 6 | Foo Foo::sorted() const & { return Foo(*this).sorted(); } 7 | ``` 8 | 9 | ### Solution 10 | 11 | This would actually call the rvalue version of sorted because `Foo(*this)` is an 12 | rvalue. 13 | -------------------------------------------------------------------------------- /15/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.32 2 | 3 | What happens when an object of type `Query` is copied, moved, assigned or 4 | destroyed? 5 | 6 | ### Solution 7 | 8 | In its current state, the `Query` class has synthesized copy and move 9 | constructors, destructor and assignment operators. 10 | -------------------------------------------------------------------------------- /15/41/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.41 2 | 3 | Reimplement your classes to use built-in pointers to `Query_base` rather than 4 | `shared_ptr`s. Remember that your classes will no longer be able to use the 5 | synthesized copy-control members. 6 | 7 | ### Solution 8 | 9 | See program files. 10 | -------------------------------------------------------------------------------- /01/13/1.11.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int v1, v2; 6 | 7 | std::cout << "Enter two integers in ascending order:" << std::endl; 8 | std::cin >> v1 >> v2; 9 | for (; v1 <= v2; v1++) 10 | std::cout << v1 << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /01/16/16.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int sum = 0, x; 6 | 7 | std::cout << "Enter integers followed by EOF:" << std::endl; 8 | 9 | while (std::cin >> x) 10 | sum += x; 11 | std::cout << "Sum: " << sum << std::endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /02/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.11 2 | 3 | Explain whether each of the following is a declaration or a definition: 4 | 5 | (a) `extern int ix = 1024;` 6 | (b) `int iy;` 7 | (c) `extern int iz;` 8 | 9 | ### Solution 10 | 11 | (a) definition 12 | (b) definition 13 | (c) declaration 14 | -------------------------------------------------------------------------------- /04/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.13 2 | 3 | What are the values of `i` and `d` after each assignment? 4 | 5 | ``` 6 | int i; double d; 7 | ``` 8 | 9 | (a) `d = i = 3.5;` 10 | (b) `i = d = 3.5;` 11 | 12 | ### Solution 13 | 14 | (a) `i` is 3, `d` is 3.0. 15 | (b) `i` is 3, `d` is 3.5. 16 | -------------------------------------------------------------------------------- /09/24/24.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::vector v; 7 | std::cout << v.at(0) << std::endl; 8 | std::cout << v[0] << std::endl; 9 | std::cout << v.front() << std::endl; 10 | std::cout << *(v.begin()) << std::endl; 11 | } 12 | -------------------------------------------------------------------------------- /10/26/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.26 2 | 3 | Explain the differences among the three kinds of insert iterators. 4 | 5 | ### Solution 6 | 7 | The three insert iterators use different insert operations. `back_inserter` uses 8 | `push_back`; `front_inserter` uses `push_front`; `inserter` uses `insert`. 9 | -------------------------------------------------------------------------------- /05/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.02 2 | 3 | What is a block? When might you use a block? 4 | 5 | ### Solution 6 | 7 | A block is a sequence of statements which has its own scope and can be used in 8 | place of a single statement. They are often used for function definitions and 9 | loop bodies. 10 | -------------------------------------------------------------------------------- /06/03/fact.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fact(int val) 4 | { 5 | int ret = 1; 6 | while (val > 1) 7 | ret *= val--; 8 | return ret; 9 | } 10 | 11 | int main(void) 12 | { 13 | int j = fact(5); 14 | std::cout << "5! is " << j << std::endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /08/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.12 2 | 3 | Why didn't we use in-class initializers in `PersonInfo`? 4 | 5 | ### Solution 6 | 7 | Both members of `PersonInfo` can be default-initialized, and the synthesized 8 | default constructor will do just that, so an in-class initializer is not 9 | necessary. 10 | -------------------------------------------------------------------------------- /09/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.08 2 | 3 | What type should be used to read elements in a `list` of `string`s? To write 4 | them? 5 | 6 | ### Solution 7 | 8 | `std::list::const_iterator` is sufficient for reading, but 9 | `std::list::iterator` is necessary for writing. 10 | -------------------------------------------------------------------------------- /11/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.15 2 | 3 | What are the `mapped_type`, `key_type` and `value_type` of a `map` from `int` to 4 | `vector`? 5 | 6 | ### Solution 7 | 8 | The `mapped_type` is a `vector`; the `key_type` is an `int`, and the 9 | `value_type` is a `pair>`. 10 | -------------------------------------------------------------------------------- /14/38/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.38 2 | 3 | Write a class that tests whether the length of a given `string` matches a given 4 | bound. Use that object to write a program to report how many words in an input 5 | file are of sizes 1 through 10 inclusive. 6 | 7 | ### Solution 8 | 9 | See `38.cc`. 10 | -------------------------------------------------------------------------------- /01/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.10 2 | 3 | In addition to the `++` operator that adds `1` to its operand, there is a 4 | decrement operator (`--`) that subtracts `1`. Use the decrement operator to 5 | write a `while` that prints the numbers from ten to zero. 6 | 7 | ### Solution 8 | 9 | See `10.cc`. 10 | -------------------------------------------------------------------------------- /03/06/6.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::cin; 6 | using std::string; 7 | 8 | int main() 9 | { 10 | string s; 11 | cin >> s; 12 | for (auto &c : s) 13 | c = 'X'; 14 | cout << s << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /03/07/7.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::cin; 6 | using std::string; 7 | 8 | int main() 9 | { 10 | string s; 11 | cin >> s; 12 | for (char &c : s) 13 | c = 'X'; 14 | cout << s << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /04/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.05 2 | 3 | Determine the result of the following expressions. 4 | 5 | (a) `-30 * 3 + 21 / 5` 6 | (b) `-30 + 3 * 21 / 5` 7 | (c) `30 / 3 * 21 % 5` 8 | (d) `-30 / 3 * 21 % 4` 9 | 10 | ### Solution 11 | 12 | (a) `-360` 13 | (b) `-18` 14 | (c) `0` 15 | (d) `-2` 16 | -------------------------------------------------------------------------------- /07/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.14 2 | 3 | Write a version of the default constructor that explicitly initializes the 4 | members to the values we have provided as in-class initializers. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | Sales_data(): 10 | bookNo(""), units_sold(0), revenue(0) { } 11 | ``` 12 | -------------------------------------------------------------------------------- /11/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.23 2 | 3 | Rewrite the `map` that stored `vector`s of children's names with a key that is 4 | the family last name for the exercises in § 11.2.1 to use a `multimap`. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | std::multimap family; 10 | ``` 11 | -------------------------------------------------------------------------------- /12/25/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.25 2 | 3 | Given the following `new` expression, how would you `delete pa`? 4 | 5 | ```cpp 6 | int *pa = new int[10]; 7 | ``` 8 | 9 | ### Solution 10 | 11 | Since `pa` is an array, you must use the `delete[]` operator: 12 | 13 | ```cpp 14 | delete [] pa; 15 | ``` 16 | -------------------------------------------------------------------------------- /01/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.01 2 | 3 | Review the documentation for your compiler and determine what file naming 4 | convention it uses. Compile and run the `main` program from page 2. 5 | 6 | ### Solution 7 | 8 | The `g++` compiler produces an output file `a.out` upon compilation. See 9 | `main.cc`. 10 | -------------------------------------------------------------------------------- /01/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.19 2 | 3 | Revise the program you wrote for the exercises in § 1.4.1 (p. 13) that 4 | printed a range of numbers so that it handles input in which the first number is 5 | smaller than the second. 6 | 7 | ### Solution 8 | 9 | See `19.cc` for the rewrite of exercise 1.11. 10 | -------------------------------------------------------------------------------- /02/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.02 2 | 3 | To calculate a mortgage payment, what types would you use for the rate, 4 | principal and payment? Explain why you selected each type. 5 | 6 | ### Solution 7 | 8 | I would use `double` for all three variables to accurately calculate the payment 9 | to 10 sigfigs. 10 | -------------------------------------------------------------------------------- /03/05/5.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | 8 | int main() 9 | { 10 | string word, total; 11 | while (cin >> word) 12 | total += word; 13 | cout << total << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /06/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.07 2 | 3 | Write a function that returns 0 when it is first called and then generates 4 | numbers in sequence each time it is called again. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | int callNo(void) 10 | { 11 | static int call = 0; 12 | return call++; 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /09/15/15.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::vector v1 = {0, 1, 2, 3, 4, 5}; 7 | std::vector v2 = {0, 2, 4, 6, 8,10}; 8 | 9 | std::cout << ((v1 == v2) ? "Equal!" : ((v1 < v2) ? "v1 < v2" : "v1 > v2")); 10 | std::cout << std::endl; 11 | } 12 | -------------------------------------------------------------------------------- /15/26/26.cc: -------------------------------------------------------------------------------- 1 | #include "debug_quote.h" 2 | #include 3 | 4 | int main() 5 | { 6 | Bulk_quote *b1 = new Bulk_quote; 7 | delete b1; 8 | Quote *q1 = new Quote; 9 | delete q1; 10 | 11 | Bulk_quote b2, b3; 12 | b3 = b2; 13 | b2 = std::move(b3); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /03/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.16 2 | 3 | Write a program to print the size and contents of the `vector`s from exercise 4 | 3.13. Check whether your answers to that exercise were correct. If not, restudy 5 | § 3.3.1 (p. 97) until you understand why you were wrong. 6 | 7 | ### Solution 8 | 9 | See `16.cc`. 10 | -------------------------------------------------------------------------------- /03/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.17 2 | 3 | Read a sequence of words from `cin` and store the values in a `vector`. After 4 | you've read all the words, process the `vector` and change each word to 5 | uppercase. Print the transformed elements, eight words to a line. 6 | 7 | ### Solution 8 | 9 | See `17.cc`. 10 | -------------------------------------------------------------------------------- /08/02/2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | std::istream &inOut(std::istream &is) 5 | { 6 | std::string line; 7 | while (is >> line) 8 | std::cout << line << std::endl; 9 | is.clear(); 10 | return is; 11 | } 12 | 13 | int main() 14 | { 15 | inOut(std::cin); 16 | } 17 | -------------------------------------------------------------------------------- /09/40/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.40 2 | 3 | If the program in the previous exercise reads 256 words, what is its likely 4 | `capacity` after it is `resize`d? What if it reads 512? 1,000? 1,048? 5 | 6 | ### Solution 7 | 8 | 256: 1024. 9 | 512: 1024. 10 | 1,000: 2048 (probably). 11 | 1,048: 2048 (probably). 12 | -------------------------------------------------------------------------------- /09/41/41.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::vector v{'H', 'e', 'l', 'l', 'o', ',', ' ', 8 | 'w', 'o', 'r', 'l', 'd', '\0'}; 9 | std::string s(v.begin(), v.end()); 10 | std::cout << s << std::endl; 11 | } 12 | -------------------------------------------------------------------------------- /10/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.04 2 | 3 | Assuming `v` is a `vector`, what, if anything, is wrong with calling 4 | `accumulate(v.cbegin(), v.cend(), 0)`? 5 | 6 | ### Solution 7 | 8 | The function call will truncate the `double`s in the `vector` to `int`s becaause 9 | the accumulator is an integer. 10 | -------------------------------------------------------------------------------- /14/25/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.25 2 | 3 | Implement any other assignment operators your class should define. Explain which 4 | types should be used as operands and why. 5 | 6 | ### Solution 7 | 8 | No other assignment operators are required, because their construction would be 9 | illogical or unneccessary. 10 | -------------------------------------------------------------------------------- /15/33/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.33 2 | 3 | What about objects of type `Query_base`? 4 | 5 | ### Solution 6 | 7 | `Query_base` is a completely abstract base class that has NO publicly-accessible 8 | data members. Instead, we must rely on pointers or references to `Query_base` 9 | objects to use its members. 10 | -------------------------------------------------------------------------------- /01/04/4.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int v1 = 0, v2 = 0; 6 | 7 | std::cout << "Enter two numbers:" << std::endl; 8 | std::cin >> v1 >> v2; 9 | std::cout << "The product of " << v1 << " and " << v2 10 | << " is " << v1 * v2 << std::endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /03/01/1.09.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | int sum = 0, count = 50; 8 | 9 | while (count <= 100) 10 | { 11 | sum += count; 12 | count++; 13 | } 14 | cout << "Sum of [50, 100]: " << sum << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /03/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.01 2 | 3 | Rewrite the exercises from § 1.4.1 (p. 13) and § 2.6.2 (p. 76) with 4 | appropriate `using` declarations. 5 | 6 | ### Solution 7 | 8 | See program files for rewrites of exercises 1.09 through 1.11 as well as 9 | exercises 1.20 through 1.25 (from exercise 2.42). 10 | -------------------------------------------------------------------------------- /03/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.09 2 | 3 | What does the following program do? Is it valid? If not, why not? 4 | 5 | ```cpp 6 | string s; 7 | cout << s[0] << endl; 8 | ``` 9 | 10 | ### Solution 11 | 12 | The program fragment is not valid because the subscript of the string is out of 13 | bounds, which is undefined. 14 | -------------------------------------------------------------------------------- /05/23/23.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cin; 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | int x, y; 9 | cout << "Enter two integers: " << endl; 10 | cin >> x; 11 | cin >> y; 12 | cout << x << " / " << y << " = " << (double) x/y << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /05/25/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.25 2 | 3 | Revise your program from the previous exercise to use a `try` block to `catch` 4 | the exception. The `catch` clause should print a message to the user and ask 5 | them to supply a ew number and repeate the code inside the `try`. 6 | 7 | ### Solution 8 | 9 | See `25.cc`. 10 | -------------------------------------------------------------------------------- /06/34/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.34 2 | 3 | What would happen if the stopping condition in `factorial` were 4 | ```cpp 5 | if (val != 0) 6 | ``` 7 | 8 | ### Solution 9 | 10 | If this were the case, then a negative integer passed to `factorial` would 11 | result in an infinite loop rather than immediately returning 1. 12 | -------------------------------------------------------------------------------- /06/54/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.54 2 | 3 | Write a declaration for a function that takes two `int` parameters and returns 4 | an `int`, and declare a `vector` whose elements have this function pointer type. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | int f(int, int); 10 | std::vector vec; 11 | ``` 12 | -------------------------------------------------------------------------------- /07/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.05 2 | 3 | Provide operations in your `Person` class to return the name and address. Should 4 | these functions be `const`? Explain your choice. 5 | 6 | ### Solution 7 | 8 | See `person.cc`. The functions should be `const` because they do not affect the 9 | members of the `Person`. 10 | -------------------------------------------------------------------------------- /09/35/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.35 2 | 3 | Explain the difference between a `vector`'s `capacity` and its `size`. 4 | 5 | ### Solution 6 | 7 | A `vector`'s `capacity` is the max amount of elements until the `vector` 8 | requires reallocation, while its `size` is the current amount of elements in the 9 | `vector`. 10 | -------------------------------------------------------------------------------- /13/48/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.48 2 | 3 | Define a `vector` and call `push_back` several times on that `vector`. 4 | Run your program and see how often `String`s are copied. 5 | 6 | ### Solution 7 | 8 | See program files. For four `push_back`s, the `String` copy constructor was 9 | called twelve times. 10 | -------------------------------------------------------------------------------- /03/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.05 2 | 3 | Write a program to read `string`s from the standard input, concatenating what is 4 | read into one large `string`. Print the concatenated `string`. Next, change the 5 | program to separate adjacent input `string`s by a space. 6 | 7 | ### Solution 8 | 9 | See `5.cc` and `5b.cc`. 10 | -------------------------------------------------------------------------------- /03/31/31.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | constexpr size_t arr_size = 10; 8 | int arr[arr_size]; 9 | for (size_t i = 0; i < arr_size; i++) 10 | { 11 | arr[i] = i; 12 | cout << arr[i] << endl; 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /03/40/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.40 2 | 3 | Write a program to define two character arrays initialized from string literals. 4 | Now define a third character array to hold the concatenation of the two arrays. 5 | Use `strcpy` and `strcat` to copy the two arrays into the third. 6 | 7 | ### Solution 8 | 9 | See `40.cc`. 10 | -------------------------------------------------------------------------------- /09/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.23 2 | 3 | In the first program in this section on page 346, what would the values of 4 | `val`, `val2`, `val3` and `val4` be if `c.size()` is `1`? 5 | 6 | ### Solution 7 | 8 | All would be equal to the first element of `c` (given that `val3` and `val4` 9 | could access the final element). 10 | -------------------------------------------------------------------------------- /09/47/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.47 2 | 3 | Write a program that finds each numeric character and then each alphabetic 4 | character in the `string` `"ab2c3d7R4E6"`. Write two versions of the program. 5 | The first should use `find_first_of` and the second `find_first_not_of`, 6 | 7 | ### Solution 8 | 9 | See `47.cc`. 10 | -------------------------------------------------------------------------------- /09/50/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.50 2 | 3 | Write a program to process a `vector` whose elements represent integral 4 | values. Produce the sum of all the elements in that `vector`. Change the program 5 | so that it sums the `string`s that represent floating-point values. 6 | 7 | ### Solution 8 | 9 | See `50.cc`. 10 | -------------------------------------------------------------------------------- /14/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.15 2 | 3 | Should the class you chose for exercise 7.40 from § 7.5.1 define any of the 4 | arithmetic operators? If so implement them. If not, explain why not. 5 | 6 | ### Solution 7 | 8 | No. It is not meaningful to add two `Books` together, or do any other arithmetic 9 | operation. 10 | -------------------------------------------------------------------------------- /02/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.13 2 | 3 | What is the value of `j` in the following program? 4 | 5 | ```cpp 6 | int i = 42; 7 | int main() 8 | { 9 | int i = 100; 10 | int j = i; 11 | } 12 | ``` 13 | 14 | ### Solution 15 | 16 | `j` is equal to `100` because the local variable `i` hides the global variable 17 | `i`. 18 | -------------------------------------------------------------------------------- /03/01/1.11.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::cin; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | int v1, v2; 9 | 10 | cout << "Enter two integers in ascending order:" << endl; 11 | cin >> v1 >> v2; 12 | while (v1 <= v2) 13 | cout << v1++ << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /04/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.02 2 | 3 | Using table 4.12 (p. 166), parenthesize the following expressions to indicate 4 | the order in which the operands are grouped: 5 | 6 | (a) `* vec.begin()` 7 | (b) `* vec.begin() + 1` 8 | 9 | ### Solution 10 | 11 | (a) `(* ((vec.begin)()))` 12 | (b) `((* ((vec.begin)())) + 1)` 13 | -------------------------------------------------------------------------------- /07/44/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.44 2 | 3 | Is the following declaration legal? If not, why not? 4 | 5 | ```cpp 6 | vector vec(10); 7 | ``` 8 | 9 | ### Solution 10 | 11 | The declaration is not legal, as the `NoDefault`s are default-initialized here, 12 | and there is no default constructor defined for `NoDefault`. 13 | -------------------------------------------------------------------------------- /09/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.03 2 | 3 | What are the constraints on the iterators that form iterator ranges? 4 | 5 | ### Solution 6 | 7 | Iterators must be pointing to the elements within the range `[begin, end)`. 8 | That is, between `begin` inclusive and `end` exclusive. They also must point to 9 | the same container. 10 | -------------------------------------------------------------------------------- /09/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.09 2 | 3 | What is the difference between the `begin` and `cbegin` functions? 4 | 5 | ### Solution 6 | 7 | `cbegin` will always return a `const_iterator` type, while `begin` will only 8 | return a `const_iterator` if the container is `const`, otherwise it will return 9 | a regular `iterator`. 10 | -------------------------------------------------------------------------------- /11/31/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.31 2 | 3 | Write a program that defines a `multimap` of authors and their works. Use `find` 4 | to find an element in the `multimap` and `erase` that element. Be sure your 5 | program works correctly if the element you look for is not in the `map`. 6 | 7 | ### Solution 8 | 9 | See `31.cc`. 10 | -------------------------------------------------------------------------------- /11/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.32 2 | 3 | Using the `multimap` from the previous exercise, write a program to print the 4 | list of authors and their works alphabetically. 5 | 6 | ### Solution 7 | 8 | My solution for Exercise 11.31 already achieves this in the `for` loop at the 9 | end of the program. See Exercise 11.31. 10 | -------------------------------------------------------------------------------- /13/31/31.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "HasPtr.h" 5 | 6 | using std::swap; 7 | 8 | int main() 9 | { 10 | HasPtr a("Hello"), b("World!"), c("Goodbye"); 11 | std::vector hps{a,b,c}; 12 | 13 | std::sort(hps.begin(), hps.end()); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /07/28/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.28 2 | 3 | What would happen in the previous exercise if the return type of `move`, `set` 4 | and `display` was `Screen` rather than `Screen&`? 5 | 6 | ### Solution 7 | 8 | The program would be forced to create a new `Screen` on every action, and the 9 | original `myScreen` would not be updated. 10 | -------------------------------------------------------------------------------- /07/29/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.29 2 | 3 | Revise your `Screen` class so that `move`, `set` and `display` functions return 4 | `Screen` and check your prediction from the previous exercise. 5 | 6 | ### Solution 7 | 8 | See program files. Prediction was correct; call to `myScreen` did not show 9 | changes from previous updates. 10 | -------------------------------------------------------------------------------- /07/38/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.38 2 | 3 | We might want to supply `cin` as a default argument to the constructor that 4 | takes an `istream&`. Write the constructor declaration that uses `cin` as a 5 | default argument. 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | Sales_data(std::istream &is = std::cin) { read(is, *this); } 11 | ``` 12 | -------------------------------------------------------------------------------- /10/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.15 2 | 3 | Write a lambda that captures an `int` from its enclosing function and takes an 4 | `int` parameter. The lambda should return the sum of the captured `int` and the 5 | `int` parameter. 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | int i = 100; 11 | [i](const int &j) { return i + j; } 12 | ``` 13 | -------------------------------------------------------------------------------- /10/34/34.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::vector v{1,2,3,4,5,6,7,8,9}; 8 | std::for_each(v.crbegin(), v.crend(), [](const int &i) 9 | { std::cout << i << " "; }); 10 | std::cout << std::endl; 11 | } 12 | -------------------------------------------------------------------------------- /10/42/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.42 2 | 3 | Reimplement the program that eliminated duplicate words that we wrote in § 4 | 10.2.3 to use a `list` instead of a `vector`. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | void elimDups(std::list &words) 10 | { 11 | words.sort(); 12 | words.unique(); 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /03/20/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.20 2 | 3 | Read a set of integers into a `vector`. Print the sum of each pair of adjacent 4 | elements. Change your program so that it prints the sum of the first and last 5 | elements, followed by the sum of the second and second-to-last, and so on. 6 | 7 | ### Solution 8 | 9 | See `20.cc` and `20b.cc`. 10 | -------------------------------------------------------------------------------- /03/44/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.44 2 | 3 | Rewrite the programs from the previous exercise using a type alias for the type 4 | of the loop control variables. 5 | 6 | ### Solution 7 | 8 | See `44.cc` and `44c.cc` for rewrites of exercise 3.43. Note that `43b.cc` does 9 | not need a rewrite, because the loop control variables are `int`s. 10 | -------------------------------------------------------------------------------- /04/33/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.33 2 | 3 | Using Table 4.12 (p. 166) explain what the following expression does: 4 | 5 | ```cpp 6 | someValue ? ++x, ++y : --x, --y; 7 | ``` 8 | 9 | ### Solution 10 | 11 | If the boolean-cast value of `someValue` is `true`, then `x` and `y` will be 12 | incremented; otherwise, they will be decremented. 13 | -------------------------------------------------------------------------------- /10/35/35.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::vector v{1,2,3,4,5,6,7,8,9}; 9 | auto it = std::prev(v.cend()); 10 | while (it != v.cbegin()) 11 | std::cout << *it-- << " "; 12 | std::cout << *it << std::endl; 13 | } 14 | -------------------------------------------------------------------------------- /11/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.16 2 | 3 | Using a `map` iterator write an expression that assigns a value to an element. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | std::map dictionary{{"apple", ""}}; 9 | auto map_it = dictionary.begin(); 10 | map_it->second = "(n) A red fruit grown from apple trees"; 11 | ``` 12 | -------------------------------------------------------------------------------- /12/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.23 2 | 3 | Write a program to concatenate two string literals, putting the result in a 4 | dynamically-allocated array of `char`. Write a program to concatenate two 5 | library `string`s that have the same value as the literals used in the first 6 | program. 7 | 8 | ### Solution 9 | 10 | See `23.cc`. 11 | -------------------------------------------------------------------------------- /13/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.02 2 | 3 | Explain why the following declaration is illegal: 4 | 5 | ```cpp 6 | Sales_data::Sales_data(Sales_data rhs) 7 | ``` 8 | 9 | ### Solution 10 | 11 | This incorrectly attempts to make a copy constructor that will loop and call 12 | itself indefinitely. The `rhs` argument must be a reference. 13 | -------------------------------------------------------------------------------- /01/15/15.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main ( { 4 | 5 | int v1 = 0, v2 = 0; 6 | std::cout << "Read each file." << std::endl: 7 | std::cout << Update master. << std::endl; 8 | std::cout << "Write new master." std::endl; 9 | 10 | std::cin >> v >> v2; 11 | cout << v1 + v2 << std::endl; 12 | return 0 13 | } 14 | -------------------------------------------------------------------------------- /03/08/8b.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::cin; 6 | using std::string; 7 | 8 | int main() 9 | { 10 | string s; 11 | cin >> s; 12 | for (decltype (s.size()) i = 0; i < s.size(); i++) 13 | s[i] = 'X'; 14 | cout << s << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /03/40/40.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include // strcat 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | const char *s1 = "Hello, "; 9 | const char *s2 = "world."; 10 | char s3[strlen(s1) + strlen(s2) + 1]; 11 | strcat(strcpy(s3, s1), s2); 12 | cout << s3 << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /04/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.07 2 | 3 | What does overflow mean? Show three expressions that will overflow. 4 | 5 | ### Solution 6 | 7 | Overflow is when a variable's value exceeds the maximum or minimum bounds of its 8 | type. 9 | 10 | ```cpp 11 | int i = INT_MAX + 1; 12 | char c = CHAR_MIN - 1; 13 | unsigned int ui = -1; 14 | ``` 15 | -------------------------------------------------------------------------------- /06/25/25.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char **argv) 5 | { 6 | if (argc != 3) { 7 | std::cout << "Usage: " << argv[0] << " STR1 STR2" << std::endl; 8 | return EXIT_FAILURE; 9 | } 10 | 11 | std::cout << argv[1] << argv[2] << std::endl; 12 | return EXIT_SUCCESS; 13 | } 14 | -------------------------------------------------------------------------------- /07/34/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.34 2 | 3 | What would happen if we put the `typedef` of `pos` in the `Screen` class on page 4 | 285 as the last line in the class? 5 | 6 | ### Solution 7 | 8 | Because the `pos` type is not defined for the definitions required in 9 | `dummy_fcn`, `cursor`, `height` and `width`, the program would be in error. 10 | -------------------------------------------------------------------------------- /11/28/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.28 2 | 3 | Define an initialize a variable to hold the result of calling `find` on ` map` 4 | from `string` to `vector` if `int`. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | std::map m; 10 | 11 | std::map>::iterator it = m.find("username"); 12 | ``` 13 | -------------------------------------------------------------------------------- /14/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.19 2 | 3 | Should the class you chose for exercise 7.40 from § 7.5.1 define the 4 | relational operators? If so, implement them. If not, explain why not. 5 | 6 | ### Solution 7 | 8 | Normally, yes. A book is normally ordered by its Dewey Decimal System number, 9 | then by author and title. See `book.h`. 10 | -------------------------------------------------------------------------------- /14/22/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.22 2 | 3 | Define a version of the assignment operator that can assign a `string` 4 | representing an `ISBN` to a `Sales_data`. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | Sales_data &Sales_data::operator=(const std::string &num) 10 | { 11 | *this = Sales_Data(num); 12 | return *this; 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /15/37/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.37 2 | 3 | What changes would your classes need if the derived classes had members of type 4 | `shared_ptr` rather than that of type `Query`? 5 | 6 | ### Solution 7 | 8 | All the constructors and member `q` types would have to change to a shared 9 | pointer, but would otherwise stay the same. 10 | -------------------------------------------------------------------------------- /03/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.04 2 | 3 | Write a program to read two `string`s and report whether the `string`s are 4 | equal. If not, report which of the two is larger. Now, change the program to 5 | report whether the `string`s have the same length, and if not, report which is 6 | longer. 7 | 8 | ### Solution 9 | 10 | See `4.cc` and `4b.cc`. 11 | -------------------------------------------------------------------------------- /03/08/8.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::cin; 6 | using std::string; 7 | 8 | int main() 9 | { 10 | string s; 11 | cin >> s; 12 | decltype (s.size()) i = 0; 13 | while (i < s.size()) 14 | s[i++] = 'X'; 15 | cout << s << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /03/26/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.26 2 | 3 | In the binary search program on page 112, why did we write `mid = beg + (end - 4 | beg) / 2;` instead of `mid = (beg + end) / 2;`? 5 | 6 | ### Solution 7 | 8 | The `end()` property of an iterator points to the item placed one item 9 | after the end of the container's contents, so it would be off by one. 10 | -------------------------------------------------------------------------------- /06/46/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.46 2 | 3 | Would it be possible to define `isShorter as a `constexpr`? If so, do so. If 4 | not, explain why not. 5 | 6 | ### Solution 7 | 8 | Since the `std::string::size()` member is not a constant expression as well as 9 | `s1.size() == s2.size()`, `isShorter` cannot be defined as a `constexpr` 10 | function. 11 | -------------------------------------------------------------------------------- /07/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.08 2 | 3 | Why does `read` defined its `Sales_data` parameter as a plain reference and 4 | `print` define its parameter as a reference to `const`? 5 | 6 | ### Solution 7 | 8 | Because `read` modifies the `Sales_data` given to its function. `print` does not 9 | do any modifications and therefore can be `const`. 10 | -------------------------------------------------------------------------------- /09/33/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.33 2 | 3 | In the final example in this section what would happen if we did not assign the 4 | result of `insert` to `begin`? Write a program that omits this assignment to see 5 | if your expectation was correct. 6 | 7 | ### Solution 8 | 9 | Since the iterator would be invalid, the program would fail. See `33.cc`. 10 | -------------------------------------------------------------------------------- /11/34/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.34 2 | 3 | What would happen if we used the subscript operator instead of `find` in the 4 | `transform` function? 5 | 6 | ### Solution 7 | 8 | The subscript operator would unconditionally add an element like `{"example", 9 | ""}`, leading to the entire program substituting every word with the empty 10 | string. 11 | -------------------------------------------------------------------------------- /12/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.21 2 | 3 | We could have written `StrBlobPtr`'s `deref` member as follows: 4 | 5 | ```cpp 6 | std::string& deref() const 7 | { return (*check(curr, "dereference past end"))[curr]; } 8 | ``` 9 | 10 | ### Solution 11 | 12 | The original two-line solution is clearer, although this solution is more 13 | consise. 14 | -------------------------------------------------------------------------------- /01/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.12 2 | 3 | What does the following `for` loop do? What is the final value of `sum`? 4 | 5 | ```cpp 6 | int sum = 0; 7 | for (int i = -100; i <= 100; ++i) 8 | sum += i; 9 | ``` 10 | 11 | ### Solution 12 | 13 | The loop adds all integers in the range [-100, 100] to `sum`; the final value of 14 | `sum` will be zero. 15 | -------------------------------------------------------------------------------- /03/14/14.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cin; 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | 8 | int main() 9 | { 10 | vector seq; 11 | int cur; 12 | while (cin >> cur) 13 | seq.push_back(cur); 14 | for (int i : seq) 15 | cout << i << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /03/45/45.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | int ia[3][4] = {{0, 1, 2, 3}, 8 | {4, 5, 6, 7}, 9 | {8, 9, 10, 11}}; 10 | 11 | for (auto &row : ia) 12 | for (auto i : row) 13 | cout << i << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /04/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.18 2 | 3 | What would happen if the `while` loop on page 148 that prints the elements from 4 | a `vector` used the prefix increment operator? 5 | 6 | ### Solution 7 | 8 | The first element of the `vector` would be skipped, and the program would 9 | attempt to print the value of one element past the end of the `vector`. 10 | -------------------------------------------------------------------------------- /04/21/21.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::vector; 6 | 7 | int main() 8 | { 9 | vector vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 10 | for (int i : vec) 11 | { 12 | i = i % 2 == 0 ? i : 2 * i; 13 | cout << i << endl; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /04/27/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.27 2 | 3 | What is the result of each of these expressions? 4 | 5 | ```cpp 6 | unsigned long ul1 = 3, ul2 = 7; 7 | ``` 8 | 9 | (a) `ul1 & ul2` 10 | (b) `ul1 | ul2` 11 | (c) `ul1 && ul2` 12 | (d) `ul1 || ul2` 13 | 14 | ### Solution 15 | 16 | (a) `3` 17 | (b) `7` 18 | (c) `true` (`1`) 19 | (d) `true` (`1`) 20 | -------------------------------------------------------------------------------- /06/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.1 2 | 3 | What is the difference between a parameter and an argument? 4 | 5 | ### Solution 6 | 7 | An argument initializes a function's parameter. That is, when a function that 8 | requires an argument is called, like `f(x)`, the argument `x` is used to 9 | initialize `count` in `f`'s declaration `int f(int count)`. 10 | -------------------------------------------------------------------------------- /08/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.14 2 | 3 | Why did we declare `entry` and `nums` as `const auto &`? 4 | 5 | ### Solution 6 | 7 | It is not necessary to copy each person or number into a new object, and using a 8 | reference to `const` uses less resources which could be intensive in the case of 9 | a person with a million different telephone numbers. 10 | -------------------------------------------------------------------------------- /13/16/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.16 2 | 3 | What if the parameter in `f` were `const numbered &`? Does that change the 4 | output? If so, why? What output gets generated? 5 | 6 | ### Solution 7 | 8 | Yes. Since now there is no copy operation happening to `f`, the three serial 9 | numbers produced will be the true serial numbers of `a`, `b` and `c`. 10 | -------------------------------------------------------------------------------- /13/50/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.50 2 | 3 | Put print statements in the move operations in your `String` class and rerun the 4 | program from exercise 13.48 in § 13.6.1 that used a `vector` to see 5 | when copies are avoided. 6 | 7 | ### Solution 8 | 9 | See program files. All copies made were avoided and replaced by move operations. 10 | -------------------------------------------------------------------------------- /14/45/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.45 2 | 3 | Write conversion operators to convert a `Sales_data` to `string` and to 4 | `double`. What values do you think these operators should return? 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | explicit operator std::string() const { return bookNo; } 10 | explicit operator double const { return avg_price(); } 11 | ``` 12 | -------------------------------------------------------------------------------- /15/36/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.36 2 | 3 | Put print statements in the constructors and `rep` members and run your code to 4 | check your answers to `(a)` and `(b)` from the first exercise. 5 | 6 | ### Solution 7 | 8 | See program files. 9 | 10 | My solutions were all correct except for the copy operations in (a), which are 11 | not constructed. 12 | -------------------------------------------------------------------------------- /02/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.18 2 | 3 | Write code to change the value of a pointer. Write code to change the value to 4 | which the pointer points. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | int *p = nullptr; 10 | int i = 100; 11 | p = &i; // p's value is changed to the address of i 12 | *p = 42; // i's value is changed to 42 13 | ``` 14 | -------------------------------------------------------------------------------- /02/41/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.41 2 | 3 | Use your `Sales_data` class to rewrite the exercises in § 1.5.1 (p. 22), 4 | § 1.5.2 (p. 24) and § 1.6 (p. 25). For now, you should define your 5 | `Sales_data` class in the same file as your `main` function. 6 | 7 | ### Solution 8 | 9 | See program files for reworks of Exercises 1.20 through 1.25. 10 | -------------------------------------------------------------------------------- /03/43/43.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | int ia[3][4] = {{0, 1, 2, 3}, 8 | {4, 5, 6, 7}, 9 | {8, 9, 10, 11}}; 10 | 11 | for (const int(&row)[4] : ia) 12 | for (int i : row) 13 | cout << i << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /04/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.17 2 | 3 | Explain the difference between prefix and postfix increment. 4 | 5 | ### Solution 6 | 7 | The prefix increment operator increments its operand and returns that value, 8 | while the postfix increment operator returns its unmodified value to be 9 | incremented definitely after execution of the complete statement. 10 | -------------------------------------------------------------------------------- /06/26/26.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char **argv) 4 | { 5 | std::cout << argc-1 << " additional argument" 6 | << (argc-1 != 1 ? "s were " : " was ") << "supplied." << std::endl; 7 | for (int i = 0; i < argc; ++i) 8 | std::cout << "argv[" << i << "]: " << argv[i] << std::endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /07/57/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.57 2 | 3 | Write your own version of the `Account` class. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | class Account { 9 | public: 10 | static double rate() { return interestRate; } 11 | static void rate(double); 12 | private: 13 | static constexpr int period = 30; 14 | double daily_tbl[period]; 15 | }; 16 | ``` 17 | -------------------------------------------------------------------------------- /09/18/18.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::string word; 8 | std::deque words; 9 | 10 | while (std::cin >> word) 11 | words.push_back(word); 12 | for (auto it = words.cbegin(); it != words.cend(); it++) 13 | std::cout << *it << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /09/19/19.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::string word; 8 | std::list words; 9 | 10 | while (std::cin >> word) 11 | words.push_back(word); 12 | for (auto it = words.cbegin(); it != words.cend(); it++) 13 | std::cout << *it << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /09/24/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.24 2 | 3 | Write a program that fetches the first element in a `vector` using `at`, the 4 | subscript operator, `front` and `begin`. Test your program on an empty `vector`. 5 | 6 | ### Solution 7 | 8 | See `24.cc`. Using `at`, the program failed with an uncaught `out_of_range` 9 | exception. All other attempts segfaulted. 10 | -------------------------------------------------------------------------------- /13/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercie 13.10 2 | 3 | What happens when a `StrBlob` object is destroyed? What about a `StrBlobPtr`? 4 | 5 | ### Solution 6 | 7 | When a `StrBlob` is destroyed, each element is deleted, but not the underlying 8 | `vector`. Similarly, when a `StrBlobPtr` is destroyed, each element is 9 | destroyed, but the pointers are not `delete`d. 10 | -------------------------------------------------------------------------------- /13/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.32 2 | 3 | Would the pointerlike version of `HasPtr` benefit from defining a `swap` 4 | function? If so, what is the benefit? If not, why not? 5 | 6 | ### Solution 7 | 8 | A pointerlike `HasPtr` class would have no benefit from a `swap` function, since 9 | all data types are built-in and would just use the `std::swap`. 10 | -------------------------------------------------------------------------------- /13/54/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.54 2 | 3 | What would happen if we defined a `HasPtr` move-assignment operator but did not 4 | change the copy-and-swap operator? Write code to test your answer. 5 | 6 | ### Solution 7 | 8 | See program files. Calling the assignment operator would have two candidate 9 | functions, fail to compile due to the ambiguous call. 10 | -------------------------------------------------------------------------------- /03/05/5b.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::cin; 5 | using std::endl; 6 | using std::string; 7 | 8 | // TODO: NB: prints trailing space. 9 | 10 | int main() 11 | { 12 | string word, total; 13 | while (cin >> word) 14 | total += word + " "; 15 | cout << total << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /04/22/22.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cin; 3 | using std::cout; 4 | using std::endl; 5 | 6 | int main() 7 | { 8 | int i; 9 | while (cin >> i) 10 | cout << (i > 90 ? "high pass" 11 | : (i > 75 ? "pass" 12 | : (i >= 60 ? "low pass" : "fail"))) << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /01/02/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.02 2 | 3 | Change the program to return `-1`. A return value of `-1` is often treated as an 4 | indicator that the program failed. Recompile and rerun your program to see how 5 | your system treats a failure indicator from `main`. 6 | 7 | ### Solution 8 | 9 | See `main.cc`. The status returned by `main` given by `echo $?` is 255. 10 | -------------------------------------------------------------------------------- /02/39/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.39 2 | 3 | Compile the following program to see what happens when you forget the semicolon 4 | after a class definition. Remember the message for future reference. 5 | 6 | ```cpp 7 | struct Foo { /* empty */ } // Note: no semicolon 8 | int main() 9 | { 10 | return 0; 11 | } 12 | ``` 13 | 14 | ### Solution 15 | 16 | See `39.cc`. 17 | -------------------------------------------------------------------------------- /03/10/10.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::cin; 6 | using std::endl; 7 | using std::string; 8 | 9 | int main() 10 | { 11 | string s; 12 | getline(cin, s); 13 | for (auto &c : s) 14 | if (!ispunct(c)) 15 | cout << c; 16 | cout << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /03/32/32b.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::vector; 6 | 7 | int main() 8 | { 9 | vector v1, v2; 10 | for (int i = 0; i < 10; i++) 11 | { 12 | v1.push_back(i); 13 | v2.push_back(v1[i]); 14 | cout << v2[i] << endl; 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /09/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.14 2 | 3 | Write a program to assign the elements from a `list` of `char*` pointers to 4 | C-style character strings to a `vector` of `string`s. 5 | 6 | ### Solution 7 | 8 | See `14.cc`. Note that although this program compiles, my compiler warns that 9 | ISO C++11 does not allow conversion from string literals to `const char *`. 10 | -------------------------------------------------------------------------------- /10/01/1.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::vector v; 8 | int i; 9 | 10 | while (std::cin >> i) 11 | v.push_back(i); 12 | std::cout << "There are " << count(v.cbegin(), v.cend(), 42) 13 | << " integers in the sequence with value 42." << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /12/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.13 2 | 3 | What happens if we execute the following code? 4 | 5 | ```cpp 6 | auto sp = make_shared(); 7 | auto p = sp.get(); 8 | delete p; 9 | ``` 10 | 11 | ### Solution 12 | 13 | `sp` will become a dangling pointer after `delete p`, and will be in error after 14 | the scope ends and the `shared_ptr` attempts to `delete sp`. 15 | -------------------------------------------------------------------------------- /13/33/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.33 2 | 3 | Why is the parameter to the `save` and `remove` members of a `Message` a 4 | `Folder&`? Why didn't we define that parameter as `Folder`? Or `const Folder&`? 5 | 6 | ### Solution 7 | 8 | This way the `Folder` object does not need to be copied over, and we are editing 9 | the `Folder`, so we cannot use a `const Folder&`. 10 | -------------------------------------------------------------------------------- /01/20/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.20 2 | 3 | contains a copy of `Sales_item.h` in 4 | the Chapter 1 code directory. Copy that file to your working directory. Use it 5 | to write a program that reads a set of book sales transactions, writing each 6 | transaction to the standard output. 7 | 8 | ### Solution 9 | 10 | See `20.cc`. 11 | -------------------------------------------------------------------------------- /03/43/43b.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | int ia[3][4] = {{0, 1, 2, 3}, 8 | {4, 5, 6, 7}, 9 | {8, 9, 10, 11}}; 10 | 11 | for (int i = 0; i < 3; i++) 12 | for (int j = 0; j < 4; j++) 13 | cout << ia[i][j] << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /13/48/48.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "String.h" 4 | 5 | int main() 6 | { 7 | std::vector ss; 8 | ss.push_back(String()); 9 | ss.push_back(String("Hello, world")); 10 | ss.push_back(String("test")); 11 | ss.push_back(String("another")); 12 | ss.push_back(String("final message")); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /13/50/50.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "string.h" 4 | 5 | int main() 6 | { 7 | std::vector ss; 8 | ss.push_back(String()); 9 | ss.push_back(String("Hello, world")); 10 | ss.push_back(String("test")); 11 | ss.push_back(String("another")); 12 | ss.push_back(String("final message")); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /02/22/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.22 2 | 3 | Assuming `p` is a pointer to `int`, explain the following code: 4 | 5 | ```cpp 6 | if (p) // ... 7 | if (*p) // ... 8 | ``` 9 | 10 | ### Solution 11 | 12 | The first `if` statement tests if the value of `p` is not `0`, that is, if it is 13 | a null pointer. The second `if` statement tests if the value `p` points to is 14 | not `0`. 15 | -------------------------------------------------------------------------------- /03/45/45b.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | int ia[3][4] = {{0, 1, 2, 3}, 8 | {4, 5, 6, 7}, 9 | {8, 9, 10, 11}}; 10 | 11 | for (auto i = 0; i < 3; i++) 12 | for (auto j = 0; j < 4; j++) 13 | cout << ia[i][j] << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /05/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.08 2 | 3 | What is a "dangling `else`"? How are `else` clauses resolved in C++? 4 | 5 | ### Solution 6 | 7 | A dangling `else` is the problem of matching each `else` clause to an `if` 8 | clause, which must be dealt with by the implementation of the programming 9 | language. C++ matches each `else` clause to the nearest preceding `if` clause. 10 | -------------------------------------------------------------------------------- /10/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.01 2 | 3 | The `algorithm` header defines a function named `count` that, like `find`, takes 4 | a pair of iterators and a value. `count` returns a count of how often that value 5 | appears. Read a sequence of `int`s into a `vector` and print the `count` of how 6 | many elements have a given value. 7 | 8 | ### Solution 9 | 10 | See `1.cc`. 11 | -------------------------------------------------------------------------------- /10/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.08 2 | 3 | We said that algorithms do not change the size of the containers over which they 4 | operate. Why doesn't the use of `back_inserter` invalidate this claim. 5 | 6 | ### Solution 7 | 8 | Because use of a `back_inserter` first pushes back another element in the 9 | container before returning an iterator to the newly-added element. 10 | -------------------------------------------------------------------------------- /10/36/36.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::list l{0,1,0,1,2,0,1,2,3,0,1,2,3,4}; 9 | std::cout << "Position of last 0 in list: "; 10 | auto lastzero = std::find(l.crbegin(), l.crend(), 0); 11 | std::cout << std::distance(lastzero, l.crend()) << std::endl; 12 | } 13 | -------------------------------------------------------------------------------- /10/40/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.40 2 | 3 | What kinds of iterators do you think `copy` requires? What about `reverse` or 4 | `unique`? 5 | 6 | ### Solution 7 | 8 | `copy` needs three total iterators: two input iterators to read from a sequence, 9 | and an output iterator to read to. `reverse` requires bidirectional iterators, 10 | and `unique` requires a forward iterator. 11 | -------------------------------------------------------------------------------- /12/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.01 2 | 3 | How many elements do `b1` and `b2` have at the end of this code? 4 | 5 | ```cpp 6 | StrBlob b1; 7 | { 8 | StrBlob b2 = {"a", "an", "the"}; 9 | b1 = b2; 10 | b2.push_back("about"); 11 | } 12 | ``` 13 | 14 | ### Solution 15 | 16 | `b2` will be out of scope, but `b1` will contain all four strings originally in 17 | `b2`. 18 | -------------------------------------------------------------------------------- /13/29/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.29 2 | 3 | Explain why the calls to `swap` inside `swap(HasPtr &, HasPtr &)` do not cause a 4 | recursion loop. 5 | 6 | ### Solution 7 | 8 | Inside `swap(HasPtr &, HasPtr &)`, the calls to `swap` are `swap(string *, 9 | string *)` and `swap(int, int)`, which will call those overloaded `swap`s 10 | (probably `std::swap`) and not our `swap`. 11 | -------------------------------------------------------------------------------- /13/44/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.44 2 | 3 | Write a class named `String` that is a simplified version of the library 4 | `string` class. Your class should have at least a default constructor and a 5 | constructor that takes a pointer to a C-style string. Use an `allocator` to 6 | allocate memory that your `String` class uses. 7 | 8 | ### Solution 9 | 10 | See `string.h`. 11 | -------------------------------------------------------------------------------- /03/37/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.37 2 | 3 | What does the following program do? 4 | 5 | ```cpp 6 | const char ca[] = {'h', 'e', 'l', 'l', 'o'}; 7 | const char *cp = ca; 8 | while (*cp) { 9 | cout << *cp << endl; 10 | ++cp; 11 | } 12 | ``` 13 | 14 | ### Solution 15 | 16 | The program prints the C-string `"hello"` to the standard output, without the 17 | null character. 18 | -------------------------------------------------------------------------------- /03/45/45c.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | int ia[3][4] = {{0, 1, 2, 3}, 8 | {4, 5, 6, 7}, 9 | {8, 9, 10, 11}}; 10 | 11 | for (auto i = ia; i < ia + 3; i++) 12 | for (auto j = *i; j < *i + 4; j++) 13 | cout << *j << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /06/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.21 2 | 3 | Write a function that takes an `int` and a pointer to an `int` and returns the 4 | larger of the `int` value or the value to which the pointer points. What type 5 | should you use for the pointer? 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | int larger(const int &i, const int *const j) 11 | { 12 | return (i > *j) ? i : j; 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /09/14/14.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::list seq{"Hello,", "world", "!"}; 9 | std::vector vec; 10 | 11 | vec.assign(seq.begin(), seq.end()); 12 | 13 | for (const auto &e : vec) 14 | std::cout << e << " "; 15 | std::cout << std::endl; 16 | } 17 | -------------------------------------------------------------------------------- /10/27/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.27 2 | 3 | In addition to `unique`, the library defines a function named `unique_copy` that 4 | takes a third iterator denoting a destination into which to copy the unique 5 | elements. Write a program that uses `unique_copy` to copy the unique elements 6 | from a `vector` into an initially empty `list`. 7 | 8 | ### Solution 9 | 10 | See `27.cc`. 11 | -------------------------------------------------------------------------------- /01/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 1.08 2 | 3 | Indicate which, if any, of the following output statements are legal: 4 | 5 | ```cpp 6 | std::cout << "/*"; 7 | std::cout << "*/"; 8 | std::cout << /* "*/" */; 9 | std::cout << /* "*/" /* "/*" */; 10 | ``` 11 | 12 | ### Solution 13 | 14 | The final statement will properly compile, but it nests two comments, so it 15 | should not be used. 16 | -------------------------------------------------------------------------------- /01/19/19.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int v1, v2; 6 | 7 | std::cout << "Enter two integers:" << std::endl; 8 | std::cin >> v1 >> v2; 9 | if (v1 > v2) 10 | while (v1 >= v2) 11 | std::cout << v1-- << std::endl; 12 | else 13 | while (v1 <= v2) 14 | std::cout << v1++ << std::endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /02/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.10 2 | 3 | What are the initial values, if any, of each of the following variables? 4 | 5 | ```cpp 6 | std::string global_str; 7 | int global_int; 8 | int main() 9 | { 10 | int local_int; 11 | std::string local_str; 12 | } 13 | ``` 14 | 15 | ### Solution 16 | 17 | `global_str` and `local_str` will both be initialized to the empty string `""`. 18 | -------------------------------------------------------------------------------- /02/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.12 2 | 3 | Which, if any, of the following names are invalid? 4 | 5 | (a) `int double = 3.14;` 6 | (b) `int _;` 7 | (c) `int catch-22;` 8 | (d) `int 1_or_2 = 1;` 9 | (e) `double Double = 3.14` 10 | 11 | ### Solution 12 | 13 | `double` and `catch-22` are invalid identifiers because of the reserved keyword 14 | `double` and use of the hyphen. 15 | -------------------------------------------------------------------------------- /03/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.07 2 | 3 | What would happen if you define the loop control variable in the previous 4 | exercise as type `char`? Predict the results and then change your program to use 5 | a `char` to see if you were right. 6 | 7 | ### Solution 8 | 9 | The program would work the same, because the previous `auto` variable is deduced 10 | to be a `char`. See `7.cc`. 11 | -------------------------------------------------------------------------------- /03/32/32.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | constexpr size_t arr_size = 10; 8 | int arr[arr_size]; 9 | int arr2[arr_size]; 10 | for (size_t i = 0; i < arr_size; i++) 11 | { 12 | arr[i] = i; 13 | arr2[i] = arr[i]; 14 | cout << arr2[i] << endl; 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /03/43/43c.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | int ia[3][4] = {{0, 1, 2, 3}, 8 | {4, 5, 6, 7}, 9 | {8, 9, 10, 11}}; 10 | 11 | for (int (*i)[4] = ia; i < ia + 3; i++) 12 | for (int *j = *i; j < *i + 4; j++) 13 | cout << *j << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /04/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.32 2 | 3 | Explain the following loop. 4 | 5 | ```cpp 6 | constexpr int size = 5; 7 | int ia[size] = {1, 2, 3, 4, 5}; 8 | for (int *ptr = ia, ix = 0; ix != size && ptr != ia+size; ++ix, ++ptr) 9 | ; /* ... */ 10 | ``` 11 | 12 | ### Solution 13 | 14 | The loop will go through each element of `ia` with `ptr` and retain its 15 | subscript in `ix`. 16 | -------------------------------------------------------------------------------- /06/09/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.09 2 | 3 | Write your own versions of the `fact.cc` and `factMain.cc` files. These files 4 | should include your `Chapter6.h` from the exercises in the previous section. Use 5 | these files to understand how your compiler supports separate compilation. 6 | 7 | ### Solution 8 | 9 | See relevant files. G++-9 generates `a.out` without giving an argument. 10 | -------------------------------------------------------------------------------- /06/52/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.52 2 | 3 | Given the following declarations, 4 | ```cpp 5 | void manip(int, int); 6 | double dobj; 7 | ``` 8 | what is the ank (§ 6.6.1) of each conversion in the following calls? 9 | 10 | (a) `manip('a', 'z');` 11 | (b) `manip(55.4, dobj);` 12 | 13 | ### Solution 14 | 15 | (a) Integral promotion 16 | (b) Conversion to another arithmetic type 17 | -------------------------------------------------------------------------------- /14/14/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.14 2 | 3 | Why do you think it is more efficient to define `operator+` to call `operator+=` 4 | rather than the other way around? 5 | 6 | ### Solution 7 | 8 | Because `operator+` must create a new object that is the result of adding two 9 | objects together. `operator+=` would use an unneccessary temporary object if 10 | implemented via `operator+`. 11 | -------------------------------------------------------------------------------- /03/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.03 2 | 3 | Explain how whitespace characters are handled in the `string` input operator and 4 | in the `getline` function. 5 | 6 | ### Solution 7 | 8 | In the `string` input operator, whitespace is ignored before and after 9 | non-whitespace characters. In the `getline` function, all whitespace is read 10 | except for the final end-of-line character `'\n'. 11 | -------------------------------------------------------------------------------- /03/23/23.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::vector; 6 | 7 | int main() 8 | { 9 | vector vec = {23, -74, 1, -76, 234, -54, 13, 676, 87, 2}; 10 | for (auto it = vec.begin(); it != vec.end(); it++) 11 | *it *= 2; 12 | for (auto elem : vec) 13 | cout << elem << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /03/35/35.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include // begin, end 3 | using std::cout; 4 | using std::endl; 5 | using std::begin; 6 | using std::end; 7 | 8 | int main() 9 | { 10 | int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 11 | for (int *p = begin(arr); p != end(arr); p++) 12 | { 13 | *p = 0; 14 | cout << *p << endl; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /03/44/44.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | using int_row = const int [4]; 8 | int ia[3][4] = {{0, 1, 2, 3}, 9 | {4, 5, 6, 7}, 10 | {8, 9, 10, 11}}; 11 | 12 | for (int_row &row : ia) 13 | for (int i : row) 14 | cout << i << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /05/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.01 2 | 3 | What is a null statement? When might you use a null statement? 4 | 5 | ### Solution 6 | 7 | A null statement is a statement which does nothing, often used in the body of a 8 | loop which requires no additional statements to be run. For example: 9 | 10 | ```cpp 11 | char *s = "Hello, world!"; 12 | while (*s++) 13 | ; // null statement 14 | ``` 15 | -------------------------------------------------------------------------------- /06/27/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.27 2 | 3 | Write a function that takes an `initializer_list` and produces the sum of 4 | the elements in the list. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | int sum(std::initializer_list il) 10 | { 11 | int total = 0; 12 | for (auto beg = il.begin(); beg != il.end(); ++beg) 13 | total += *beg; 14 | return total; 15 | } 16 | ``` 17 | -------------------------------------------------------------------------------- /06/49/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.49 2 | 3 | What is a candidate function? What is a viable function? 4 | 5 | ### Solution 6 | 7 | A candidate function is a previously-declared function which has the same name 8 | as the function being called. A viable function is a subsect of candidate 9 | functions which has the same number and (at least, convertable) type of 10 | arguments as the call. 11 | -------------------------------------------------------------------------------- /07/20/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.20 2 | 3 | When are friends useful? Discuss the pros and cons of using friends. 4 | 5 | ### Solution 6 | 7 | Friends are useful for nonmember functions part of the interface of a class 8 | require access to encapsulated memberes of that class. This advantage could 9 | potentially cause harm if the implementation of those encapsulated members 10 | changes. 11 | -------------------------------------------------------------------------------- /07/43/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.43 2 | 3 | Assume we have a class named `NoDefault` that has a constructor that takes an 4 | `int` but has no default constructor. Define a class `C` that has a member of 5 | type `NoDefault`. Define the default constructor for `C`. 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | struct C { 11 | NoDefault mymember; 12 | C(): mymember(0) { } 13 | }; 14 | ``` 15 | -------------------------------------------------------------------------------- /10/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.32 2 | 3 | Rewrite the bookstore problem from § 1.6 using a `vector` to hold the 4 | transactions and various algorithms to do the processing. Use `sort` with your 5 | `compareIsbn` function from § 10.3.1 to arrange the transactions in order, 6 | and then use `find` and `accumulate` to do the sum. 7 | 8 | ### Solution 9 | 10 | See program files. 11 | -------------------------------------------------------------------------------- /11/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.08 2 | 3 | Write a program that stores the excluded words in a `vector` instead of a `set`. 4 | What are the advantages to using a `set`? 5 | 6 | ### Solution 7 | 8 | See `8.cc`. For this program, the only noticable advantage of using a set is 9 | that we can use `set`'s `find` method rather than needing to include the generic 10 | algorithm in our program. 11 | -------------------------------------------------------------------------------- /12/28/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.28 2 | 3 | Write a program to implement text queries without defining classes to manage the 4 | data. Your program should take a file and interact with a user to query for 5 | words in that file. Use `vector`, `map` and `set` containers to hold the data 6 | for the file and to generate the results for the queries. 7 | 8 | ### Solution 9 | 10 | See `28.cc`. 11 | -------------------------------------------------------------------------------- /13/13/X.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() {std::cout << "~X()" << std::endl;} 8 | X &operator=(const X&) { 9 | std::cout << "X &operator=(const X&)" << std::endl; 10 | return *this; 11 | } 12 | }; 13 | #endif 14 | -------------------------------------------------------------------------------- /13/35/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.35 2 | 3 | What would happen if `Message` had used the synthesized versions of the 4 | copy-control members? 5 | 6 | ### Solution 7 | 8 | If this were the case, a `Message` would fail to add its contents to the 9 | necessary folders in a copy constructor as well as fail to correctly update its 10 | folders to replace the `Message`s in a copy-assignment. 11 | -------------------------------------------------------------------------------- /16/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 16.04 2 | 3 | Write a template that acts like the library `find` algorithm. The function will 4 | need two template type parameters, one to represent the function's iterator 5 | parameters and the other for the type of the value. Use your function to find a 6 | given value in a `vector` and in a `list`. 7 | 8 | ### Solution 9 | 10 | See `4.cc`. 11 | -------------------------------------------------------------------------------- /02/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.32 2 | 3 | Is the following code legal or not? If not, how might you make it legal? 4 | 5 | ```cpp 6 | int null = 0, *p = null; 7 | ``` 8 | 9 | ### Solution 10 | 11 | The code is illegal because a pointer cannot be assigned a variable value. One 12 | solution might be to assign the pointer to `nullptr`: 13 | 14 | ```cpp 15 | int null = 0, *p = nullptr; 16 | ``` 17 | -------------------------------------------------------------------------------- /04/04/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.04 2 | 3 | Parenthesize the following expression to show how it is evaluated. Test your 4 | answer by compiling the expression (without parentheses) and printing its 5 | result. 6 | 7 | ```cpp 8 | 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2 9 | ``` 10 | 11 | ### Solution 12 | 13 | ```cpp 14 | (((12 / 3) * 4) + ((5 * 15) + ((24 % 4) / 2))) 15 | ``` 16 | 17 | See `4.cc`. 18 | -------------------------------------------------------------------------------- /04/25/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.25 2 | 3 | What is the value of `~'q' << 6` on a machine with 32-bit `int`s and 8-bit 4 | `char`s, that uses Latin-1 character set in which `'q'` has the bit pattern 5 | `01110001`? 6 | 7 | ### Solution 8 | 9 | ``` 10 | ~'q' ~01110001 11 | ~'q' << 6 11111111 11111111 11111111 10001110 << 6 12 | -7296 11111111 11111111 11100011 10000000 13 | ``` 14 | -------------------------------------------------------------------------------- /06/33/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.33 2 | 3 | Write a recursive function to print the contents of a `vector`. 4 | 5 | ### Solution 6 | 7 | ```cpp 8 | void print_vector(std::vector::iterator beg, 9 | std::vector::iterator end) 10 | { 11 | if (beg == end) 12 | return; 13 | std::cout << *beg++ << std::endl; 14 | print_vector(beg, end); 15 | } 16 | ``` 17 | -------------------------------------------------------------------------------- /07/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.21 2 | 3 | Update your `Sales_data` class to hide its implementation. The programs you've 4 | written to use `Sales_data` operations should still continue to work. Recompile 5 | those programs with your new class definition to verify that they still work. 6 | 7 | ### Solution 8 | 9 | See `Sales_data.h`. Recompiled program from exercise 13 and works as intended. 10 | -------------------------------------------------------------------------------- /07/27/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.27 2 | 3 | Add the `move`, `set` and `display` operations yo your version of `Screen`. Test 4 | your class by executing the following code: 5 | 6 | ```cpp 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 | ``` 13 | 14 | ### Solution 15 | 16 | See program files. 17 | -------------------------------------------------------------------------------- /11/22/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.22 2 | 3 | Given a `map>`, write the types used as an argument and as 4 | the return value for the veresion of `insert` that inserts one element. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | std::map>::iterator insert( // return type 10 | std::pair> element); // argument 11 | ``` 12 | -------------------------------------------------------------------------------- /14/30/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.30 2 | 3 | Add dereference and arrow operators to your `StrBlobPtr` and `ConstStrBlobPtr` 4 | classes that you defined in exercise 12.22 from § 12.1.6. Note that the 5 | operators in `ConstStrBlobPtr` must return `const` references because the `data` 6 | member in `ConstStrBlobPtr` points to a `const vector`. 7 | 8 | ### Solution 9 | 10 | See program files. 11 | -------------------------------------------------------------------------------- /02/36/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.36 2 | 3 | In the following code, determine the type of each variable and the value each 4 | variable has when the code finishes: 5 | 6 | ```cpp 7 | int a = 3, b = 4; 8 | decltype(a) c = a; 9 | decltype((b)) d = a; 10 | ++c; 11 | ++d; 12 | ``` 13 | 14 | ### Solution 15 | 16 | `a`: `int`, 4. 17 | `b`: `int`, 4. 18 | `c`: `int`, 4. 19 | `d`: `int &`, `a` (4). 20 | -------------------------------------------------------------------------------- /03/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.11 2 | 3 | Is the following range `for` legal? If so, what is the type of `c`? 4 | 5 | ```cpp 6 | const string s = "Keep out!"; 7 | for (auto &c : s) { /* ... */ } 8 | ``` 9 | 10 | ### Solution 11 | 12 | The range `for` is legal as long as the `for` body does not attempt to modify 13 | the string or its individual characters. The type of each `c` is `const char &`. 14 | -------------------------------------------------------------------------------- /03/41/41.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // begin, end 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | using std::begin; 8 | using std::end; 9 | 10 | int main() 11 | { 12 | int arr[] = {1, 2, 3, 4, 5, 6, 7}; 13 | vector vec(begin(arr), end(arr)); 14 | for (int i : vec) 15 | cout << i << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /04/30/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.30 2 | 3 | Using table 4.12 (p. 166), parenthesize the following expressions to match the 4 | default evaluation: 5 | 6 | (a) `sizeof x + y` 7 | (b) `sizeof p->mem[i]` 8 | (c) `sizeof a < b` 9 | (d) `sizeof f()` 10 | 11 | ### Solution 12 | 13 | (a) `(sizeof(x) + y)` 14 | (b) `(sizeof((p->mem)[i]))` 15 | (c) `((sizeof(a)) < b)` 16 | (d) `(sizeof((f())))` 17 | -------------------------------------------------------------------------------- /06/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.12 2 | 3 | Rewrite the program from exercise 6.10 in § 6.2.1 to use references instead 4 | of pointers to swap the value of two `int`s. Which version do you think would be 5 | easier to use and why? 6 | 7 | ### Solution 8 | 9 | See `12.cc`. The reference version is easier to use, as no thinking about 10 | pointers needs to be done by the user of the function. 11 | -------------------------------------------------------------------------------- /09/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.10 2 | 3 | What are the types of the following four objects? 4 | 5 | ```cpp 6 | vector v1; 7 | const vector v2; 8 | auto it1 = v1.begin(), it2 = v2.begin(); 9 | auto it3 = v1.cbegin(), it4 = v2.cbegin(); 10 | ``` 11 | 12 | ### Solution 13 | 14 | `it1` is a `std::vector::iterator`, and `it2` through `it4` are 15 | `std::vector::const_iterator`s 16 | -------------------------------------------------------------------------------- /10/30/30.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::istream_iterator in(std::cin), eof; 9 | std::ostream_iterator out(std::cout, " "); 10 | std::vector v(in, eof); 11 | std::sort(v.begin(), v.end()); 12 | std::copy(v.begin(), v.end(), out); 13 | std::cout << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /10/37/37.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | std::vector v{1,2,3,4,5,6,7,8,9,10}; 10 | std::list l; 11 | std::copy(v.cbegin() + 3, v.cbegin() + 8, std::front_inserter(l)); 12 | for (auto &e : l) 13 | std::cout << e << " "; 14 | std::cout << std::endl; 15 | } 16 | -------------------------------------------------------------------------------- /13/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.03 2 | 3 | What happens when we copy a `StrBlob`? What about `StrBlobPtr`s? 4 | 5 | ### Solution 6 | 7 | When a `StrBlob` is copied, the default synthesized copy constructor is called, 8 | and the `shared_ptr` count is incremented. For a `StrBlobPtr`, the `weak_ptr` 9 | count is not incremented, but its `shared_ptr` is and all the members are 10 | similarly copied. 11 | -------------------------------------------------------------------------------- /13/46/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.46 2 | 3 | Which kind of reference can be bound to the following initializers? 4 | 5 | ```cpp 6 | int f(); 7 | vector vi(100); 8 | int? r1 = f(); 9 | int? r2 = vi[0]; 10 | int? r3 = r1; 11 | int? r4 = vi[0] * f(); 12 | ``` 13 | 14 | ### Solution 15 | 16 | ```cpp 17 | int &&r1 = f(); 18 | int &r2 = vi[0]; 19 | int &r3 = r1; 20 | int &&r4 = vi[0] * f(); 21 | ``` 22 | -------------------------------------------------------------------------------- /14/47/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.47 2 | 3 | Explain the difference between these two conversion operators: 4 | 5 | ```cpp 6 | struct Integral { 7 | operator const int(); 8 | operator int() const; 9 | }; 10 | ``` 11 | 12 | ### Solution 13 | 14 | The first is a conversion operator to a `const int`, while the second is a 15 | conversion operator to a regular `int` which is a `const` method. 16 | -------------------------------------------------------------------------------- /15/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.12 2 | 3 | Is it ever useful to declare a member function as both `override` and `final`? 4 | Why or why not? 5 | 6 | ### Solution 7 | 8 | Yes, as the qualifiers have different purposes. If you want to ensure a function 9 | properly overrides a virtual function but not be able to be overriden by 10 | subsequent derived classes, a function may have both qualifiers. 11 | -------------------------------------------------------------------------------- /02/24/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.24 2 | 3 | Why is the initializations of `p` legal but that of `lp` illegal? 4 | 5 | ```cpp 6 | int i = 42; 7 | void *p = &i; 8 | long *lp = &i; 9 | ``` 10 | 11 | ### Solution 12 | 13 | `p`'s initialization is legal because a pointer to `void` can be of any type, 14 | while a specified poiner to a `long int` can only point to a `long int`, which 15 | is otherwise illegal. 16 | -------------------------------------------------------------------------------- /03/39/39.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::string; 6 | 7 | int main() 8 | { 9 | string s1 = "Foo"; 10 | string s2 = "bar"; 11 | 12 | if (s1 == s2) 13 | cout << s1 << " and " << s2 << " are equal." << endl; 14 | else 15 | cout << s1 << " and " << s2 << " are not equal." << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /06/10/10.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int *x, int *y) 4 | { 5 | int temp = *x; 6 | *x = *y; 7 | *y = temp; 8 | } 9 | 10 | int main(void) 11 | { 12 | int x = 23; 13 | int y = -9; 14 | std::cout << "Before:\nx = " << x << "\ny = " << y << std::endl; 15 | swap(&x,&y); 16 | std::cout << "\nAfter:\nx = " << x << "\ny = " << y << std::endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /06/12/12.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int &x, int &y) 4 | { 5 | int temp = x; 6 | x = y; 7 | y = temp; 8 | } 9 | 10 | int main(void) 11 | { 12 | int x = 23; 13 | int y = -9; 14 | 15 | std::cout << "Before:\nx = " << x << "\ny = " << y << std::endl; 16 | swap(x, y); 17 | std::cout << "After:\nx = " << x << "\ny = " << y << std::endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /06/51/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.51 2 | Write all four versions of `f`. Each function should print a distinguishing 3 | message. Check your answers for the previous exercise. If your answers were 4 | incorrect, study this section until you understand why your answers were wrong. 5 | 6 | ### Solution 7 | 8 | See `51.cc`. The compiler exited with error, because (a) was ambiguous. All 9 | results as expected. 10 | -------------------------------------------------------------------------------- /07/11/11.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Sales_data.h" 3 | 4 | int main(void) 5 | { 6 | Sales_data s1; 7 | Sales_data s2(std::cin); 8 | Sales_data s3("book3", 50, 9.95); 9 | Sales_data s4("book4"); 10 | 11 | print(std::cout, s1) << std::endl; 12 | print(std::cout, s2) << std::endl; 13 | print(std::cout, s3) << std::endl; 14 | print(std::cout, s4) << std::endl; 15 | } 16 | -------------------------------------------------------------------------------- /07/50/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.50 2 | 3 | Determine whether any of your `Person` class constructors should be `explicit`. 4 | 5 | ### Solution 6 | 7 | The only constructor which could make meaningful use of being `explicit` would 8 | be the constructor which takes a reference to an `istream` (as it is the only 9 | one with a single argument). This could be useful to not allow implicit 10 | conversions. 11 | -------------------------------------------------------------------------------- /08/03/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.03 2 | 3 | What causes the following `while` to terminate? 4 | 5 | ```cpp 6 | while (cin >> i) /* ... */ 7 | ``` 8 | 9 | ### Solution 10 | 11 | Assuming `i` is an `int`, for example, the `while` could terminate if passed a 12 | non-`int`, like `"foo"`. Otherwise, if the stream returns any other failure, 13 | like end-of-file or failure, then the `while` will terminate. 14 | -------------------------------------------------------------------------------- /09/32/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.32 2 | 3 | In the program on page 354 would it be legal to write the call it `insert` as 4 | follows? If not, why not? 5 | 6 | ```cpp 7 | iter = vi.insert(iter, *iter++); 8 | ``` 9 | 10 | ### Solution 11 | 12 | No, it would be illegal, since the increment postfix operator is not guaranteed 13 | to increment `iter` before or after its assignment. Its behavior is undefined. 14 | -------------------------------------------------------------------------------- /10/31/31.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::istream_iterator in(std::cin), eof; 9 | std::ostream_iterator out(std::cout, " "); 10 | std::vector v(in, eof); 11 | std::sort(v.begin(), v.end()); 12 | std::unique_copy(v.begin(), v.end(), out); 13 | std::cout << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /11/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.06 2 | 3 | Explain the difference between a `set` and a `list`. When might you use one or 4 | the other? 5 | 6 | ### Solution 7 | 8 | A `set` organizes its data with the element's `<` operator, while a `list` does 9 | not organize its data nor remove duplicates. `set`s are very fast to search, add 10 | and erase elements, but `list`s allow for organization and duplicates. 11 | -------------------------------------------------------------------------------- /11/12/12.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::vector> pairs; 9 | std::string word; 10 | int i; 11 | while (std::cin >> word >> i) 12 | pairs.push_back({word, i}); 13 | for (auto &e : pairs) 14 | std::cout << e.first << " " << e.second << std::endl; 15 | } 16 | -------------------------------------------------------------------------------- /11/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.19 2 | 3 | Define a variable that you initialize by calling `begin()` on the `multiset` 4 | named `bookstore` from § 11.2.2. Write the variable's type without using 5 | `auto` or `decltype`. 6 | 7 | ### Solution 8 | 9 | ```cpp 10 | std::multiset::iterator 11 | first_book = bookstore.begin(); 12 | ``` 13 | -------------------------------------------------------------------------------- /11/29/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.29 2 | 3 | What do `upper_bound`, `lower_bound` and `equal_range` return when you pass then 4 | a key that is not in the container? 5 | 6 | ### Solution 7 | 8 | `upper_bound` and `lower_bound` will both return iterators pointing to the 9 | element just larger than the given key, and `equal_range` will return a `pair` 10 | with iterators pointing to those same element. 11 | -------------------------------------------------------------------------------- /12/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.06 2 | 3 | Write a function that returns a dynamically allocated `vector` of `int`s. Pass 4 | that `vector` to another function that reads the standard input to give values 5 | to the elements. Pass the `vector` to another function to print the values that 6 | were read. Remember to `delete` the `vector` at the appropriate time. 7 | 8 | ### Solution 9 | 10 | See `6.cc`. 11 | -------------------------------------------------------------------------------- /14/46/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.46 2 | 3 | Explain whether defining these `Sales_data` conversion operators is a good idea 4 | and whther they should be `explicit`. 5 | 6 | ### Solution 7 | 8 | The conversion operators are unintuitive and are more clear by simply returning 9 | `salesdata1.bookNo` or `salesdata1.avg_price()`. In the case they are completely 10 | necessary, then they should be `explicit`. 11 | -------------------------------------------------------------------------------- /02/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.19 2 | 3 | Explain the key differences between pointers and references. 4 | 5 | ### Solution 6 | 7 | A pointer is an object -- unlike a reference -- which does not need to be 8 | initialized (although it is advisable to do so). A pointer can change what it 9 | points to during its lifetime and can point to nothing, and a pointer which 10 | points to `void` can be of any type. 11 | -------------------------------------------------------------------------------- /02/20/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.20 2 | 3 | What does the following program do? 4 | 5 | ```cpp 6 | int i = 42; 7 | int *p1 = &i; 8 | *p1 = *p1 * *p1; 9 | ``` 10 | 11 | ### Solution 12 | 13 | The program assigns the value 42 to the integer variable, `i`. It creates a 14 | pointer which points to the address of `i`. Finally, it changes the value of `i` 15 | to the square of `i`, using dereference operators. 16 | -------------------------------------------------------------------------------- /03/15/15.cc: -------------------------------------------------------------------------------- 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::string; 9 | 10 | int main() 11 | { 12 | vector seq; 13 | string cur; 14 | while (cin >> cur) 15 | seq.push_back(cur); 16 | for (string s : seq) 17 | cout << s << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /04/15/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.15 2 | 3 | The following assignment is illegal. Why? How would you correct it? 4 | 5 | ```cpp 6 | double dval; int ival; int *pi; 7 | dval = ival = pi = 0; 8 | ``` 9 | 10 | ### Solution 11 | 12 | A pointer value cannot be assigned to a non-pointer type. One fix, assigning all 13 | values to `0` in one statement could be: 14 | 15 | ```cpp 16 | pi = ival = dval = 0; 17 | ``` 18 | -------------------------------------------------------------------------------- /08/09/9.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | std::istream &inOut(std::istream &is) 6 | { 7 | std::string line; 8 | while (is >> line) 9 | std::cout << line << std::endl; 10 | is.clear(); 11 | return is; 12 | } 13 | 14 | int main() 15 | { 16 | std::string hello = "Hello, world."; 17 | std::istringstream ss(hello); 18 | inOut(ss); 19 | } 20 | -------------------------------------------------------------------------------- /09/43/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.43 2 | 3 | Write a function that takes three `string`s, `s`, `oldVal` and `newVal`. Using 4 | iterators and the `insert` and `erase` functions, replace all instances of 5 | `oldVal` that appear in `s` with `newVal`. Test your function by using it to 6 | replace common abbreviations, such as "tho" by "though" and "thru" by "through". 7 | 8 | ### Solution 9 | 10 | See `43.cc`. 11 | -------------------------------------------------------------------------------- /10/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.21 2 | 3 | Write a lambda that captures a local `int` variable and decrements that variable 4 | until it reaches 0. Once the variable is 0 additional calls should no longer 5 | decrement the variable. The lambda should return a `bool` that indicates whether 6 | the captured variable is 0. 7 | 8 | ### solution 9 | 10 | ```cpp 11 | [&i]() { return i > 0 ? !--i : !i; }; 12 | ``` 13 | -------------------------------------------------------------------------------- /13/01/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.01 2 | 3 | What is a copy constructor? When is it used? 4 | 5 | ### Solution 6 | 7 | A copy constructor is the constructor that is used when an object's argument is 8 | only a reference to another object of the same type. It is used during defining 9 | variables with `=`, passing objects to and from functions and brace 10 | initializing elements in an array or aggregate class. 11 | -------------------------------------------------------------------------------- /13/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.07 2 | 3 | What happens when we assign one `StrBlob` to another? What about `StrBlobPtr`s? 4 | 5 | ### Solution 6 | 7 | When a `StrBlob` is copy-assigned, the `shared_ptr` will be copied and not the 8 | `vector`, and the count will increase. This will similarly happen with the 9 | `StrBlobPtr`: its elements will be individually copied, and not the underlying 10 | `vector`. 11 | -------------------------------------------------------------------------------- /15/19/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 15.19 2 | 3 | Assume that each of the classes from page 612 and page 613 has a member function 4 | of the form: 5 | 6 | ```cpp 7 | void memfcn(Base &b) { b = *this; } 8 | ``` 9 | 10 | For each class, determine whether this function would be legal. 11 | 12 | ### Solution 13 | 14 | All would be legal except for `Derived_from_Private`, because `Base` would not 15 | be accessible. 16 | -------------------------------------------------------------------------------- /02/04/4.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned u = 10, u2 = 42; 6 | std::cout << u2 - u << std::endl; 7 | std:: cout << u - u2 << std::endl; 8 | 9 | int i = 10, i2 = 42; 10 | std::cout << i2 - i << std::endl; 11 | std::cout << i - i2 << std::endl; 12 | 13 | std::cout << i - u << std::endl; 14 | std::cout << u - i << std::endl; 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /06/29/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.29 2 | 3 | When you use an `initializer_list` in a range `for` would you ever use a 4 | reference as the loop control variable? If so, why? If not, why not? 5 | 6 | ### Solution 7 | 8 | Yes, a reference as the loop control variable would keep from copying out the 9 | values of every item in the `initializer_list`, especially if we declare each 10 | element as a reference to `const`. 11 | -------------------------------------------------------------------------------- /08/11/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 8.11 2 | 3 | The program in this section defined its `istringstream` object inside the outer 4 | `while` loop. What changes would you need to make if `record` were defined 5 | outside that loop? Rewrite the program, moving the definition of `record` 6 | outside the `while`, and see whether you thought of all the changes that are 7 | needed. 8 | 9 | ### Solution 10 | 11 | See `11.cc`. 12 | -------------------------------------------------------------------------------- /09/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.13 2 | 3 | How would you initialize a `vector` from a `list`? From a 4 | `vector`? Write code to check your answers. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | std::list seq = {0, 1, 2, 3, 4}; 10 | std::vector seq2 = {0, 1, 2, 3, 4}; 11 | std::vector vec1(seq1.begin(), seq1.end()); 12 | std::vector vec2(seq2.begin(), seq2.end()); 13 | ``` 14 | -------------------------------------------------------------------------------- /09/45/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.45 2 | 3 | Write a function that takes a `string` representing a name and two other 4 | `string`s representing a prefix, such as "Mr." or "Ms." and a suffix, such as 5 | "Jr." or "III". Using iterators and the `insert` and `append` functions, 6 | generate and return a new `string` with the suffix and prefix added to the given 7 | name. 8 | 9 | ### Solution 10 | 11 | See `45.cc`. 12 | -------------------------------------------------------------------------------- /10/02/2.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::list seq; 9 | std::string s; 10 | 11 | while (std::cin >> s) 12 | seq.push_back(s); 13 | std::cout << "There are " << count(seq.cbegin(), seq.cend(), "hi") 14 | << " strings in the sequence with value \"hi\"." << std::endl; 15 | } 16 | -------------------------------------------------------------------------------- /13/17/13_14.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class numbered { 4 | public: 5 | int mysn; 6 | static int uniq; 7 | numbered() { mysn = uniq++; } 8 | }; 9 | 10 | int numbered::uniq = 0; 11 | 12 | void f(numbered n) 13 | { 14 | std::cout << n.mysn << std::endl; 15 | } 16 | 17 | int main() 18 | { 19 | numbered a, b = a, c = b; 20 | f(a); 21 | f(b); 22 | f(c); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /13/21/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.21 2 | 3 | Do you think the `TextQuery` and `QueryResult` classes need to define their own 4 | versions of the copy-control members? If so, why? If not, why not? Implement 5 | whichever copy-control operations you think these classes require. 6 | 7 | ### Solution 8 | 9 | The default copy-control members are adequate for these classes, therefore 10 | nothing needs to be implemented. 11 | -------------------------------------------------------------------------------- /14/43/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.43 2 | 3 | Using library function objects, determine whether a given `int` value is 4 | divisible by any element in a container of `int`s. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | bool isDivisible(const int &n, const std::vector &vec) 10 | { 11 | return std::find_if_not(vec.begin(), vec.end(), 12 | std::bind(std::modulus(), n, _1)) == vec.end(); 13 | } 14 | ``` 15 | -------------------------------------------------------------------------------- /01/05/5.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int v1 = 0, v2 = 0; 6 | 7 | std::cout << "Enter two numbers:" << std::endl; 8 | std::cin >> v1 >> v2; 9 | std::cout << "The sum of "; 10 | std::cout << v1; 11 | std::cout << " and "; 12 | std::cout << v2; 13 | std::cout << " is "; 14 | std::cout << v1 + v2; 15 | std::cout << std::endl; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /02/34/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.34 2 | 3 | Write a program containing the variables and assignments from the previous 4 | exercises. Print the variables before and after the assignments to check whether 5 | your predictions in the previous exercise were correct. If not, study the 6 | examples until you can convince yourself to know what led you to the wrong 7 | conclusion. 8 | 9 | ### Solution 10 | 11 | See `34.cc`. 12 | -------------------------------------------------------------------------------- /03/24/24.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cin; 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | 8 | int main() 9 | { 10 | vector vec; 11 | int num; 12 | while (cin >> num) 13 | vec.push_back(num); 14 | for (auto i = vec.begin(); i != vec.end() - 1; i++) 15 | cout << *i + *(i+1) << " "; 16 | cout << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /03/44/44c.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | int main() 6 | { 7 | using int_row = int (*)[4]; 8 | int ia[3][4] = {{0, 1, 2, 3}, 9 | {4, 5, 6, 7}, 10 | {8, 9, 10, 11}}; 11 | 12 | for (int_row i = ia; i < ia + 3; i++) 13 | for (int *j = *i; j < *i + 4; j++) 14 | cout << *j << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /12/24/24.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int buf = 0; 7 | std::cout << "Enter max length: " << std::flush; 8 | std::cin >> buf; 9 | char *s = new char[buf+1]; // for '\0' at end 10 | std::cin.ignore(); 11 | 12 | std::cout << "Input string: " << std::flush; 13 | std::cin.get(s, buf+1); 14 | std::cout << s << std::endl; 15 | delete [] s; 16 | } 17 | -------------------------------------------------------------------------------- /13/51/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.51 2 | 3 | Although `unique_ptr`s cannot be copied, in § 12.15 we wrote a `clone` 4 | function that returned a `unique_ptr` by value. Explain why that function is 5 | legal and how it works. 6 | 7 | ### Solution 8 | 9 | `unique_ptr`s will use move operations rather than copy operations. Therefore, 10 | they can be returned by a function, given that they are about to be destroyed. 11 | -------------------------------------------------------------------------------- /02/07/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.07 2 | 3 | What values do these literals represent? What type does each have? 4 | 5 | (a) `"Who goes with F\145rgus?\012"` 6 | (b) `3.14e1L` 7 | (c) `1024f` 8 | (d) `3.14L` 9 | 10 | ### Solution 11 | 12 | (a) "Who goes with Færgus?", followed by a Form Feed; string literal 13 | (`char[]`). 14 | (b) 31.4; `long double` 15 | (c) 1024.0; `float` 16 | (d) 3.14, `long double` 17 | -------------------------------------------------------------------------------- /02/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.23 2 | 3 | Given a pointer `p`, can you determine whether `p` points to a valid object? If 4 | so, how? If not, why not? 5 | 6 | ### Solution 7 | 8 | You can test if `p` is a null pointer, which would therefore not be a vlid 9 | object, but otherwise `p` could be uninitialized and have a garbage value 10 | assigned to it. Altering or testing its value could result in undefined 11 | behavior. 12 | -------------------------------------------------------------------------------- /04/12/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 4.12 2 | 3 | Assuming `i`, `j` and `k` are all `int`s, explain what `i != j < k` means. 4 | 5 | ### Solution 6 | 7 | First the expression `j < k` will evaluate to `true` or `false`. The inequality 8 | operator will cast the result of `j < k` to an integer, `1` or `0`. Next, `i` 9 | will be compared to either `1` or `0`, returning `true` if `i` does not equal 10 | the casted value of `j < k`. 11 | -------------------------------------------------------------------------------- /06/04/4.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fact(int val) 4 | { 5 | int ret = 1; 6 | while (val > 1) 7 | ret *= val--; 8 | return ret; 9 | } 10 | 11 | void interact(void) 12 | { 13 | int i; 14 | std::cout << "Enter a number: "; 15 | std::cin >> i; 16 | std::cout << i << "! is " << fact(i) << std::endl; 17 | } 18 | 19 | int main(void) 20 | { 21 | interact(); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /06/13/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.13 2 | 3 | Assuming `T` is the name of a type, explain the difference between a function 4 | declared as `void f(T)` and `void f(T&)`. 5 | 6 | ### Solution 7 | 8 | A function declared with its parameter as a reference to type `T` will effect 9 | the argument which is passed to the function, while the regular `T` argument in 10 | the first function will only pass its value to the parameter. 11 | -------------------------------------------------------------------------------- /06/40/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.40 2 | 3 | Which, if either, of the following declarations are errors? 4 | 5 | (a) 6 | ```cpp 7 | int ff(int a, int b = 0, int c = 0); 8 | ``` 9 | 10 | (b) 11 | ```cpp 12 | char *init(int ht = 24, int wd, char bckgrnd); 13 | ``` 14 | 15 | ### Solution 16 | 17 | (b) is an error, because all parameters after the first default argument 18 | parameter must also be default argument parameters. 19 | -------------------------------------------------------------------------------- /07/18/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.18 2 | 3 | What is encapsulation? Why is it useful? 4 | 5 | ### Solution 6 | 7 | Encapsulation is hiding the implementation of an object and creating a public 8 | interface to interact with the object. It is useful to encapsulate because it 9 | ensures user code does not accidentally modify structural code, and a change in 10 | the implementation will not require user code to change anything. 11 | -------------------------------------------------------------------------------- /09/42/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.42 2 | 3 | Given that you want to read a character at a time into a `string`, and you know 4 | that you need to read at least 100 characters, how might you improve the 5 | performance of your program? 6 | 7 | ### Solution 8 | 9 | Reserve more than 100 elements in the `string` prior to reading characters: 10 | 11 | ```cpp 12 | std::string s; 13 | s.reserve(128); 14 | // process... 15 | ``` 16 | -------------------------------------------------------------------------------- /13/23/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 13.23 2 | 3 | Compare the copy-control members that you wrote for the solutions to the 4 | previous section's exercises to the code presented here. Be sure you understand 5 | the differences, if any, between you code and ours. 6 | 7 | ### Solution 8 | 9 | As stated in Exercises 13.08, the original `HasPtr` copy constructor written 10 | there was in error and could not accept self-assignment. 11 | -------------------------------------------------------------------------------- /16/05/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 16.05 2 | 3 | Write a template version of the `print` function from § 6.2.4 that takes a 4 | reference to an array and can handle arrays of any size and any element type. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | template 10 | void print(const T (&array)[M]) 11 | { 12 | for (size_t i = 0; i != N; ++i) 13 | std::cout << array[i] << std::endl; 14 | } 15 | ``` 16 | -------------------------------------------------------------------------------- /03/08/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 3.08 2 | 3 | Rewrite the program in the first exercise, first using a `while` and again using 4 | a traditional `for` loop. Which of the three approaches do you prefer and why? 5 | 6 | ### Solution 7 | 8 | See `8.cc` and `8b.cc`. My prefered approach is the range `for` loop because a 9 | `decltype` is not needed (nor is a `string::size_type`) and assessing the size 10 | of the string is not needed. 11 | -------------------------------------------------------------------------------- /03/20/20.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cin; 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | 8 | int main() 9 | { 10 | vector vec; 11 | int num; 12 | while (cin >> num) 13 | vec.push_back(num); 14 | for (decltype(vec.size()) i = 0; i < vec.size() - 1; i++) 15 | cout << vec[i] + vec[i+1] << " "; 16 | cout << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /06/47/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 6.47 2 | 3 | Revise the program you wrote in the exercises in § 6.3.2 that used 4 | recursion to print the contents of a `vector` to conditionally print information 5 | about its execution. For example, you might print the size of the `vector` on 6 | each call. Compiler and run the program without debugging turned on and again 7 | with it turned off. 8 | 9 | ### Solution 10 | 11 | See `47.cc`. 12 | -------------------------------------------------------------------------------- /09/49/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.49 2 | 3 | A letter has an ascender if, as with `d` or `f`, part of the letter extends 4 | above the middle of the line. A letter has a descender if, as with `p` or `g`, 5 | part of the letter extends below the line. Write a program that reads a file 6 | containing words and reports the longest word that contains neither ascenders 7 | nor descenders. 8 | 9 | ### Solution 10 | 11 | See `49.cc`. 12 | -------------------------------------------------------------------------------- /12/33/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 12.33 2 | 3 | In Chapter 15 we'll extend our query system and will need some additional 4 | members in the QueryResult class. Add members named `begin` and `end` that 5 | return iterators into the `set` of line numbers returned by a given query, and a 6 | member named `get_file` that returns a `shared_ptr` to the file in the 7 | `QueryResult` object. 8 | 9 | ### Solution 10 | 11 | See program files. 12 | -------------------------------------------------------------------------------- /16/03/3.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include // less 3 | 4 | #include "Sales_data.h" 5 | 6 | template 7 | int compare(const T& v1, const T& v2) 8 | { 9 | if (std::less()(v1, v2)) return -1; 10 | if (std::less()(v2, v1)) return 1; 11 | return 0; 12 | } 13 | 14 | int main() 15 | { 16 | Sales_data a, b; 17 | std::cout << compare(a, b) << std::endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /03/42/42.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | using std::vector; 7 | 8 | int main() 9 | { 10 | vector vec = {1, 2, 3, 4, 5, 6, 7}; 11 | int arr[vec.size()]; 12 | for (auto it = vec.begin(); it != vec.end(); it++) 13 | arr[it - vec.begin()] = *it; 14 | for (int i : arr) 15 | cout << i << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /05/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.17 2 | 3 | Given two `vector`s of `int`s, write a program to determine whether one `vector` 4 | is a prefix of the other. For `vector`s of unequal length, compare the number of 5 | elements of the smaller `vector`. For example, given the `vector`s containing 6 | `{0, 1, 1, 2}` and `{0, 1, 1, 2, 3, 5, 8}`, respectively your program should 7 | return `true`. 8 | 9 | ### Solution 10 | 11 | See `17.cc`. 12 | -------------------------------------------------------------------------------- /05/22/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 5.22 2 | 3 | The last example in this section that jumped back to `begin` could be better 4 | written using a loop. Rewrite the code to eliminate the `goto`. 5 | 6 | ```cpp 7 | begin: 8 | int sz = get_size(); 9 | if (sz <= 0) 10 | goto begin; 11 | ``` 12 | 13 | ### Solution 14 | 15 | ```cpp 16 | for (int sz = get_size(); sz <= 0; sz = get_size()) 17 | ; // null statement 18 | ``` 19 | -------------------------------------------------------------------------------- /14/51/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 14.51 2 | 3 | Show the conversion sequences (if any) needed to call each version of `calc` and 4 | explain why the best viable function is selected. 5 | 6 | ```cpp 7 | void calc(int); 8 | void calc(LongDouble); 9 | double dval; 10 | calc(dval); // which calc? 11 | ``` 12 | 13 | ### Solution 14 | 15 | `calc(int(dval))` or `calc(LongDouble(dval))` are viable function calls and 16 | therefore ambiguous. 17 | -------------------------------------------------------------------------------- /02/06/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 2.06 2 | 3 | What, if any, are the differences between the following definitions: 4 | 5 | ```cpp 6 | int month = 9, day = 7; 7 | int month = 09, day = 07; 8 | ``` 9 | 10 | ### Solution 11 | 12 | Because of the zero prefix in the second statement, the two values `month` and 13 | `day` will be interpreted as octal literals. Because of this, the literal `09` 14 | is not valid, and the program will not compile. 15 | -------------------------------------------------------------------------------- /07/25/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 7.25 2 | 3 | Can `Screen` safely rely on the default versions of copy and assignment? if so, 4 | why? If not, why not? 5 | 6 | ### Solution 7 | 8 | Since the `Screen` class does not rely on manually-allocated memory structures 9 | defined outside the class, and since the class uses only built-in types and 10 | `string`s, the `Screen` class can safely rely on the default copy and assignment 11 | operations. 12 | -------------------------------------------------------------------------------- /09/45/45.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | std::string getName(const std::string &name, const std::string &prefix, 5 | const std::string &suffix) 6 | { 7 | std::string ret(name); 8 | ret.insert(ret.begin(), prefix.cbegin(), prefix.cend()); 9 | return ret.append(suffix); 10 | } 11 | 12 | int main() 13 | { 14 | std::cout << getName("George", "HRH King ", " III") << std::endl; 15 | } 16 | -------------------------------------------------------------------------------- /15/30/30.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "basket.h" 5 | #include "quote.h" 6 | 7 | int main() 8 | { 9 | Basket basket; 10 | for (int i = 0; i < 20; i++) 11 | basket.add_item(Bulk_quote("0-123-4567", 4.99, 10, 0.10)); 12 | for (int i = 0; i < 20; i++) 13 | basket.add_item(Bulk_quote("9-876-5432", 19.89, 100, 0.25)); 14 | basket.total_receipts(std::cout); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /05/20/20.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cin; 4 | using std::cout; 5 | using std::endl; 6 | using std::string; 7 | 8 | int main() 9 | { 10 | string s, t; 11 | while (cin >> s) 12 | { 13 | if (s == t) 14 | break; 15 | t = s; 16 | } 17 | if (s == t) 18 | cout << s << endl; 19 | else 20 | cout << "No word was repeated." << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /05/24/24.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cin; 4 | using std::cout; 5 | using std::endl; 6 | using std::runtime_error; 7 | 8 | int main() 9 | { 10 | int x, y; 11 | cout << "Enter two integers: " << endl; 12 | cin >> x; 13 | cin >> y; 14 | if (y == 0) 15 | throw runtime_error("Divide by zero error"); 16 | cout << x << " / " << y << " = " << (double) x/y << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /09/39/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 9.39 2 | 3 | Explain what the following program fragment does: 4 | 5 | ```cpp 6 | vector svec; 7 | svec.reserve(1024); 8 | string word; 9 | while (cin >> word) 10 | svec.push_back(word); 11 | svec.resize(svec.size()+svec.size()/2); 12 | ``` 13 | 14 | ### Solution 15 | 16 | The program allocates memory for 1024 elements, adds elements until `cin` 17 | returns zero, and then increases the `vector` by 150%. 18 | -------------------------------------------------------------------------------- /10/17/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 10.17 2 | 3 | Rewrite exercise 10.12 from § 10.3.1 to use a lambda in the call to `sort` 4 | instead of the `compareIsbn` function. 5 | 6 | ### Solution 7 | 8 | ```cpp 9 | std::vector v; 10 | /* fill vector with Sales_datas */ 11 | 12 | std::sort(v.begin(), v.end(), 13 | [](const Sales_data &book1, const Sales_data &book2) 14 | { return book1.isbn() < book2.isbn(); }); 15 | ``` 16 | -------------------------------------------------------------------------------- /11/10/README.md: -------------------------------------------------------------------------------- 1 | ### Exercise 11.10 2 | 3 | Could we define a `map` from `vector::iterator` to `int`? What about from 4 | `list::iterator` to `int`? In each case, if not, why not? 5 | 6 | ### Solution 7 | 8 | In each case, the iterators must support the `<` operators in order to be 9 | organize properly, which is possible for `vector::iterator`s, but `list` 10 | iterators do not support `<`, so the second declaration would not work. 11 | --------------------------------------------------------------------------------