习题答案 及 StackOverflow上的讨论_C++ Primer中文版书评-查字典图书网
查字典图书网
当前位置: 查字典 > 图书网 > > C++ Primer中文版 > 习题答案 及 StackOverflow上的讨论
第四只猴子 C++ Primer中文版 的书评 发表时间:2014-01-09 13:01:21

习题答案 及 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

===========以下为原帖===========

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

展开全文
有用 47 无用 0

您对该书评有什么想说的?

发 表

推荐文章

猜你喜欢

附近的人在看

推荐阅读

拓展阅读

对“习题答案 及 StackOverflow上的讨论”的回应

O_o_Hong 2016-07-23 15:03:51

十分感谢你的付出!

御宅暴君 2015-06-22 16:33:15

好厉害!(五体投地

第四只猴子 2015-01-27 22:40:41

https://github.com/Mooophy/Cpp-Primer
@qzuser

水沫泡鱼 2015-01-27 22:38:23

求楼主发份答案。不甚感激
smpy0504@sina.com

第四只猴子 2015-01-11 17:28:29

@hellooo here :
https://github.com/Mooophy/Cpp-Primer

hellooo 2015-01-07 20:32:40

1534521395@qq.com
楼主发份答案吧。。谢谢啦

第四只猴子 2014-12-17 07:29:12

@奔跑的时针
I believe hosting on github is the most convenient way .

奔跑的时针 2014-12-16 21:54:23

可不可以有pdf 版方便阅读呢?

第四只猴子 2014-09-01 09:24:59

@マメ

习题答案在这里,可在线浏览,也可下到电脑上看:
https://github.com/Mooophy/Cpp-Primer

中文讨论进这里:http://www.douban.com/group/532124/

city_of_stars 2014-09-01 00:10:19

467419748@qq.com
楼主能否发一份答案?十分感谢

第四只猴子 2014-08-27 12:29:57

更新:
关于https://github.com/Mooophy/Cpp-Primer 的讨论和问题可以贴在这里:

http://www.douban.com/group/532124/

第四只猴子 2014-08-22 13:41:37

@ihappy

cheers.

ihappy 2014-08-22 09:35:36

感谢楼主啊,都看到12章了,终于找到相关答案啊了~

第四只猴子 2014-08-22 03:15:52

@训训
你在github上的帐号是gdhaoren吗?
发现bug了,就report一个issue,如果想contribute的话就给我一个pull request。加油吧。

训训 2014-08-21 21:32:32

@戳戳
谢谢。

第四只猴子 2014-08-21 05:43:30

@训训
https://github.com/Mooophy/Cpp-Primer

训训 2014-08-20 22:40:49

楼主好人,最近一直在努力学习c++ primer 楼主能给份答案,谢谢两个先派上啦。
275076730@qq.com

第四只猴子 2014-05-19 14:12:38

  想要讨论的同学要熟悉一下git的用法,大致需要以下几步:
  1、注册帐号;
  2、fork 我的repository;
  3、在你自己的repository里commit,也就是修改你想要讨论的地方,比如填加个注释什么的;
  4、给我一个pull request。
  
  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

airscofield 2014-05-09 20:09:36

@戳戳
self-evident.
Q.E.D

第四只猴子 2014-05-09 19:38:46

@airscofield

check mail.
just wondering how did you find I m 聪明努力的?
anyway you r right .
haha..

airscofield 2014-05-09 17:30:35

正在学,能否请聪明努力的楼主给我一封你的答案,谢谢
aj23wtf@gmail.com

压强 2014-05-04 00:19:10

收到~谢谢楼主!

第四只猴子 2014-05-03 16:09:18

check mail
@每天都被自己帅

吴小白要努力 2014-05-02 22:23:14

593036031@qq.com.
谢谢楼主了!

第四只猴子 2014-05-01 17:39:40

@ 哇咔咔
no problem. just let me know if you find any problem or bug.

憨憨de熊 2014-05-01 16:54:22

收到.THX

第四只猴子 2014-05-01 13:00:04

@哇咔咔 @压强 check mails

压强 2014-05-01 11:49:18

yaqiang_haha321@126.com
求答案~谢谢楼主~

憨憨de熊 2014-04-30 22:40:32

gulullu@gmail.com,跪求答案

第四只猴子 2014-04-22 11:32:07

check your mail box.
@ yeyeyeye

yeyeyeye 2014-04-21 17:31:58

正在学习中。
ifournight@gmail.com 求一份答案,谢谢楼主了。

第四只猴子 2014-04-07 13:23:30

@steve evan
已发。
编译的时候注意C++11 的flag。
发现bug,或其它问题,告诉我一下。

steve evan 2014-04-06 21:26:10

xiekun605746@163.com 感谢楼主

ptmagic 2014-03-17 18:09:29

没有问题哈哈@戳戳

第四只猴子 2014-03-17 16:41:36

@ptmagic
已发。
编译的时候注意C++11 的flag。
发现bug,或其它问题,告诉我一下。

ptmagic 2014-03-17 15:12:47

ufo22332003@gmail.com,求答案

第四只猴子 2014-01-27 04:42:40

哈哈,我也在用ubuntu。 虚拟机不太靠谱,各种莫名其妙的问题。空间够的话,直接给ubuntu装个分区好了,稳定的多,双系统的启动也很方便。然后,ubuntu自带的gcc版本也不够,有些c++11的特性有bug,要手动升级到4.8。

Soichir 2014-01-26 23:55:43

最近太忙了
回来都凌晨了。。
争取早日跟上你的进度。
编译器摆弄了下Mingw 没摆弄明白。
准备有空装个虚拟机跑ubuntu了。

第四只猴子 2014-01-26 04:57:43

进度是15.22,基本都有comment,发现bug务必告诉我一下。

第四只猴子 2014-01-26 04:51:04

已发送,查收。
compiler弄好了吗?这些代码多数都需要C++11的支持才能compile。。

Soichir 2014-01-26 01:19:09

传个答案吧。 参考下。

第四只猴子 2014-01-10 08:41:26

moophy@foxmail.com
这个还能对付,太难的就不行了。。

Soichir 2014-01-10 08:15:01

才看到你后面的回复。 我是把指针和引用的赋值 和 变量的直接赋值 搞混淆了。
先上班去了。thx : )

