applenob / cpp_primer_practice Goto Github PK
View Code? Open in Web Editor NEW搞定C++:punch:。C++ Primer 中文版第5版学习仓库,包括笔记和课后练习答案。
搞定C++:punch:。C++ Primer 中文版第5版学习仓库,包括笔记和课后练习答案。
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);
使用的是PdgCntEdit软件
修复后文件在此:Cpp Primer.pdf
笔记中"第十四章 重载运算与类型转换"下的"避免有二义性的类型转换"第一行打错字了
第几个亿 ——> 定义
原:
新:
p2 = p1; // 合法, 可以将普通指针赋值给常量指针
这个是答案错误的。
原文中,p2是一个指针常量,是不可以修改指针值的。
https://github.com/applenob/Cpp_Primer_Practice/blob/master/excersize/ch10.md#%E7%BB%83%E4%B9%A01037
copy(v.crend() - 8, v.crend() - 3, back_inserter(l));
# 检验
copy(v.cbegin() + 3, v.cbegin() + 8, front_inserter(l));
d中,p3是const指针,已经存在了就不能再变化,不能再将&ic的值赋给p3;
e中,同d,p2是const指针,已经初始化就不能再变化;
f中,ic已经是一个常量,*p3也是一个常量,不能进行赋值操作
应该输出
10 10
而不是10,10
构造函数的列表初始化应该用 {a,b,c...}, 不是(a,b,c...)
为什么Point *heap = new Point(global) 采用拷贝构造,这只是传指针,不应该是直接初始化吗。
int main()
{
vector<int> iv = { 1,2,3,4,5,6,7,8 };
vector<int>::iterator iter = iv.begin(),
mid = iv.begin() + iv.size() / 2;
while (iter != mid)
{
if (*iter == 3)
{
iter = iv.insert(iter, 2 * 3);
++iter;
mid = iv.begin() + iv.size() / 2;
}
++iter;
}
return 0;
}
iter类型应当是
vector<string>::iterator
练习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
cout << i << " " << ++i << endl;
虽然<<被重载了,但重载不改变优先级,++的优先级比<<高,为什么不先计算++,再<<?为什么输出是未定义的?
第七章33题,按照254页上面的内容,不应该是在pos前面加上Screen::吗?为什么是去掉size前面的Screen:: ??这么做是为什么,求解释。
原文中:
ic = *p3; // 合法, 对 p3 取值后是一个 int 然后赋值给 ic
其中 ic 和 p3 的定义是:
const int ic;
const int *const p3;
无法将值赋给常量,此答案应为非法
ch09.md line166, line168
ch11.md line109
// 正确答案
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, " "));
对于练习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);
}
(a) if (ival1 != ival2)
ival1 = ival2
else
ival1 = ival2 = 0;
#缺少分号,应改成
(a) if (ival1 != ival2)
ival1 = ival2;
else
ival1 = ival2 = 0;
练习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;
用 c风格的字符串比较 还是会相等的, 比较的还是 字符
为你点赞
My account was compromised, as a result many spam issues got created across multiple repos. I am deleting all such issues. Please check my tweet: https://x.com/arghyac35/status/1729721954909684064?s=20
合法。指针可以初始化为 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
可以通过编译
这应该算是一个版本吧
reserve只分配了需要的空间 泛性算法只能在已有元素上修改和移动,无法添加和删除元素(c++primer 5edtion P338)应改为resize
原题:给定指针 p,你能知道它是否指向了一个合法的对象吗?如果能,叙述判断的思路;如果不能,也请说明原因。
我的看法: 把 if(p)
放在 try
中 , 当程序块顺利执行时,表示p指向了合法的对象;当程序块出错跳转到catch语句时,表示p没有指向合法的对象。
答案思路来源- 牛客网
https://m.nowcoder.com/questionTerminal?uuid=3c218c5306324eaf9de45a74932f0b9e
既然空字符串存在1个元素\0,使用下标0去访问它应该是合法的,经检验也是可行的。
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.