本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
cs:programming:cpp:cpp_primer:answers:chpt_14 [2024/01/14 13:46] – 移除 - 外部编辑 (未知日期) 127.0.0.1 | cs:programming:cpp:cpp_primer:answers:chpt_14 [2024/01/14 13:47] (当前版本) – ↷ 链接因页面移动而自动修正 codinghare | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ======Chapter.14====== | ||
+ | Answers for chapter 14 | ||
+ | ---- | ||
+ | ====Ex.14.1-14.10==== | ||
+ | ==ex.14.1== | ||
+ | > | ||
+ | |||
+ | * The difference: | ||
+ | * Overloaded versions must, at least contain a class type parameter, or be a class member | ||
+ | * Neither the order of evaluation nor the properties of the short circuit evaluation for some specified operators will be guaranteed by overloaded versions. | ||
+ | * Overloaded versions can be called directly. | ||
+ | * The same: | ||
+ | * precedence and associativity. | ||
+ | ==ex.14.2== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.3== | ||
+ | > | ||
+ | <code cpp> | ||
+ | (a) " | ||
+ | (b) svec1[0] == svec2[0] //string version | ||
+ | (c) svec1 == svec2 //vector version | ||
+ | (d) " | ||
+ | </ | ||
+ | ==ex.14.4== | ||
+ | > | ||
+ | <code cpp> | ||
+ | (a) % non-member, | ||
+ | (b) %= member, compound-assignment operators | ||
+ | (c) ++ //member, Operators that change the state of their object or that are closely tied to their given type | ||
+ | (d) -> //must be member | ||
+ | (e) << // | ||
+ | (f) && // | ||
+ | (g) == non-member, | ||
+ | (h) () //must be member | ||
+ | </ | ||
+ | ==ex.14.5== | ||
+ | > | ||
+ | <code cpp> | ||
+ | (a) Book | ||
+ | // | ||
+ | //equality: ==, != | ||
+ | //stream: << >> | ||
+ | </ | ||
+ | ==ex.14.6== | ||
+ | > | ||
+ | Please see [[cs: | ||
+ | ==ex.14.7== | ||
+ | > | ||
+ | Please see [[cs: | ||
+ | ==ex.14.8== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.9== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.10== | ||
+ | > | ||
+ | <code bash> | ||
+ | (a) 0-201-99999-9 10 24.95 | ||
+ | (b) 10 24.95 0-210-99999-9 | ||
+ | </ | ||
+ | The first input yields the correct answer. The second input doesn' | ||
+ | <code bash> | ||
+ | //output | ||
+ | 0-201-99999-9 10 249.5 24.95 | ||
+ | 10 24 22.8 0.95 | ||
+ | </ | ||
+ | ====Ex.14.11-14.20==== | ||
+ | ==ex.14.11== | ||
+ | > | ||
+ | <code cpp> | ||
+ | istream& | ||
+ | { | ||
+ | double price; | ||
+ | in >> s.bookNo >> s.units_sold >> price; | ||
+ | s.revenue = s.units_sold * price; | ||
+ | return in; | ||
+ | } | ||
+ | </ | ||
+ | This program failed to validate whether the input was in the proper state. The second input won't be correct based on the given inputs. | ||
+ | ==ex.14.12== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.13== | ||
+ | > | ||
+ | Currently, the // | ||
+ | ==ex.14.14== | ||
+ | > | ||
+ | |||
+ | * If we implement '' | ||
+ | * The '' | ||
+ | * The copy happens when '' | ||
+ | * If we implement '' | ||
+ | * First the '' | ||
+ | * Second, the '' | ||
+ | In sum, implementing the '' | ||
+ | ==ex.14.15== | ||
+ | > | ||
+ | There should be an '' | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.16== | ||
+ | > | ||
+ | //StrBlob & StrBlobPtr// | ||
+ | | ||
+ | //StrVec//: [[https:// | ||
+ | | ||
+ | //String//: [[https:// | ||
+ | | ||
+ | ==ex.14.17== | ||
+ | > | ||
+ | Please see [[cs: | ||
+ | ==ex.14.18== | ||
+ | > | ||
+ | //StrBlob & StrBlobPtr// | ||
+ | | ||
+ | //StrVec//: [[https:// | ||
+ | | ||
+ | //String//: [[https:// | ||
+ | | ||
+ | ==ex.14.19== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.20== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.21== | ||
+ | > | ||
+ | As we discussed in exercise 14.14 before, using '' | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.22== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.23== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.24== | ||
+ | > | ||
+ | No. The the synthesized version works well for the //Book// class. | ||
+ | ==ex.14.25== | ||
+ | > | ||
+ | No other assignment operator needed for //Books// class. | ||
+ | ==ex.14.26== | ||
+ | > | ||
+ | //StrBlob & StrBlobPtr// | ||
+ | | ||
+ | //StrVec//: [[https:// | ||
+ | | ||
+ | //String//: [[https:// | ||
+ | | ||
+ | ==ex.14.27== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.28== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.29== | ||
+ | > | ||
+ | // | ||
+ | ==ex.14.30== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ====Ex.14.31-14.40==== | ||
+ | ==ex.14.31== | ||
+ | > | ||
+ | Because there is only one data member, weak_ptr, and there is no dynamic allocation involved. The synthesized one will handle it well. | ||
+ | ==ex.14.32== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | ==ex.14.33== | ||
+ | > | ||
+ | 256. | ||
+ | >//C++ Language, Annex B - Implementation quantities: ... — Parameters in one function definition [256].// | ||
+ | ==ex.14.34== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | [[https:// | ||
+ | ==ex.14.35== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | [[https:// | ||
+ | ==ex.14.36== | ||
+ | > | ||
+ | [[https:// | ||
+ | ==ex.14.37== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | [[https:// | ||
+ | ==ex.14.38== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | [[https:// | ||
+ | ==ex.14.39== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | [[https:// | ||
+ | ==ex.14.40== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | [[https:// | ||
+ | ====Ex.14.41-14.50==== | ||
+ | ==ex.14.41== | ||
+ | > | ||
+ | Lambda is more terse than a function object. However, in some cases, we might be able to benefit from using function objects, as the function may be reused. For instance, finding the biggest number. We know various numbers may be present. In this case, if we use lambdas, we end up with duplicated codes, whereas we could implement another function call overloading with a different number of parameters to handle the case. | ||
+ | \\ \\ | ||
+ | As a result, lambda should be used for simple or temporary applications. When the case becomes more complex, consider using a function object. | ||
+ | ==ex.14.42== | ||
+ | > | ||
+ | |||
+ | * a) Count the number of values that are greater than 1024 | ||
+ | * b) Find the first string that is not equal to pooh | ||
+ | * c) Multiply all values by 2 | ||
+ | CODE : [[https:// | ||
+ | ==ex.14.43== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | ==ex.14.44== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | ==ex.14.45== | ||
+ | > | ||
+ | |||
+ | * when Sales_data is converted to a string, it should return '' | ||
+ | * when Sales_data is converted to a double, it should return '' | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.46== | ||
+ | > | ||
+ | For // | ||
+ | ==ex.14.47== | ||
+ | > | ||
+ | <code cpp> | ||
+ | struct Integral { | ||
+ | operator const int(); // conversion operator that convert the integral class type to const int type. The member should be a const member | ||
+ | operator int() const; // conversion operator that convert the integral class type to int type | ||
+ | }; | ||
+ | </ | ||
+ | ==ex.14.48== | ||
+ | > | ||
+ | No. In general, there is no obvious mapping between the '' | ||
+ | ==ex.14.49== | ||
+ | > | ||
+ | CODE : [[https:// | ||
+ | | ||
+ | ==ex.14.50== | ||
+ | > | ||
+ | <code cpp> | ||
+ | struct LongDouble { | ||
+ | LongDouble(double = 0.0); | ||
+ | operator double(); | ||
+ | operator float(); | ||
+ | }; | ||
+ | LongDouble ldObj; | ||
+ | int ex1 = ldObj; | ||
+ | float ex2 = ldObj; | ||
+ | </ | ||
+ | * The initialization of '' | ||
+ | * The initialization of '' | ||
+ | ====Ex.14.51-14.53==== | ||
+ | ==ex.14.51== | ||
+ | > | ||
+ | <code cpp> | ||
+ | void calc(int); | ||
+ | void calc(LongDouble); | ||
+ | double dval; | ||
+ | calc(dval); // which calc? | ||
+ | </ | ||
+ | In this case, the // | ||
+ | ==ex.14.52== | ||
+ | > | ||
+ | <code cpp> | ||
+ | struct LongDouble { | ||
+ | // member operator+ for illustration purposes; + is usually a nonmember | ||
+ | LongDouble operator+(const SmallInt& | ||
+ | // other members as in § 14.9.2 (p. 587) | ||
+ | }; | ||
+ | LongDouble operator+(LongDouble&, | ||
+ | SmallInt si; | ||
+ | LongDouble ld; | ||
+ | ld = si + ld; | ||
+ | ld = ld + si; | ||
+ | </ | ||
+ | * '' | ||
+ | * **Candidate: | ||
+ | * **Viable**: Only built-in version. because '' | ||
+ | * **Best Match**:None. '' | ||
+ | * '' | ||
+ | * **Candidate: | ||
+ | * **Viable**: '' | ||
+ | * **Best Match**:'' | ||
+ | ==ex14.53== | ||
+ | > | ||
+ | <code cpp> | ||
+ | SmallInt s1; | ||
+ | double d = s1 + 3.14; | ||
+ | </ | ||
+ | The function call is an ambiguous call. '' | ||
+ | The call can be rewrote as follows in order to make it legal: | ||
+ | <code cpp> | ||
+ | double d = s1 + SmartInt(3.14); | ||
+ | </ | ||