Giter Club home page Giter Club logo

cpp_primer_answers's People

Contributors

amytx avatar decccc avatar fushenshen avatar gglin98 avatar huangmingchuan avatar jokerkeny avatar sanerror avatar skyhiter avatar smzztx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpp_primer_answers's Issues

练习2.29中答案有问题

练习2.29中第2问不合法确实没有问题,但是理由应该是p3是指向const的指针所以不能给普通指针赋值,而const指针是可以给普通指针赋值的;在第4问中,答案应该是非法的,p3是指向const的const指针,题中应该假设该指针已经初始化,所以不能再重新给它赋值。

练习15.19答案有误

答案写的是Derived_from_Protected 正确答案应该是Derived_from_Private不合法
因为如果派生类继承基类的方式是公有地或者受保护的,则派生类的成员和友元可以使用派生类向基类的类型转换;反之,如果派生类继承基类的方式是私有的,则不能使用。

7.13会多输出一行0 0 0

编译环境为:

Ubuntu 18.04
g++ 7.5.0(-std=c++11)

代码应该将while循环后的print(...)这一行去掉,不然会在最后将读取失败的那一个读取结果输出出来。

练习7.32 私有成员screens 未定义

在Window_mgr中的私有成员screens未定义,会导致成员函数clear中的s未定义。
Screen &s = screens[i];//screens未定义

在Window_mgr::clear定义前定义私有成员screens:Window_mgr::Window_mgr():screens{ Screen(8, 5, ' ') }{}

习题2.23怎么理解?

问:给定指针 p,你能知道它是否指向了一个合法的对象吗?如果能,叙述判断的思路;如果不能,也请说明原因。

答:不能,因为首先要确定这个指针是不是合法的,才能判断它所指向的对象是不是合法的。

这个逻辑怎么理解?是因为本身指针就可能不合法,然后就无法判断指向的对象是否合法嘛?那指针的合法性是否能判断呢?

练习9.4答案有误

while (begin++ != end) { if (*begin == i) return true; }
在这段循环中,begin迭代器被++,导致在接下来的查找操作中无法对vector的第一个元素进行判断。

练习7.58中有一个疑问

static double rate = 6.5
虽然说是少了一个constexpr,但是在书270页下面也写了为静态成员提供const 整数 类型的类内初始值,6.5不算是整型吧应该。虽然可以编译成功,但是这个情况算是书上的一个错误吗?我看英文版的翻译也是integral type

练习13.50有错误

应该只有一个地方避免了拷贝

Mystring baz()
{
	Mystring ret("world");
	return ret; // avoided
}

练习7.35答案有问题

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的程序有问题

练习9.4程序中该句while (begin++ != end)感觉有问题,会跳过第一个元素,自增运算应在下面的if语句执行之后再执行

7.54 问题

是改版了么,我测试的constexpr函数是可以没有return的,只要返回类型是void

2.31 疑问

2.31 中 r1 = v2 合法吗?
此时相当于

const int v2 = 0;
int &r1 = v2;

这应该并不合法吧?虽然 v2 是顶层的,但这个位置不是顶层的拷贝,r1 是个普通引用

练习2.31有误

r1 = v2;//应该非法,非常量引用指向一个常量对象

练习2.37

练习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;
}

谢谢

练习12.33 get_file编写有误

file本身就是一个智能指针了,直接return即可。
答案中用make_shared,但是编写有误,不能用file初始化此对象,要用也是用*file。

练习6.50 (b) f(42)

练习6.50: (b) f(42) 的可行函数应该是是 f(int) 和 f(double, double = 3.14), f(int) 是最佳匹配。

第三章练习3_20

没有考虑输入字符串为奇数情况,英文版此处考虑到了,应改为
while (m <= n)

3.37的疑问

程序正常退出了,没有像你说的不能退出循环

练习14.43 我的笨解法

#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;
}

有关练习13.28

std::string value; int *count; TreeNode *left; TreeNode *right;
请问class TreeNode的数据域是不是写错了?在原书中是int count,但是答案中写的是int * count
我的理解是作者想要在此处实现引用计数的功能?

练习4.25有疑惑

博主在练习4.25中的练习只是对"q"进行移位,但题目要求的是在移位之前还有个位取反的操作,答案有误。
我的答案是-7296。
思路是:q的二进制01110001取反得到的结果是10001110,其十进制为142,超出了8位char的表示范围(-128~127),char溢出时会循环表示,即128的表示是-128,所以142的表示是(-128+14 = -114),左移6位相当于乘以2^6,所以最后值为-7269。
欢迎大家讨论^.^

练习4.37

答案中的(b) i = static_cast(*pc);应该改为(b) i = static_cast<int>(*pc);
少了<int>
还有练习6.13中的void f(&T)应该改为void f(T&)吧,不明白为什么书上也是写成了void f(&T)

6.24的疑问

void print(const int (&ia)[10]) { for (size_t i = 0; i != 10; ++i) cout << ia[i] << endl; }
ia形参是引用类型,引用不支持下标操作。可改用范围for语句。

9.22的答案用VS2017编译有错

由于插入元素会使所有指针失效,因此需要对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;
}

error

练习4.4

在下面的表达式中添加括号,说明其求值过程及最终结果。编写程序编译该(不加括号的)表达式并输出结果验证之前的推断。

12 / 3 * 4 + 5 * 15 + 24 % 4 / 2

((12/3)4) + (515) + ((24%4)/2)

你的答案中515这个少了乘号吧

练习 5.19 有错

练习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: ";

答案上的那个 是比较两个字符串的字典序 但是题目是要比较 字符串的长度 并输出较短的那个

练习12.17

(b) IntP p1(pi); 这个应该是不合法的吧,unique_ptr应该是new返回的指针配合使用吧

9_43.cc有误

循环条件应该是
while (curr <= s.end() - oldVal.size())
否则无法处理如下字符
hello lo ll

the second anwser in 4.29

I have written the according code, it shows 1 when cout <<sizeof(p)/sizeof(*p)<<endl;.
Would you please check it out again?

4.30

应该是
(c) (sizeof a) < b

练习3.53

当p1是个常量时,该操作非法,而不是什么时候都合法

exercise 2.32

If you write code like int null = 0, *p = null, you will get a error:
you can not init a int* with a int

Exercise 4.16

if (p = getPtr() != 0) 当getPtr() 返回为空指针,则if 条件返回为false

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.