Soichir 2014-01-10 08:10:36

刚才试了下,果然能编过
明白了,方案2 中间插入了副本,这个副本既不是引用也不是指针 所以不存在const能不能赋值的约束关系。
多谢。 留个邮箱吧, 有问题继续喝你交流。

第四只猴子 2014-01-10 07:58:55

回到你的问题:
“这个解决方案没太看明白,定义一个什么类型的string变量呢 如果是const string,那岂不是还是不能赋值给find_char,如果是非const string,那上层传递过来的const string 又如何能赋值给这个副本呢?”
定义一个什么类型的string变量呢 -- nonconst string对象,如上述代码;
那岂不是还是不能赋值给find_char -- 可以。该方案针对的是那个bad design,也就是使用nonconst的find_char,如上述。
如果是非const string,那上层传递过来的const string 又如何能赋值给这个副本呢?--可以,const的含义是“只读”.可以【用const 创建非const】。就是说可以用“只读的”创建“读写的”。事实上这是一种标准做法,也就是你提到的【副本】更详细的内容参看第七章和第十三章关于copy constructor的内容。
--缀一句,你提到的问题十分重要,是编程的基础思想,在c和其他语言里也有体现,最好能写代码体会一下。

第四只猴子 2014-01-10 07:50:52

方案2的代码应为:
bool is_sentence(const string &s)
{
string::size_type ctr = 0;
string temp = s; //NOTE HERE
return find_char(temp, '.', ctr) == s.size() - 1 && ctr == 1;
}

第四只猴子 2014-01-10 07:47:21

我手里只有英文版的,页码不太对的上。你所提到地方是6.2.3最后那一段话吧?如下:

The right way to fix this problem is to fix the parameter in find_char. If it’s not possible to change find_char, then define a local string copy of s inside is_sentence and pass that string to find_char.

这里【针对那个不好的设计】提出了两个解决方案:

其一,把find_char的第一个parameter修改成(const string& s)。
其二,如果第一个方案无法实现的话,就在is_sentence函数里定义一个local的s的copy,再把这个copy传给find_char。

以上二方案是针对那个bad design的,也就是:

string::size_type
find_char( /* no const */ string &s, char c, string::size_type &occurs);

Soichir 2014-01-10 00:18:26

上面写错页码了 是P192页。

Soichir 2014-01-09 23:59:17

P215 关于常量引用的讲解
下面给出了一个不好的例子,并给出解决方案
“如果实在不能修改find_char 就在is_sentence内部定义一个string类型的变量,令其未s的副本,然后把这个string对象传递给find_char”


这个解决方案没太看明白,定义一个什么类型的string变量呢 如果是const string,那岂不是还是不能赋值给find_char,如果是非const string,那上层传递过来的const string 又如何能赋值给这个副本呢?

Soichir 2014-01-09 23:55:25

nice 正在看这本书呢。好多问题不太会。