How many elements do b1 and b2 have at the end of this code?
StrBlob b1; { StrBlob b2 = { "a", "an", "the" }; b1 = b2; b2.push_back("about"); }
At the end of this code,
b1
holds 4 elements;b2
has been destroyed automatically. So it's meaningless to say how many elements in b2
.Does this class need const versions of push_back and pop_back? If so, add them. If not, why aren’t they needed?
You can certainly do this if you want to, but there doesn't seem to be any logical reason. The compiler doesn't complain because this doesn't modify data (which is a pointer) but rather the thing data points to, which is perfectly legal to do with a const pointer. by David Schwartz.
Discussion over this exercise on Stack Overflow
Discussion over this exercise more on douban(chinese)
In our check function we didn’t check whether i was greater than zero. Why is it okay to omit that check?
Because the type of i
is std::vector<std::string>::size_type
which
is an unsigned
.When any argument less than 0 is passed in, it will convert
to a number greater than 0. In short std::vector<std::string>::size_type
will ensure it is a positive number or 0.
We did not make the constructor that takes an initializer_list explicit (7.5.4, p. 296). Discuss the pros and cons of this design choice.
keyword explicit
prevents automatic conversion from an initializer_list
to StrBlob
.
This design choice would easy to use but hard to debug.
Pros
Cons
Explain what if anything is wrong with the following function.
bool b() { int* p = new int; // ... return p; }
The p will convert to a bool , which means that the dynamic memory allocated has no chance to be freed. As a result, memory leakage will occur.
Explain what happens in the following code:
int *q = new int(42), *r = new int(100); r = q; auto q2 = make_shared<int>(42), r2 = make_shared<int>(100); r2 = q2;
q
and r
:Memory leakage happens. Because after r = q
was executed, no pointer points to the int r
had pointed to. It implies that no chance to free the memory for it.
q2
and r2
:It's safe. Because after 'r2 = q2', the reference count belongs to r2 reduce to 0 and the reference count belongs to q2 increase to 2, then the memory allocated by r2 will be released automatically.
We could have written StrBlobPtr’s deref member as follows:
std::string& deref() const { return (*check(curr, "dereference past end"))[curr]; }
Which version do you think is better and why?
The original one is better, because it's more readable.
Given the following new expression, how would you delete pa?
int *pa = new int[10];
delete [] pa;
We could have written the loop to manage the interaction with the user as a do while (5.4.4, p. 189) loop. Rewrite the loop to use a do while. Explain which version you prefer and why.
do {
std::cout << "enter word to look for, or q to quit: ";
string s;
if (!(std::cin >> s) || s == "q") break;
print(std::cout, tq.query(s)) << std::endl;
} while ( true );
I prefer do while
, because it looks clearer.
What difference(s) would it make if we used a vector instead of a set to hold the line numbers? Which approach is better? Why?
vector
doesn't guarantee that elements being held are unique, so set
is a better choice for this case.