C++ Primer(第5版) 习题解答
龙心雕虫
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
C++ Primer(第五版)7.1.4节练习
7.11 添加构造函数struct Sales_data { Sales_data()= default; Sales_data(const string &s): bookNo(s) { } Sales_data(const string &s, unsigned n, double p): bookNo(s), units_sold(n), reve...原创 2019-10-24 11:30:53 · 397 阅读 · 0 评论 -
C++ Primer(第五版)7.2--7.2.1节练习
7.16 访问说明符指定其后成员的访问级别,其出现次数没有严格规定,有效范围直到出现下一个访问说明符或到达类结尾。可在整个程序内被访问的成员定义为public的;只能被类的成员函数访问,不能被使用该类的代码访问,定义为private的。7.17 默认访问权限不同,struct所有成员是public的,class所有成员是private的。7.18 封装可以隐藏类的实现细节...原创 2019-10-24 11:56:52 · 368 阅读 · 0 评论 -
C++ Primer(第五版) 7.3.1--7.3.4节练习
7.23 #ifndef SCREEN_H#define SCREEN_Hclass Screen {public: using pos = std::string::size_type; Screen() = default; Screen(pos ht, pos wd): height(ht), width(wd), contents(ht * wd, ' ') { ...原创 2019-10-24 14:49:40 · 587 阅读 · 0 评论 -
C++ Primer(第五版) 7.4--7.6节练习
7.33 pos位于类的作用域之外,无法直接使用,应改为Screen::pos。7.34 放在最后一行无法通过编译,声明中使用的名字,包括返回类型和参数列表使用的名字,都必须在使用前确保可见。7.35 typedef string Type;Type iniVal(); //Type为stringclass Exercise {public: t...原创 2019-10-25 12:56:53 · 735 阅读 · 0 评论 -
C++ Primer(第五版) 7.1.1--7.1.3节练习
7.1 Sales_data类:#ifndef SALES_DATA_H#define SALES_DATA_H#include <iostream>#include <string>using namespace std;struct Sales_data { string bookNo; unsigned units_sold = 0;...原创 2019-10-22 10:45:44 · 517 阅读 · 0 评论 -
C++ Primer(第五版) 6.6--6.7节练习
6.49 函数匹配时,对应的重载函数集称为候选函数;考察提供的实参,候选函数中能够被这组实参调用的函数,叫做可行函数。6.50 (a) 二义性调用,无法匹配; (b) 精确匹配void f(int); (c) 精确匹配void f(int, int); (d) 精确匹配void f(double, double);...原创 2019-10-18 12:46:18 · 359 阅读 · 0 评论 -
C++ Primer(第五版) 6.5.1--6.5.3节练习
6.40 (b) 错误,ht被赋予默认值,则其后的所有形参都必须有默认值。6.41 (a) 非法,至少要提供第一个参数; (b) 合法; (c) 合法,但不符合预期,'*'被传递给第二个参数。6.42 #include <iostream>using namespace std;string make...原创 2019-10-18 12:05:07 · 357 阅读 · 0 评论 -
C++ Primer(第五版)6.3.2--6.4节练习
6.30 1)for循环中的return没有返回值,编译器报错; 2)for循环之后没有return语句,编译器无法发现这一错误。6.31 当引用绑定的是函数的局部变量时,返回的引用或常量引用都是无效的。6.32 合法。功能是把数组按照其索引号赋值。get函数接受一个数组指针和索引号为参数,返回数组该元素的引用,然后在main函数的for循环中赋值。...原创 2019-10-18 10:49:05 · 553 阅读 · 0 评论 -
C++ Primer(第五版)6.2.4--6.2.6节练习
6.21 int cmp(const int a, const int *p){ return (a > *p)? a : *p;}6.22 #include <iostream>using namespace std;void myswap(int *&p1, int *&p2){ auto temp = p1;...原创 2019-10-17 12:25:23 · 541 阅读 · 2 评论 -
C++ Primer(第五版)6.2.1--6.2.3节练习
6.10 #include <iostream>using namespace std;void iswap(int *a, int *b){ int temp = *a; *a = *b; *b = temp;}int main(){ int a = 1, b = 2; cout << a << " " <<...原创 2019-10-17 11:34:55 · 342 阅读 · 0 评论 -
C++ Primer(第五版) 6.1--6.1.3节练习
6.1 函数定义中的参数是形参;调用函数时所用的参数为实参。实参是形参的初始值。6.2 (a) 函数定义的返回值为int,函数体中返回值为string,不匹配; (b) 缺少返回类型; (c) 形参名字重复; (d) 缺少花括号。6.3 int fact(int val){ if (val > 1) ...原创 2019-10-16 10:16:21 · 381 阅读 · 0 评论 -
C++ Primer(第五版) 5.4.1--5.6.3节练习
5.14 #include <iostream>#include <vector>using namespace std;//统计单词及次数的数据结构struct wcnt { string word; int cnt;};int main(){ string word; vector<string> svec; w...原创 2019-10-15 12:12:43 · 265 阅读 · 0 评论 -
C++ Primer(第五版) 5.1--5.3.2节练习
5.1 只有一个单独分号的语句是空语句;当语法需要但逻辑上不需要时用到空语句;5.2 复合语句也被称作块,是指用花括号括起来的语句和声明序列;语法上需要一条语句,但逻辑上需要多条语句,应该用到块。5.3 while (val <= 10) sum += val,++val; //可读性降低5.4 (a) 非法,iter...原创 2019-10-14 10:49:04 · 473 阅读 · 0 评论 -
C++ Primer(第五版)4.9--4.11.3节练习
4.28 #include <iostream>using namespace std;int main(){ cout << "char: " << sizeof(char) << endl; cout << "short: " << sizeof(short) << endl; co...原创 2019-10-12 11:00:17 · 324 阅读 · 0 评论 -
C++ Primer(第五版) 13.1.6节练习
13.18 Employee类及其测试代码如下:#include <iostream>#include <string>using namespace::std;class Employee { friend void get_info(const Employee &s);public: Employee() { id = num++;...原创 2019-06-03 11:08:43 · 453 阅读 · 2 评论 -
C++ Primer(第五版) 13.1.1节练习
13.1 拷贝构造函数:如果一个构造函数的第一个参数是自身类类型的引用(一般为const引用),且任何额外参数都有默认值,则该构造函数为拷贝构造函数。 使用拷贝构造函数的几种情况: 1)用 = 定义变量(拷贝初始化) 2)将一个对象作为实参传递给一个非引用类型的形参 3)用花括号列表初始化一个数组...原创 2019-06-02 16:09:50 · 651 阅读 · 0 评论 -
C++ Primer(第五版) 13.1.2节练习
13.6 拷贝赋值运算符:可以看作一个名为operator=的成员函数,接受一个与其所在类相同类型的参数,返回一个指向其左侧运算对象的引用。 何时使用:对类对象进行赋值操作时使用; 进行的操作:将右侧运算对象的每个非static成员赋予左侧运算对象的对应成员; 当一个类未定义自己的拷贝赋值运算符时,编译器会生成合成拷贝运算符。...原创 2019-06-02 16:21:18 · 356 阅读 · 0 评论 -
C++ Primer(第五版) 13.1.3节练习
13.9 析构函数:执行和构造函数相反的操作,释放对象所使用的资源,并销毁对象的非static成员。析构函数名字为~接类名,无返回值和参数; 合成析构函数先执行函数体(空函数体),后销毁成员。 当一个类没有定义析构函数时,编译器会合成析构函数。13.10 销毁StrBlob对象:执行合成的析构函数,先执行空函数体,再销毁成员da...原创 2019-06-02 16:31:06 · 417 阅读 · 0 评论 -
C++ Primer(第五版) 13.1.4节练习
13.14 使用合成的拷贝控制成员,则直接复制mysn的值,f(a), f(b), f(c)输出相同的结果。13.15 使用自定义的拷贝构造函数生产新序号,会改变结果。但b=a, c=b,及三次调用f(),都运行了拷贝构造函数,每次都产生新的序号,因此f(a), f(b), f(c)的输出和a, b, c中的mysn值并不相同。13.16 参数使用const ...原创 2019-06-02 16:57:10 · 529 阅读 · 1 评论 -
C++ Primer(第五版) 13.2--13.21节练习
13.22 HasPtr类的拷贝构造函数和拷贝赋值运算符代码如下:#include <iostream>#include <string>using namespace::std;class HasPtr {public: HasPtr(const string &s = string()): ps(new string(s)), i(0) ...原创 2019-06-03 11:36:50 · 260 阅读 · 0 评论 -
C++ Primer(第五版) 13.2.2节练习
13.27 引用计数版本的HasPtr:class HasPtr {public: HasPtr(const string &s = string()): ps(new string(s)), i(0), use(new size_t(1)) {} HasPtr(const HasPtr &p): ps(new string(*p.ps)), i(p.i...原创 2019-06-03 11:37:07 · 429 阅读 · 0 评论 -
C++ Primer(第五版) 13.3节练习
13.29 swap(HasPtr&, HasPtr&)函数中调换的ps和i都是内置类型,会调用标准库std::swap,不会导致递归循环。13.30 代码如下(operator=用swap实现):#include <iostream>#include <string>#include <vector>#include...原创 2019-06-03 11:37:31 · 381 阅读 · 0 评论 -
C++ Primer(第五版) 8.1.2节练习
练习8.1--8.2代码:#include <iostream>using namespace::std;istream& in_out(istream &in){ int temp; while (1) { cin >> temp; /* 到达文件结尾 */ if (in.eof()) { cout <&l...原创 2019-04-30 17:11:07 · 546 阅读 · 0 评论 -
C++ Primer(第五版) 9.3.1节练习
9.18#include <iostream>#include <deque>using namespace std;int main(){ deque<string> sd; string s; while (cin >> s) sd.push_back(s); for (auto beg = sd.begin(...原创 2019-05-14 12:04:57 · 363 阅读 · 0 评论 -
C++ Primer(第五版) 9.3.2--9.3.3节练习
9.23 4个值都是容器中仅有的一个元素。9.24 vector<int> iv; 4种方式:*(iv.begin()); iv.front() ; iv[0] ;iv.at(0) ;9.25 elem1 = elem2,不删除元素;elem2是尾后迭代器,删除elem1到容器的最后一个元素;若elem1和elem2都是尾后迭代器,不删除元素。...原创 2019-05-14 15:44:28 · 347 阅读 · 0 评论 -
C++ Primer(第五版) 9.3.4--9.3.6节练习
9.27#include <iostream>#include <forward_list>using namespace std;int main(){ forward_list<int> flist = {1, 2, 3, 4, 5, 6, 7, 8}; auto prev = flist.before_begin(); aut...原创 2019-05-14 18:17:55 · 363 阅读 · 0 评论 -
C++ Primer(第五版) 9.4--9.5.1节练习
9.35 capacity是在不分配新的内存空间的前提下,vector中最多可以保存多少元素;size是vector已经保存的元素数目。9.36 capacity不可能小于size。9.37 list占用的内存总与当前保存的元素占用的内存相等;array的大小不会改变;二者都不需要capacity。9.38 程序结果时vector每次需要分配新内存空间时将当前容量...原创 2019-05-15 11:17:02 · 353 阅读 · 0 评论 -
C++ Primer(第五版) 8.2.1--8.2.2节练习
8.4 代码:以一行文本为元素#include <iostream>#include <fstream>#include <vector>#include <string>using namespace std;int main(int argc, char *argv[]){ vector<string> fve...原创 2019-05-10 15:18:34 · 462 阅读 · 0 评论 -
C++ Primer(第五版) 9.5.5--9.6节练习
9.50#include <iostream>#include <string>#include <vector>using namespace std;int main(){ vector<string> int_val = {"123", "456", "-789"}; int sum1 = 0; for (au...原创 2019-05-20 16:36:43 · 456 阅读 · 0 评论 -
C++ Primer(第五版) 8.3.1节练习
8.9 代码#include <iostream>#include <sstream>using namespace std;istream& in_out(istream &in){ int temp; while (1) { cin >> temp; /* 到达文件结尾 */ if (in.eof())...原创 2019-05-10 17:42:53 · 268 阅读 · 0 评论 -
C++ Primer(第五版) 8.3.2节练习
8.13 从文件读取数据版本:#include <iostream>#include <sstream>#include <fstream>#include <vector>using namespace std;struct PersonInfo { string name; vector<string> pho...原创 2019-05-10 18:02:39 · 264 阅读 · 0 评论 -
C++ Primer(第五版) 11.1--11.2.1节练习
11.1 map是关联容器,其中的元素是按关键字来保存和访问的,支持高效的关键字查找和访问; vector是顺序容器,元素是按它们在容器种的位置顺序保存和访问的。11.2 如果程序要在容器中间插入或删除元素,应使用list; ...原创 2019-05-24 11:45:47 · 441 阅读 · 0 评论 -
C++ Primer(第五版) 11.2.2--11.2.3节练习
11.9#include <iostream>#include <fstream>#include <sstream>#include <list>#include <map>#include <string>using namespace std;void trans(string &word...原创 2019-05-24 11:53:24 · 468 阅读 · 0 评论 -
C++ Primer(第五版) 11.3.1--11.3.2节练习
11.15 map<int, vector<int>> //mapped_type:vector<int>; key_type: int; value_type: pair<const int, vector<int>>11.16 map<int, int> m;auto it = ...原创 2019-05-24 16:11:01 · 465 阅读 · 0 评论 -
C++ Primer(第五版) 10.1--10.2.1节练习
10.1 #include <iostream>#include <vector>#include <algorithm>using namespace std;int main(){ vector<int> ivec; int val; cout << "input some numbers: " &l...原创 2019-05-21 11:25:51 · 409 阅读 · 0 评论 -
C++ Primer(第五版)10.2.2--10.2.3节练习
10.6 #include <iostream>#include <algorithm>#include <vector>using namespace std;int main(){ vector<int> ivec = {1, 2, 3, 4, 5}; fill_n(ivec.begin(), ivec.size(...原创 2019-05-21 15:34:43 · 538 阅读 · 0 评论 -
C++ Primer(第五版) 9.5.2节练习
9.43#include <iostream>#include <string>using namespace std;void str_rep(string &s, const string oldVal, string newVal){ auto len = oldVal.size(); if (s.size() < len) ...原创 2019-05-16 14:12:09 · 349 阅读 · 0 评论 -
C++ Primer(第五版) 9.5.3节练习
9.47#include <iostream>#include <string>using namespace std;void find_char(string &s, const string &chars){ string::size_type pos = 0; while ((pos = s.find_first_of(cha...原创 2019-05-16 15:25:52 · 383 阅读 · 1 评论 -
C++ Primer(第五版) 10.3.1--10.3.2节练习
10.11 #include <iostream>#include <algorithm>#include <vector>using namespace std;bool isShorter(const string &s1, const string &s2){ return s1.size() < s2.s...原创 2019-05-21 17:42:41 · 392 阅读 · 0 评论 -
C++ Primer(第五版) 10.3.3--10.3.4节练习
10.20 #include <iostream>#include <algorithm>#include <vector>using namespace std;int larger(vector<string> &words, vector<string>::size_type sz) { int ...原创 2019-05-22 13:27:45 · 375 阅读 · 0 评论
分享