习题答案 及 StackOverflow上的讨论
更新: 不少同学要代码。我折腾了一晚上,终于把习题的代码发到github上了: https://github.com/Mooophy/Cpp-Primer 要讨论的同学要熟悉一下git的用法,需要以下几步: 1、注册帐号; 2、fork 我的repository; 3、在你自己的repository里commit,比如填加个注释什么的; 4、给我一个pull request。 ========================================== 或者到这个小组:http://www.douban.com/group/532124/ ========================================== git的操作大概需要一两天来上手,但换来的价值远远超过读一天CP或者做一天习题。因为github上实在是神器遍地、大牛横行啊。。 详细内容参阅这里: Fork A Repo https://help.github.com/articles/fork-a-repo Using pull requests https://help.github.com/articles/using-pull-requests 王月 08, 2014 ===========以下为原帖=========== Exercise 4.33: Explain what the following expression does(Page158): someValue ? ++x, ++y : --x, --y http://stackoverflow.com/questions/22828628/how-does-the-compiler-interpret-this-expression-in-terms-of-precedence-and-asso
Exercise 6.33: Write a recursive function to print the contents of a vector. http://stackoverflow.com/questions/20184299/how-to-understand-and-fix-the-segmentation-fault-in-this-code
Exercise 10.24:Use bind and check_size to find the first element in a vector of ints that has a value greater than the length of a specified string value. http://stackoverflow.com/questions/20539406/what-type-does-stdfind-if-not-return
Exercise 11.11: Redefine bookstore without using decltype. http://stackoverflow.com/questions/20627530/how-to-define-a-multiset-using-a-function-pointer Exercise 11.16: Using a map iterator write an expression that assigns a value to an element.(Page 431) http://stackoverflow.com/questions/20675892/is-it-possible-to-assign-a-value-to-an-element-of-map-using-a-map-iterator Exercise 12.3: Does this class need const versions of push_back and pop_back? If so, add them. If not, why aren’t they needed? (Page 458) http://stackoverflow.com/questions/20725190/operating-on-dynamic-memory-is-it-meaningful-to-overload-a-const-memeber-functi Exercise 13.22: Assume that we want HasPtr to behave like a value. That is, each object should have its own copy of the string to which the objects point. We’ll show the definitions of the copy-control members in the next section. However, you already know everything you need to know to implement these members. Write the HasPtr copy constructor and copy-assignment operator before reading on.(Page 511) http://stackoverflow.com/questions/20864892/is-it-a-good-practice-to-point-to-a-new-address-on-free-store-when-dynamicly-all Exercise 13.50: Put print statements in the move operations in your String class and rerun the program from exercise 13.48 in § 13.6.1 (p. 534) that used a vector to see when the copies are avoided.(P.544) http://stackoverflow.com/questions/20967732/why-wasnt-the-move-constructor-called Exercise 13.53: As a matter of low-level efficiency, the HasPtr assignment operator is not ideal. Explain why. Implement a copy-assignment and move-assignment operator for HasPtr and compare the operations executed in your new move-assignment operator versus the copy-and-swap version.(P.544) http://stackoverflow.com/questions/21010371/why-is-it-not-efficient-to-use-a-single-assignment-operator-handling-both-copy-a Exercise 14.7: Define an output operator for you String class you wrote for the exercises in § 13.5 (p. 531).(Page 558) http://stackoverflow.com/questions/21041877/why-doesnt-stdfor-each-lambda-work-as-expected Exercise 14.14: Why do you think it is more efficient to define operator+ to call operator+= rather than the other way around?(P.561) http://stackoverflow.com/questions/21071167/why-is-it-more-efficient-to-define-operator-to-call-operator-rather-than-the Why isn't it a reference type when std::initializer_list being used as an assignment parameter? http://stackoverflow.com/questions/21104923/why-isnt-it-a-reference-type-when-stdinitializer-list-being-used-as-an-assign Exercise 14.26: Define subscript operators for your StrVec, String, StrBlob, and StrBlobPtr classes. http://stackoverflow.com/questions/21197378/why-did-the-compiler-output-differently-when-compiling-two-similar-classes Exercise 14.33: How many operands may an overloaded function-call operator take? http://stackoverflow.com/questions/21211889/how-many-operands-may-an-overloaded-function-call-operator-take Chapter 15: The difference between reference and pointer as to being used for dynamic binding. http://stackoverflow.com/questions/21278170/why-didnt-the-dynamic-binding-behave-as-expected Trying to understand dynamic binding and virtual functions http://stackoverflow.com/questions/21320779/trying-to-understand-dynamic-binding-and-virtual-functions
Chapter 16 Exercise 16.26: Assuming NoDefault is a class that does not have a default constructor, can we explicitly instantiate vector? If not, why not? http://stackoverflow.com/questions/21525169/while-explicitly-instantiating-vectorsometype-what-is-the-sometype-default-co 答主全站排名73。由于排在前面的多是Java,C#或者C的,单算C++的话应该是top10左右的高手了,回答的很详细。 How to use functor as a member in class template? http://stackoverflow.com/questions/21548259/how-to-use-functor-as-a-member-in-class-template Exercise 16.27: For each labeled statement explain what, if any, instantiations happen. If a template is instantiated, explain why; if not, explain why not. P.677 http://stackoverflow.com/questions/21598635/how-is-a-template-instantiated 哈哈,这道题拿到了9个up vote,里面有讨论assertion的用法。我接受的那个回答讲的蛮好,深入浅出,值得一看,目前这个答案拿到了6个up。 Exercise 16.45: Given the following template, explain what happens if we call g on a literal value such as 42. What if we call g on a variable of type int? P.690 http://stackoverflow.com/questions/21624016/when-a-lvalue-is-passed-to-t-what-will-happen
Chapter 17 正则表达式: http://stackoverflow.com/questions/22263224/how-to-use-stdregex