更新:
不少同学要代码。我折腾了一晚上,终于把习题的代码发到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
===========以下为原帖===========
3-16章的习题基本都有做,有需要的同学可以豆邮我。
以下是部分po在Stack Overflow的习题,都有不错的讨论,值得一看,更欢迎指正。
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
Exercise 16.62: Define your own version of hash<Sales_data> and define an unordered_multiset of Sales_data objects. Put several transactions into the container and print its contents.
这道题没贴SO,但挺值得一说的。
官网给出了这道题的代码,但那个代码无法编译,而且把template 的specialization放在了.cc文件而非.h文件里。这样就违反了【template的所有代码都应放在header里】这一大原则。
另外,无论是课文还是官网的代码都没有inline下面这个operator的implementation,这也导致编译器报错:【重复定义】。
size_t hash<Sales_data>::operator()(const Sales_data& s) const
我最后是把这段implementation放到了class内,再把整段template的specialization放到Sale_data.h里的最后面才通过编译。经测试,代码可以正常运行。
请大牛指正。
Chapter 17
正则表达式:
http://stackoverflow.com/questions/22263224/how-to-use-stdregex
习题答案 及 StackOverflow上的讨论
对“习题答案 及 StackOverflow上的讨论”的回应
《C++ Primer中文版》热门书评
-
关于这本书的读法和适合的人群
292有用 18无用 [已注销] 2009-09-22
说实话,我学完C以后进阶到C++的第一本书就是这本CP4。CP4确实是经典,但是它却不适合初学者,我当时读了大概有一半了,但是对很多东西还是一知半解。因为这本书是站在编译器的角度来写的,很多东西非常细节,事无巨细的论述对初学者一点好处也没有。初学者需要快速能先吸引眼球的东西。我比较推崇C++新手能先...
-
关于C++的扯淡
131有用 12无用 LUCKYsama 2009-01-29
今天下午在看C++Primer的时候跟同学东扯西扯一些乱七八糟的东西就填在这里。这本书还真的是不错的。 C++ 的诞生大约是在1980-1983年。总之我的一直以来的基本观点是,C++当初的诞生一定不是为了现在面向对象方法论上教授的那些扯淡的东西……什么把一个类的数据封装起来,只提供Get()和Se...
-
我的语言学习黄金期
125有用 20无用 archer 2008-07-02
那个时候不知道大学期间应该找个女朋友,不知道应该出去跟同学喝酒唱歌,不知道还有很多更诱人的技术,我只知道我现在要学c++,而且我手头这本书,是个大砖头,有1000多页,英文版,不争分夺秒看的话,一学期肯定看不完。学期一开始的时候,我就握着这本书,拿出一张纸制定我的学习计划,这周看这章,下周学习这一章...
-
习题答案 及 StackOverflow上的讨论
47有用 0无用 第四只猴子 2014-01-09
更新:不少同学要代码。我折腾了一晚上,终于把习题的代码发到github上了:https://github.com/Mooophy/Cpp-Primer要讨论的同学要熟悉一下git的用法,需要以下几步:1、注册帐号;2、fork 我的repository;3、在你自己的repository里commi...
-
《C++ Primer》第5版问世:一本不折不扣的C++蓝本
27有用 2无用 白云鹏 2013-12-09
科学计算、分布式应用、嵌入式行业、智能控制、算法研究,乃至学术讨论和上机考试都会有一种语言的身影,那就是大名鼎鼎的C++语言。原汁原味的C++目前已经执到C++11标准。听到这门编程语言,多数人伴随而来的是晦涩、复杂、强大等关键词!这样的感觉实际上是真实的。Lippman(著名C++语言专家,《C+...
书名: C++ Primer中文版
作者:
出版社: 中国电力出版社
副标题: 第三版
译者: 潘爱民 | 张丽
出版年: 2002-4
页数: 1033
定价: 128.00元
装帧: 平装
ISBN: 9787508309897