jaege / cpp-primer-5th-exercises Goto Github PK
View Code? Open in Web Editor NEWSolutions to Exercises in C++ Primer 5th Edition
License: Apache License 2.0
Solutions to Exercises in C++ Primer 5th Edition
License: Apache License 2.0
// 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.
inc calc(int v1, int v1) ==> int calc(int v1, int v2)
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.
(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
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.
error: conversion from 'const char [6]' to non-scalar type 'HasPtr' requested
HasPtr hp1 = "World";
in the origininal cpp file.
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.
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.
It is okay for the first same ISBN(s) but when it is given another ISBN it breaks up the code and doesn't show last one's transactions :(
Cpp-Primer-5th-Exercises/ch7/7.35.cpp
Line 17 in 732276e
The fuction signature here would be std::string Exercise::setVal(double para)
Should it be "ales_data item = {"978-0590353403", 25, 15.99 * 25};" ๏ผ
15.99 is the price, not revenue, that is why book said "Such initialization is tedious and error-prone"
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.)
I would like to know if there is a specific reason to not use the std::lower function. Is there a tradeoff in performance or something.
Thank You in advance.
https://github.com/jaege/Cpp-Primer-5th-Exercises/blob/master/ch5/5.13.md
The solution is fine, but there is a logical mistake in the code we were provided.
The minor issue here is that the case 10 will never be hit if we modulus with 10.
If we want the switch to count all even numbers we should make a case label for 0 not 10.
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?
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; }
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;
}
This exercise should use the exception to handle "divide by zero" issue. It fails for b = 0 input.
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.
In fact, code will not compile, cuz m has const modificator.
Best regards!
returns 0 for any number of calls.
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 "
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.
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
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
https://github.com/jaege/Cpp-Primer-5th-Exercises/blob/master/ch5/5.12.cpp
The fPresent
record the occurrence of f, but it didn't mean that f
is the immediate predecessor character of i
, f
and l
.
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.