Explain how whitespace characters are handled in the string input operator and in the
getline
function.
is >> s
, input is separated by whitespaces while reading into string s
.getline(is, s)
input is separated by newline \n
while reading into string s
. Other whitespaces are ignored.getline(is, s, delim)
input is separated by delim
while reading into string s
. All whitespaces are ignored.What would happen if you define the loop control variable in the previous exercise as type char? Predict the results and then change your program to use a char to see if you were right.
The point here is using reference to mutate a string. If changed to something like below, c
would become a char
rather than char&
. In such case, c
is a copy of each character of string str
, thus the assignment c = 'X'
won't mutate str
. As a result, after this for range statement, nothing changes.
string str("a simple string");
for (char c : str) c = 'X';
What does the following program do? Is it valid? If not, why not?
string s; cout << s[0] << endl;
This code was dereferencing and printing the first item stored in s
. Since s
is empty, such operation is invalid, a.k.a. undefined behavior.
Is the following range for legal? If so, what is the type of c?
const string s = "Keep out!"; for (auto &c : s){ /*... */ }
Depending on the code within for loop body. For example:
cout << c; // legal.
c = 'X'; // illegal.
The type of c
is const char&
.
Which, if any, of the following vector definitions are in error? For those that are legal, explain what the definition does. For those that are not legal, explain why they are illegal.
vector<vector<int>> ivec; // legal(c++11), vectors. vector<string> svec = ivec; // illegal, different type. vector<string> svec(10, "null"); // legal, vector have 10 strings: "null".
How many elements are there in each of the following vectors? What are the values of the elements?
vector<int> v1; // size:0, no values. vector<int> v2(10); // size:10, value:0 vector<int> v3(10, 42); // size:10, value:42 vector<int> v4{ 10 }; // size:1, value:10 vector<int> v5{ 10, 42 }; // size:2, value:10, 42 vector<string> v6{ 10 }; // size:10, value:"" vector<string> v7{ 10, "hi" }; // size:10, value:"hi"
In the binary search program on page 112, why did we write
mid=beg+(end-beg)/2;
instead ofmid=(beg+end) /2;
?
There's no operator +
for adding two iterators.
Assuming txt_size is a function that takes no arguments and returns an int value, which of the following definitions are illegal? Explain why.
unsigned buf_size = 1024;
int ia[buf_size]; // illegal, The dimension value must be a constant expression.
int ia[4 * 7 - 14]; // legal
int ia[txt_size()]; // illegal, The dimension value must be a constant expression.
char st[11] = "fundamental"; // illegal, the string's size is 12.
What are the values in the following arrays?
string sa[10]; //all elements are empty strings
int ia[10]; //all elements are 0
int main()
{
string sa2[10]; //all elements are empty strings
int ia2[10]; //all elements are undefined
}
List some of the drawbacks of using an array instead of a vector.
Identify the indexing errors in the following code:
constexpr size_t array_size = 10; int ia[array_size]; for (size_t ix = 1; ix <= array_size; ++ix) ia[ix] = ix;
When ix
equal to 10, the expression ia[ix]
becomes a UB, as it is trying to dereference an element out of range.
What would happen if we did not initialize the scores array in the program on page 116?
If so, values of array are undefined. Like this:
Given that p1 and p2 point to elements in the same array, what does the following code do? Are there values of p1 or p2 that make this code illegal?
p1 += p2 - p1;
p1
with the offset p2 - p1
. After this statement, p1
and p2
points to the same address. What does the following program do?
const char ca[] = { 'h', 'e', 'l', 'l', 'o' }; const char *cp = ca; while (*cp) { cout << *cp << endl; ++cp; }
This code will print all characters in ca
, afterwards as no \0
appended, UB would happen. For most cases, the while loop here won't be terminated as expected and many rubbish would be printed out.
In this section, we noted that it was not only illegal but meaningless to try to add two pointers. Why would adding two pointers be meaningless?
See: