GithubHelp home page GithubHelp logo

cpp_primer_practice's Issues

练习2.27

练习2.27里(b)中的i2,在前面没有找到它的定义。
(a). int i = -1, &r = 0;
(g). const int i2 = i, &r = i;
(b). int *const p2 = &i2;
如果i2是按这个顺序来定义的话,那(b)应该不合法。p2得包含底层const
const int *const p2 = &i2;

7.35解释错误

在类内typedef之前并没有使用Type类型,因此可以重新声明, 正确解释如下
image

【勘误】第九章 练习9.7及练习9.8

练习9.7

  • 应将 vector<int>::size_type 改为 vector<int>::iterator

练习9.8

  • 应将 list<string>::const_iterator 改为 list<string>::value_type
  • 应将 list<string>::iterator 改为 list<string>::reference

练习10.31 答案有误

// 正确答案
istream_iterator str_in(cin);
istream_iterator end;

vector vec(str_in, end);

sort(vec.begin(),vec.end());
unique_copy(vec.begin(), vec.end(), ostream_iterator(cout, " "));

【报错】练习2.32

合法。指针可以初始化为 0 表示为空指针。

int null=0, *p=null 无法通过编译,报错

error: cannot initialize a variable of type 'int *' with an lvalue of type 'int'
int *p = null;
^ ~~~~

但是int null=0, *p=0 可以通过编译

练习10.5 错了

用 c风格的字符串比较 还是会相等的, 比较的还是 字符

【bug】练习2.29

原文中:

ic = *p3;   // 合法, 对 p3 取值后是一个 int 然后赋值给 ic

其中 ic 和 p3 的定义是:

const int ic;
const int *const p3;

无法将值赋给常量,此答案应为非法

练习7.33

第七章33题,按照254页上面的内容,不应该是在pos前面加上Screen::吗?为什么是去掉size前面的Screen:: ??这么做是为什么,求解释。

练习2.29中的错误。

p2 = p1; // 合法, 可以将普通指针赋值给常量指针

这个是答案错误的。
原文中,p2是一个指针常量,是不可以修改指针值的。

习题5.7中(a)题有误

(a) if (ival1 != ival2)
ival1 = ival2
else
ival1 = ival2 = 0;

#缺少分号,应改成

(a) if (ival1 != ival2)
ival1 = ival2;
else
ival1 = ival2 = 0;

10.7(b)问题答案错误

reserve只分配了需要的空间 泛性算法只能在已有元素上修改和移动,无法添加和删除元素(c++primer 5edtion P338)应改为resize

打错字

笔记中"第十四章 重载运算与类型转换"下的"避免有二义性的类型转换"第一行打错字了
第几个亿 ——> 定义
原:

  • 通常,不要为类第几个亿相同的类型转换,也不要在类中定义两个及以上转换源或转换目标是算术类型的转换。

新:

  • 通常,不要为类定义相同的类型转换,也不要在类中定义两个及以上转换源或转换目标是算术类型的转换。

对于13.56的理解 求指点

如果定义非const & 版本的函数 不是也会发生无限递归么 例子这里想表达的是什么意思呢

class Foo {
public:
   // Foo sorted() const &;
    Foo sorted() {
       Foo ret;
       return ret.sorted();
   }
};
int main(int arg, char **args) {

    Foo f;
    f.sorted();
    return 0;
}

image

9.47

这应该算是一个版本吧

练习6.18答案需要改进

6.18中的a,函数名是只有比较功能。输入的实参可能是右值或者常量引用。应该改成const更合理
(a) bool compare (const matrix &m1, const matrix &m2);

b中我感觉可以用const int i,比 int i更好。不需要更改i的值,传const int &i,效率比传int i更高。
(b)vector::iterator change_val(const int &i, vector::iterator itor);

习题4.20

iter类型应当是

vector<string>::iterator

13.4疑问

为什么Point *heap = new Point(global) 采用拷贝构造,这只是传指针,不应该是直接初始化吗。

练习2.29中,d,e,f都错了

d中,p3是const指针,已经存在了就不能再变化,不能再将&ic的值赋给p3;
e中,同d,p2是const指针,已经初始化就不能再变化;
f中,ic已经是一个常量,*p3也是一个常量,不能进行赋值操作

练习6.50答案有误

对于练习6.50中,f(42) 的最佳匹配为 f(int), 但可行函数应该包含两个吧,f(int)f(double, double=3.14)

如下程序所示

#include <iostream>
using namespace std;

void f(double x, double y=3.14) {
    cout << "double x, double y=3.14" << endl;
}

int main()
{
    f(42);
}

运行结果如图所示:
image

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.