c++ primer Exercise 5.0

来源:互联网 发布:三星高通9008端口救砖 编辑:程序博客网 时间:2024/06/05 09:55

Exercise5.11: What are th values of i and after the each assignments.

int i; doubl d;

d = i = 3.5; // compile warning.  i = 3, d = 3

i = d = 3.5;  //compile warding d =3.5, i =3

Exercise 5.12: Explain what happens in each of the if tests:

if ( 42 = i )

if(i = 42 )


Exercise 5.13: The following assignments is illeage

double dval; int ival; int *pi;

dval = ival = pi =0;

Exercise 5.14: Altough the following are legal, they probably do not behave as the programmer expects, why? Rewrite the exressions as you think they should be

(a) if(ptr = retrieve_pointer() != 0)

)b) if(ival = 1024)

(c) ival + = ival +1;

Exercise 5.15: Explain the difference between prefix and postfix increment


Exercise 5.16: Why do you tink c++ wasn't named ++C?

Exercise 5.17: What whould happen if the while loop that prints the contents of a vector used the prefix incrment operator?

Exercise 5.18: Write a program that defines a vector of pointers to strings. Read the vector. printing each string and its corresponding size.

Exercise 5.19: Assuming that iter is a vector<string>::iterator, indicate which, if any, of the following expressions is legal. Explain the behavior of the legal exprssions.

(a) *iter++;

(b) (*iter) ++

(c) *iter.empty()

(d) iter->empty()

(e) ++ *iter; (f) iter++->empty()

Exercise 5.20. Writ a program to prompt the user for a pair of numbers and report which is smaller.


Exercise 5.21, write a program to process the elemtns of a vector<int>. Replace each element with an odd value by twice that value

0 0
原创粉丝点击