Giter Club home page Giter Club logo

cpp-primer-5th-exercises's Introduction

Solutions to Exercises in C++ Primer 5th Edition

This repo is the solutions to exercises in book C++ Primer (5th Edition). All files except explicitly announced are under Apache License.

Contents

Chapter 1 Getting Started

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45

Chapter 4 Expressions

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38

Chapter 5 Statements

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25

Chapter 6 Functions

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56

Chapter 7 Classes

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58

Chapter 8 The IO Library

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38.1 38.2

Chapter 12 Dynamic Memory

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33

Chapter 13 Copy Control

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67

Chapter 17 Specialized Library Facilities

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39

Chapter 18 Tools for Large Programs

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30

Chapter 19 Specialized Tools and Techniques

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26

cpp-primer-5th-exercises's People

Contributors

heturing avatar jaege avatar utack avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpp-primer-5th-exercises's Issues

ch 6.2 (c)

inc calc(int v1, int v1) ==> int calc(int v1, int v2)

6.7.cpp

returns 0 for any number of calls.

4.20

(f) iter++->empty() is legal.
The expression means move iter to point to the next element and check if that string is empty.

In this case its means move iter to point to the next element and check if the original string pointed by iter is empty.

Best regards

ch2/2.32.cpp

// Method 1
const int null1 = 0;
int *p1 = null1;

// Method 5
constexpr int null2 = 0;
int *p5 = null2;

are invalid, leads to type conversion error.

12.10: Clarification

It's worth noting that while the call process(shared_ptr<int>(p)) works fine in this example, it's not necessary to use anything more than just p as the argument, because p is already a shared_ptr, so passing just p creates a copy and increments the counter just fine. The reason this is important is that if p had been defined as a built-in pointer instead, then process(shared_ptr<int>(p)) would be a problem, because the call would create a shared_ptr pointing to the same memory that the built-in pointer p does and set its count at 1 (not 2), so the memory pointed to by the smart pointer would be deleted when process is finished, resulting in the built-in pointer p becoming a dangling pointer.

Therefore, it's probably better to avoid phrasing the call in this way altogether so that we don't learn something that would be a bad habit to get into.

9.22: Iterator mid is invalidated any time an insertion before it occurs

The program also has an error in that the location of mid must be handled some other way, as the iterator to mid would be invalidated every time an insertion in the front half of the vector occurs -- though this program doesn't do it, it could be invalidated after any operation that changes the size of the container.

One way to handle this would be removing the need to access the mid iterator any time after the loop starts:

std::vector<int>::iterator iter = iv.begin();

for (std::vector<int>::size_type cnt = iv.size()/2; cnt != 0; --cnt) {
  if (*iter == some_val) {
    iter = iv.insert(iter, 2 * some_val);
    ++iter;
  }
  ++iter;
}

11.26: Return type is a reference

The return type from the subscript operator is not mapped_type. It is mapped_type&, which is what allows an expression like ++word_count[word] to increment the mapped value of the element within the map instead of only incrementing a temporary copy of it.

5.23.cpp failed

This exercise should use the exception to handle "divide by zero" issue. It fails for b = 0 input.

9.39: Description of what final line does is inaccurate.

The final line will only reallocate memory if svec.size() + svec.size / 2 > svec.capacity(). Regardless of whether reallocation occurs, the final line will change the size of the vector so that it is half-again (rounded down) the size it was before, and the elements added to the back of the vector to expand the size will be initialized to empty strings.

I think this is what you were trying to say, but the way it is worded says something entirely different.

11.34

In fact, code will not compile, cuz m has const modificator.

Best regards!

7.9

What happens if the user inputs a first and last name? Or when the user uses spaces in their address?

If we input "John Smith" the function will recognize "Smith" as the address and print "John Smith" before we've entered an address.

If we input "John" for the name and "123 Main St" for the address the function will print "John 123".

How do we get the read function to take the whole string with spaces?

7.35: A class cannot redefine a name used earlier outside the class as a type definition.

Line 9 is an error, redefining a name that had been used as a type earlier outside the class scope -- note that some compilers will not flag this as an error.

In lines 10, 11, and 16, then, Type will be std::string.
Line 18, then, has a type-mismatch error, since parm is a std::string (no conversion from std::string to int.)
Line 19, then, returns the wrong type.

To correct the errors, use a different name than Type for the in-class type definition of double, and adjust the name in lines 10, 11, and 16. Adjusting all three will fix the errors in lines 18 and 19 without further changes.

ch12.11 may be wrong

p.get() return int* , but can't implicit conversion a int* to a smart pointer.
image

9.40: Clarification

Since the exercise asks what the program does, it's worth noting what the capacity will be at the end of the program instead of what it is after the program reads the words.

(a) The capacity will be at least 1,024 -- reserve sets the capacity to be enough to hold at least the specified amount. Since half-again 256 is still less than 1,024, no reallocation occurs during the resize, so the capacity stays the same.
(b) The capacity will be at least 1,024 -- half again 512 is still less than 1,024, so no reallocation occurs.
(c) The capacity will be at least 1,500 -- half again 1,000 is greater than 1,024, so reallocation occurs during the resize, changing the capacity to be at least the resize amount.
(d) The capacity will be at least 1,572 -- after reading 1,025 words, reallocation is likely to occur before reading the remaining 23 words ("likely" because it depends on whether the original allocation was 1,024 or greater than that.) The capacity after reading the full 1,048 words, then, depends on the implementation. If it's more than 1,572, the resize won't require an additional reallocation, but we know that the final capacity cannot be less than that because of the resize.

Misunderstanding of Ex. 14.43

I first did the exercise the way it is done here, but I think there is a fundamental misunderstanding of the text. The number that has to be entered is not the divisor, but the dividend. Hence the "divisible by" in the exercise. For example, 100 (dividend) is entered and checked for 2, 3, 4 (divisor) and not 2 (divisor) checked for 80, 90, 100 (dividend).
A correct approach to solve this exercise can be found on https://github.com/pezy/CppPrimer/blob/master/ch14/ex14_43.cpp in my opinion.

This approach might be a bit odd at first (why defining std::modulus mod? Why not doing the straight solution by using %?) But using std::modulus is what the exercise asks for and since we have to enter a dividend and not a divisor, something like the solution in this link seems to me the way to go for.

13.28 solution confusion

In ch13/13.28.cpp, line 173:
BinStrTree::BinStrTree(const BinStrTree &rhs) : root(new TreeNode(*rhs.root)), use(rhs.use) { ++*use; }
As BinStrTree implements the pointer-like copy control members, I think root should be initialized with rhs.root, not new TreeNode(*rhs.root).
The code might be changed to:
BinStrTree::BinStrTree(const BinStrTree &rhs) : root(rhs.root), use(rhs.use) { ++*use; }

6.35

In exercise 6.35, if we pass val-- to the fact function instead of val - 1, the fact function will be invoked every time with the same value val, that's because val-- decrease the value by 1 and return a copy of the original value.

as a result, there will happen a stack overflow and the program will crash.

13.5 mistake

error: conversion from 'const char [6]' to non-scalar type 'HasPtr' requested
HasPtr hp1 = "World";

in the origininal cpp file.

9.9: The begin (and end) functions do not always return a (non-const) iterator type.

The type returned by begin and end depends on the const-ness of the object calling it. If the calling object is const, then begin/end will return a const_iterator. If the calling object is not const, then begin/end will return a (non-const) iterator.

(Your answer for exercise 9.10 reflects this correctly, but it is not made clear in the answer for this exercise.)

5.14 should print no repeated word

5.14 exercise should print " no repeated word " output for " hi hello " input.

input : hi hello
output : hi occurs 1 time. // should be " no repeated word "

11.29

Returns iterators which are equal and "refer to the point where the key can be inserted while maintaining the container order". This insertion point is not necessarily c.end

4.19

Increment operator has a higher preference and it returns a copy of the previous object, so

*p++ // means *(p++) which is just the original value pointed by p

Best regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.