huangmingchuan / cpp_primer_answers Goto Github PK
View Code? Open in Web Editor NEW《C++ Primer》第五版中文版习题答案
《C++ Primer》第五版中文版习题答案
练习2.29中第2问不合法确实没有问题,但是理由应该是p3是指向const的指针所以不能给普通指针赋值,而const指针是可以给普通指针赋值的;在第4问中,答案应该是非法的,p3是指向const的const指针,题中应该假设该指针已经初始化,所以不能再重新给它赋值。
答案写的是Derived_from_Protected 正确答案应该是Derived_from_Private不合法
因为如果派生类继承基类的方式是公有地或者受保护的,则派生类的成员和友元可以使用派生类向基类的类型转换;反之,如果派生类继承基类的方式是私有的,则不能使用。
指针应该是支持下标索引的,原代码执行应当没有问题。
编译环境为:
Ubuntu 18.04
g++ 7.5.0(-std=c++11)
代码应该将while
循环后的print(...)
这一行去掉,不然会在最后将读取失败的那一个读取结果输出出来。
练习3.17和3.20,怎么停止cin输入继续后面的程序呢?直接ctrl+c程序停止,我试了下可以用getline读入,然后判断字符串是否为空,但练习3.20是要读入整型数字,请问要怎么停止cin?
练习 2.29
(b)为什么const指针不能赋值给普通指针?http://www.cnblogs.com/mylinux/archive/2013/04/27/3047719.html
网页上有“常指针可以赋值给一个普通指针”;
(d)(e)(f)为什么是正确的?
https://github.com/Mooophy/Cpp-Primer/tree/master/ch02
与英文版冲突?
求解。
在Window_mgr中的私有成员screens未定义,会导致成员函数clear中的s未定义。
Screen &s = screens[i];//screens未定义
在Window_mgr::clear定义前定义私有成员screens:Window_mgr::Window_mgr():screens{ Screen(8, 5, ' ') }{}
问:给定指针 p,你能知道它是否指向了一个合法的对象吗?如果能,叙述判断的思路;如果不能,也请说明原因。
答:不能,因为首先要确定这个指针是不是合法的,才能判断它所指向的对象是不是合法的。
这个逻辑怎么理解?是因为本身指针就可能不合法,然后就无法判断指向的对象是否合法嘛?那指针的合法性是否能判断呢?
while (begin++ != end) { if (*begin == i) return true; }
在这段循环中,begin迭代器被++,导致在接下来的查找操作中无法对vector的第一个元素进行判断。
(d)应该是不合法的吧
static double rate = 6.5
虽然说是少了一个constexpr,但是在书270页下面也写了为静态成员提供const 整数 类型的类内初始值,6.5不算是整型吧应该。虽然可以编译成功,但是这个情况算是书上的一个错误吗?我看英文版的翻译也是integral type
练习2.37
decltype(a=b) d = a
应该只有一个地方避免了拷贝
Mystring baz()
{
Mystring ret("world");
return ret; // avoided
}
typedef string Type;
Type initVal(); //Type为string
class Exercise {
public:
typedef double Type;//type为double
Type setVal(Type);//type为double
Type initVal(); //type为double
private:
int val;
};
Type Exercise::setVal(Type parm) {//第一个type为string,第二个type为double ,此处有错误第一个type应改为Exercise::Type
val = parm + initVal(); //调用成员函数initVal()
return val;
}
练习9.4程序中该句while (begin++ != end)
感觉有问题,会跳过第一个元素,自增运算应在下面的if语句执行之后再执行
是改版了么,我测试的constexpr函数是可以没有return的,只要返回类型是void
g++ -m64
2.31 中 r1 = v2
合法吗?
此时相当于
const int v2 = 0;
int &r1 = v2;
这应该并不合法吧?虽然 v2
是顶层的,但这个位置不是顶层的拷贝,r1
是个普通引用
r1 = v2;//应该非法,非常量引用指向一个常量对象
练习2.37
int a = 3, b = 4;
decltype(a) c = a;
decltype(a = b) d = a;
c 是 int 类型,值为 3。d 是 int& 类型,绑定到 a。
我觉得d的类型是bool吧,我的验证程序:
#include <iostream>
#include <typeinfo>
int main() {
int a;
int b = a = 1;
decltype(a == b) c;
std::cout << typeid(c).name() << std::endl;;
bool flag;
std::cout << typeid(flag).name() << std::endl;;
return 0;
}
谢谢
不能够访问private成员!!!!!
file本身就是一个智能指针了,直接return即可。
答案中用make_shared,但是编写有误,不能用file初始化此对象,要用也是用*file。
练习6.50: (b) f(42) 的可行函数应该是是 f(int) 和 f(double, double = 3.14), f(int) 是最佳匹配。
good
题目要求使用迭代器,而答案用的是范围for语句
有个取反符合漏掉了,正确答案是-7296
没有考虑输入字符串为奇数情况,英文版此处考虑到了,应改为
while (m <= n)
程序正常退出了,没有像你说的不能退出循环
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
int main(){
using namespace std::placeholders;
int n = 5;
std::vector<int> nums = {2, 3, 4, 5};
auto is_divisible = std::any_of(nums.begin(), nums.end(), std::bind(std::equal_to<int>(), std::bind(std::modulus<int>(), n, _1), 0));
std::cout << (is_divisible ? "Yes!" : "No!") << std::endl;
return 0;
}
std::string value; int *count; TreeNode *left; TreeNode *right;
请问class TreeNode
的数据域是不是写错了?在原书中是int count
,但是答案中写的是int * count
。
我的理解是作者想要在此处实现引用计数的功能?
博主在练习4.25中的练习只是对"q"进行移位,但题目要求的是在移位之前还有个位取反的操作,答案有误。
我的答案是-7296。
思路是:q
的二进制01110001取反得到的结果是10001110,其十进制为142,超出了8位char的表示范围(-128~127),char溢出时会循环表示,即128的表示是-128,所以142的表示是(-128+14 = -114),左移6位相当于乘以2^6,所以最后值为-7269。
欢迎大家讨论^.^
答案中的(b) i = static_cast(*pc);
应该改为(b) i = static_cast<int>(*pc);
少了<int>
还有练习6.13中的void f(&T)
应该改为void f(T&)
吧,不明白为什么书上也是写成了void f(&T)
void print(const int (&ia)[10]) { for (size_t i = 0; i != 10; ++i) cout << ia[i] << endl; }
ia形参是引用类型,引用不支持下标操作。可改用范围for语句。
位于98页
错误处是!it->empty()
由于插入元素会使所有指针失效,因此需要对iter与mid进行重新赋值,不然会报错。
应改为:
vector::iterator iter = vec.begin(),
mid = vec.begin() + vec.size() / 2;
while (iter != mid)
{
if (*iter == some_val)
{
iter = vec.insert(iter, 2 * some_val);
++iter;
}
++iter;
mid = vec.begin() + vec.size() / 2;
}
应为 i = static_cast(*pc);
emmmm
似乎是很难的一道题
求答案
OwO
在下面的表达式中添加括号,说明其求值过程及最终结果。编写程序编译该(不加括号的)表达式并输出结果验证之前的推断。
12 / 3 * 4 + 5 * 15 + 24 % 4 / 2
((12/3)4) + (515) + ((24%4)/2)
你的答案中515这个少了乘号吧
练习5.19: 编写一段程序, 使用 do while 循环重复地执行任务: 首先提示用户输入两个 string 对象 然后挑出较短的那个输出 这题是要挑出较短的那个
这道题 应该 将
cout << (s1 <= s2 ? s1 : s2) << " is less than the other. " << "\n" << "More? y/n: ";
改成
cout << (s1.size() <= s2.size() ? s1 : s2) << " is less than the other. " << "\n" << "More? y/n: ";
答案上的那个 是比较两个字符串的字典序 但是题目是要比较 字符串的长度 并输出较短的那个
(b) IntP p1(pi); 这个应该是不合法的吧,unique_ptr应该是new返回的指针配合使用吧
循环条件应该是
while (curr <= s.end() - oldVal.size())
否则无法处理如下字符
hello lo ll
I have written the according code, it shows 1 when cout <<sizeof(p)/sizeof(*p)<<endl;
.
Would you please check it out again?
vector iv = {1, 2, 3, 2, 5, 7, 8, 9, 10};
经验证,输出应为*/
练习 7.39:https://github.com/huangmingchuan/Cpp_Primer_Answers/tree/master/ch07#%E7%BB%83%E4%B9%A0739
如果接受string 的构造函数和接受 istream& 的构造函数都使用默认实参,这种行为合法吗?如果不,为什么?
不合法。当你调用 Sales_data() 构造函数时,无法区分是哪个重载。
接受 string 的构造函数和接受 istream& 的构造函数参数类型不同,应该可以重载呀
应该是
(c) (sizeof a) < b
?
当p1是个常量时,该操作非法,而不是什么时候都合法
If you write code like int null = 0, *p = null
, you will get a error:
you can not init a int* with a int
if (p = getPtr() != 0) 当getPtr() 返回为空指针,则if 条件返回为false
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.