![](https://img-blog.csdnimg.cn/20210527163414648.jpg?x-oss-process=image/resize,m_fixed,h_224,w_224)
C++ Primer
文章平均质量分 66
C++ Primer第五版
杨林山小野猪
杨林山前万里长江澄濁浪,天妃庙內千年香火净凡心。
展开
-
Chapter 16: Templates And Generic Programming
Exercises Section 16.1.1Ex16.1编译器用推断出的模板参数生成一个特定版本的函数,这个过程就叫做实例化。Ex16.2#include<iostream>#include<functional>using namespace std;template <typename T> int compare(const T &v1, const T &v2){ if (less<T>() (v1, v原创 2021-06-17 20:05:01 · 79 阅读 · 0 评论 -
Chapter 15:Object-Oriented Programming
Exercises Section 15.2.1Ex15.1一个类的 virtual 成员可以被其派生类重载Ex15.2被 protected 修饰的类成员可以被该类的继承类访问,而被 private 修饰的类成员则不行。Ex15.3class Quote{public: Quote() = default; Quote(const std::string &book, double sales_price): bookNo(book), price(sales_price) {原创 2021-06-15 20:09:08 · 117 阅读 · 4 评论 -
Chapter 14: Overloaded Operations And Conversions
Exercises Section 14.1Ex14.1不同点:重载操作符必须至少有一个类类型或枚举类型的操作数;重载操作符不保证操作数的求值顺序。相同点:有相同的优先级和结合性,操作数的数目不变。Ex14.2class Sales_data{ friend ostream &operator<<(ostream &, const Sales_data &); friend istream &operator>>(istream &am原创 2021-06-11 16:39:09 · 79 阅读 · 0 评论 -
Chapter 13: Copy Control
Exercises Section 13.1.1Ex13.1copy constructor:第一个参数为类类型的引用,任何其他参数都有默认值。在如下情况下使用拷贝构造函数:用 = 定义变量传递一个对象作为参数给非引用类型函数的返回对象的类型为非引用返回类型用括号初始化数组元素或聚合类的成员Ex13.2如下传入的参数类型为非引用,调用此拷贝构造函数时则要求拷贝实参,而拷贝实参又要求调用拷贝构造函数,会陷入无限死循环Sales_data::Sales_data(Sales_data r原创 2021-06-07 12:29:54 · 118 阅读 · 1 评论 -
Chapter12: Dynamic Memory
Exercises Section 12.1.1Ex12.1// b1 和 b2 共享数据,故而 b1 和 b2 都有 4 个元素StrBlob b1;{ Strblob b2 = {"a", "an", "the"}; b1 = b2; b2.push_back("about");}Ex12.2class StrBlob {public: typedef std::vector<std::string>::size_type size_type; St原创 2021-05-31 19:14:07 · 134 阅读 · 0 评论 -
Chapter 11: Associative Containers
Exercises Section 11.1Ex11.1map:关联容器,元素为键值对;map没有push_back;map的元素按照关键字来保存和访问vector:顺序容器,元素为内置类型或者自定义类型;可以对vector调用排序算法;vector是按照元素在容器的位置保存和访问Ex11.2list:在任何位置添加和删除元素vector:随机访问,只能在尾部添加元素deque:随机访问,只能在首尾添加元素map:键值对的集合set:关键字即为值Ex11.3#include<原创 2021-05-28 14:46:53 · 62 阅读 · 0 评论 -
Chapter 10: Generic Algorithms
Exercises Section 10.1Ex10.1#include<iostream>#include<algorithm>#include<vector>using namespace std;int main(){ int value; int number = 15; vector<int> vec; while (cin >> value) vec.push_back(原创 2021-05-27 17:02:32 · 97 阅读 · 0 评论 -
Chapter 9: Sequential Containers
Exercises Section 9.1Ex9.1a) list 可能涉及中间插入操作b) deque 涉及头部删除,尾部插入 c) vector 没有更好的选择Exercises Section 9.2Ex9.2list<deque<int>> lqint;Exercises Section 9.2.1Ex9.31. begin and end refer to elements of or one past the end of原创 2021-05-27 17:00:47 · 72 阅读 · 0 评论 -
Chapter 8: The I/O Library
Exercises Section 8.1.2Ex8.1 8.2#include<iostream>#include<string>std::istream &read_Write(std::istream &is){ std::string s; while (is >> s) std::cout << s << std::endl; is.clear(); return原创 2021-05-27 16:58:54 · 79 阅读 · 0 评论 -
Chapter 7: Classes
Exercises Section 7.1.1Ex7.1#include<iostream>#include<string>struct Sales_data { std::string bookNo; unsigned units_sold = 0; double revenue = 0.0;};int main(){ Sales_data total; if (std::cin >> total.bookNo原创 2021-05-27 16:57:38 · 66 阅读 · 0 评论 -
Chapter 6: Functions
Exercises Section 6.1Ex6.11. 参数是用来传递的,变量是通过定义得到的2. 在调用过程中,有时候需要输入参数,此时参数会有值,可以直接传入;变量只有在赋值后才能使用Ex6.2a)// origin code 返回类型不匹配int f() { string s; // ... return s;}// correctstring f() { string s; // ... return s;}b)// ori原创 2021-05-27 16:55:18 · 86 阅读 · 0 评论 -
Chapter 5: Statements
Exercises Section 5.1Ex5.1; // null statementEx5.2// block{}Exercises Section 5.2Ex5.4a) while (string::iterator iter != s.end()) { /*... */ } // 未初始化 iter 的值// 修改如下string::iterator iter = s.begin();while (iter != s.end()) { /*... */ }b)原创 2021-05-27 16:53:13 · 70 阅读 · 0 评论 -
Chapter 4: Expressions
Exercises Section 4.1.2Ex4.15 + 10 * 20 / 2 = 105Ex4.2a) *(vec.begin())b) (*(vec.begin())) + 1Exercises Section 4.2Ex4.4((12 / 3) * 4) + (5 * 15) + ((24 % 4) / 2) = 91Ex4.5a) -30 * 3 + 21 / 5 = -86b) -30 + 3 * 21 / 5 = -18c) 30 / 3 * 21 % 5原创 2021-05-27 16:51:37 · 128 阅读 · 0 评论 -
Chapter 3: Strings、Vectors And Arrays
Exercises Section 3.2.2Ex3.2// 一次读取一行#include<iostream>#include<string>using namespace std;int main(){ string line; while (getline(cin, line)) { cout << line << endl; } system("pause"); retu原创 2021-05-27 16:49:45 · 84 阅读 · 0 评论 -
Chapter 2: Variables And Basic Types
Exercise Section 2.1.1Ex2.1一个 long long 类型至少和 一个 long 类型一样大;一个 long 类型至少和一个 int 类型一样大;一个 int 类型至少和一个 short 类型一样大。unsigned 类型不存在负数;signed 类型存在负数。一个 double 类型至少和一个 float 类型一样大。Exercises Section 2.1.2Ex2.3 2.4Exercises Section 2.1.3Ex2.5a) 'a'原创 2021-05-27 16:46:21 · 73 阅读 · 0 评论 -
Chapter 1: Getting Started
Preface使用的书籍为《C++ Primer》第五版 英文版Exercises Section 1.1Ex1.2Exercises Section 1.2Ex1.3Ex1.4Ex1.5Ex1.6v1 后面多了一个冒号Exercises Section 1.3Ex1.7Ex1.8 std::cout << "/*"; // 合法 std::cout << "*/"; // 合法 std::cout &l原创 2021-05-27 16:42:36 · 99 阅读 · 0 评论