├── .travis.yml
├── LICENSE
├── README.md
├── cpp_source
├── ch01
│ ├── Sales_item.h
│ ├── ch01.cpp
│ └── data
│ │ ├── add
│ │ ├── add_item
│ │ ├── book_sales
│ │ ├── mysum
│ │ └── occurs
├── ch02
│ ├── Sales_item.h
│ └── ch02.cpp
├── ch03
│ └── ch3.cpp
├── ch04
│ └── ch04.cpp
├── ch05
│ └── ch5.cpp
├── ch06
│ ├── Chapter6.h
│ ├── ch6.cpp
│ ├── fact.cpp
│ └── factMain.cpp
├── ch07
│ ├── ex_7_41
│ ├── ex_7_41.cpp
│ ├── ex_7_41.h
│ ├── ex_7_41_main.cpp
│ ├── screen
│ ├── screen.cpp
│ └── screen.h
├── ch08
│ └── ch8.cpp
├── ch09
│ ├── date
│ └── date.cpp
├── ch10
│ ├── biggies
│ ├── biggies.cpp
│ ├── inserter
│ └── inserter.cpp
├── ch11
│ ├── ex_11_3
│ ├── ex_11_3.cpp
│ ├── ex_11_4
│ └── ex_11_4.cpp
├── ch12
│ ├── ex_12_27
│ ├── ex_12_27.cpp
│ ├── ex_12_27.h
│ ├── ex_12_27_main.cpp
│ └── storyDataFile.txt
├── ch13
│ ├── ex_13_13
│ ├── ex_13_13.cpp
│ ├── ex_13_34_36_37.cpp
│ └── ex_13_34_36_37.h
├── ch14
│ ├── ex_14_44
│ └── ex_14_44.cpp
├── ch15
│ ├── ex_15_26
│ │ ├── bulk_quote.cpp
│ │ ├── bulk_quote.h
│ │ ├── disc_quote.cpp
│ │ ├── disc_quote.h
│ │ ├── limit_quote.cpp
│ │ ├── limit_quote.h
│ │ ├── main.cpp
│ │ ├── quote.cpp
│ │ └── quote.h
│ └── text_query
│ │ ├── StrBlob.h
│ │ ├── andquery.cpp
│ │ ├── andquery.h
│ │ ├── binaryquery.cpp
│ │ ├── binaryquery.h
│ │ ├── main.cpp
│ │ ├── notquery.cpp
│ │ ├── notquery.h
│ │ ├── orquery.cpp
│ │ ├── orquery.h
│ │ ├── query.cpp
│ │ ├── query.h
│ │ ├── query_base.cpp
│ │ ├── query_base.h
│ │ ├── queryresult.cpp
│ │ ├── queryresult.h
│ │ ├── storyDataFile.txt
│ │ ├── textquery.cpp
│ │ ├── textquery.h
│ │ ├── wordquery.cpp
│ │ └── wordquery.h
├── ch16
│ ├── ex_16_51
│ └── ex_16_51.cpp
└── ch17
│ ├── ex_17_4.cpp
│ ├── ex_17_4_SalesData.cpp
│ └── ex_17_4_SalesData.h
├── excersize
├── ch01.md
├── ch02.md
├── ch03.md
├── ch04.md
├── ch05.md
├── ch06.md
├── ch07.md
├── ch08.md
├── ch09.md
├── ch10.md
├── ch11.md
├── ch12.md
├── ch13.md
├── ch14.md
├── ch15.md
├── ch16.md
├── ch17.md
├── ch18.md
└── ch19.md
└── run.sh
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: cpp
2 | script: true
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 一步一个脚印
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |

2 | cpp-primer-learning
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | Solutions for C++ *Primer* 5th exercises.
11 | ## Introduction
12 | This project is used to help people who want to learn C++ language by themselves.
13 |
14 | I'm a C + + beginner and the code can be problematic. If you find a mistake, you can write an issue [here](https://github.com/codingboylj/cpp-primer-learning/issues/new) and I'll fix it as soon as I can.
15 | ## Download e-books or Source Code
16 |
17 | You can get it from the relase:https://github.com/ybygjylj/cpp-primer-learning/releases/tag/1.0
18 |
19 | ## My system environment
20 | | My system environment | |
21 | | --------------------- | ----------------------------- |
22 | | Kernel | x86_64 Linux 5.6.19-2-MANJARO |
23 | | g++ version | 5.5.0 |
24 | | editor | neovim 0.4.3 |
25 |
26 | ## Run c++ code quickly on linux
27 | In Linux, compiling a CPP file from the command line and running it requires several commands to execute, so I created a shell to quickly compile and run the CPP code. You can execute the CPP file with the run statement by adding the alias directive definition.(in ~/.zshrc or ~/.bashrc)
28 |
29 | ```
30 | alias run="~/Documents/c++/run.sh"
31 | ```
32 |
33 | When you want to perform the test.cpp, you can execute the command:
34 |
35 | ```
36 | run test.cpp
37 | ```
38 |
39 | If your code is correct, it will run directly, otherwise an error will be thrown.
40 |
41 |
--------------------------------------------------------------------------------
/cpp_source/ch01/Sales_item.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
3 | * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
4 | * copyright and warranty notices given in that book:
5 | *
6 | * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
7 | *
8 | *
9 | * "The authors and publisher have taken care in the preparation of this book,
10 | * but make no expressed or implied warranty of any kind and assume no
11 | * responsibility for errors or omissions. No liability is assumed for
12 | * incidental or consequential damages in connection with or arising out of the
13 | * use of the information or programs contained herein."
14 | *
15 | * Permission is granted for this code to be used for educational purposes in
16 | * association with the book, given proper citation if and when posted or
17 | * reproduced.Any commercial use of this code requires the explicit written
18 | * permission of the publisher, Addison-Wesley Professional, a division of
19 | * Pearson Education, Inc. Send your request for permission, stating clearly
20 | * what code you would like to use, and in what specific way, to the following
21 | * address:
22 | *
23 | * Pearson Education, Inc.
24 | * Rights and Permissions Department
25 | * One Lake Street
26 | * Upper Saddle River, NJ 07458
27 | * Fax: (201) 236-3290
28 | */
29 |
30 | /* This file defines the Sales_item class used in chapter 1.
31 | * The code used in this file will be explained in
32 | * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
33 | * Readers shouldn't try to understand the code in this file
34 | * until they have read those chapters.
35 | */
36 |
37 | #ifndef SALESITEM_H
38 | // we're here only if SALESITEM_H has not yet been defined
39 | #define SALESITEM_H
40 |
41 | // Definition of Sales_item class and related functions goes here
42 | #include
43 | #include
44 |
45 | class Sales_item {
46 | // these declarations are explained section 7.2.1, p. 270
47 | // and in chapter 14, pages 557, 558, 561
48 | friend std::istream& operator>>(std::istream&, Sales_item&);
49 | friend std::ostream& operator<<(std::ostream&, const Sales_item&);
50 | friend bool operator<(const Sales_item&, const Sales_item&);
51 | friend bool
52 | operator==(const Sales_item&, const Sales_item&);
53 | public:
54 | // constructors are explained in section 7.1.4, pages 262 - 265
55 | // default constructor needed to initialize members of built-in type
56 | Sales_item() = default;
57 | Sales_item(const std::string &book): bookNo(book) { }
58 | Sales_item(std::istream &is) { is >> *this; }
59 | public:
60 | // operations on Sales_item objects
61 | // member binary operator: left-hand operand bound to implicit this pointer
62 | Sales_item& operator+=(const Sales_item&);
63 |
64 | // operations on Sales_item objects
65 | std::string isbn() const { return bookNo; }
66 | double avg_price() const;
67 | // private members as before
68 | private:
69 | std::string bookNo; // implicitly initialized to the empty string
70 | unsigned units_sold = 0; // explicitly initialized
71 | double revenue = 0.0;
72 | };
73 |
74 | // used in chapter 10
75 | inline
76 | bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
77 | { return lhs.isbn() == rhs.isbn(); }
78 |
79 | // nonmember binary operator: must declare a parameter for each operand
80 | Sales_item operator+(const Sales_item&, const Sales_item&);
81 |
82 | inline bool
83 | operator==(const Sales_item &lhs, const Sales_item &rhs)
84 | {
85 | // must be made a friend of Sales_item
86 | return lhs.units_sold == rhs.units_sold &&
87 | lhs.revenue == rhs.revenue &&
88 | lhs.isbn() == rhs.isbn();
89 | }
90 |
91 | inline bool
92 | operator!=(const Sales_item &lhs, const Sales_item &rhs)
93 | {
94 | return !(lhs == rhs); // != defined in terms of operator==
95 | }
96 |
97 | // assumes that both objects refer to the same ISBN
98 | Sales_item& Sales_item::operator+=(const Sales_item& rhs)
99 | {
100 | units_sold += rhs.units_sold;
101 | revenue += rhs.revenue;
102 | return *this;
103 | }
104 |
105 | // assumes that both objects refer to the same ISBN
106 | Sales_item
107 | operator+(const Sales_item& lhs, const Sales_item& rhs)
108 | {
109 | Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
110 | ret += rhs; // add in the contents of (|rhs|)
111 | return ret; // return (|ret|) by value
112 | }
113 |
114 | std::istream&
115 | operator>>(std::istream& in, Sales_item& s)
116 | {
117 | double price;
118 | in >> s.bookNo >> s.units_sold >> price;
119 | // check that the inputs succeeded
120 | if (in)
121 | s.revenue = s.units_sold * price;
122 | else
123 | s = Sales_item(); // input failed: reset object to default state
124 | return in;
125 | }
126 |
127 | std::ostream&
128 | operator<<(std::ostream& out, const Sales_item& s)
129 | {
130 | out << s.isbn() << " " << s.units_sold << " "
131 | << s.revenue << " " << s.avg_price();
132 | return out;
133 | }
134 |
135 | double Sales_item::avg_price() const
136 | {
137 | if (units_sold)
138 | return revenue/units_sold;
139 | else
140 | return 0;
141 | }
142 | #endif
143 |
--------------------------------------------------------------------------------
/cpp_source/ch01/ch01.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by cer
3 | // chapter 1
4 | // 开始
5 |
6 | #include
7 | #include "Sales_item.h"
8 |
9 | void basic_io(){
10 | std::cout << "Enter two numbers:" << std::endl;
11 | int v1, v2;
12 | std::cin >> v1 >> v2;
13 | std::cout << "The sum of " << v1 << " and " << v2
14 | << " is " << v1 + v2 << std::endl;
15 | }
16 |
17 | void basic_while(){
18 | int sum = 0, val = 1;
19 | while (val <= 10){
20 | sum += val;
21 | ++val;
22 | }
23 | std::cout << "Sum of 1 to 10 inclusive is "
24 | << sum << std::endl;
25 | }
26 |
27 | void basic_for(){
28 | int sum = 0;
29 | for (int val = 1; val <= 10; ++val){
30 | sum += val;
31 | }
32 | std::cout << "Sum of 1 to 10 inclusive is "
33 | << sum << std::endl;
34 | }
35 |
36 | void basic_if(){
37 | std::cout << "Enter two numbers:" << std::endl;
38 | int v1, v2;
39 | std::cin >> v1 >> v2;
40 | int lower, upper;
41 | if (v1<=v2){
42 | lower = v1;
43 | upper = v2;
44 | } else {
45 | lower = v2;
46 | upper = v1;
47 | }
48 | int sum = 0;
49 | for (int val = lower; val <= upper; ++val)
50 | sum += val;
51 | std::cout << "Sum of " << lower
52 | << " to " << upper
53 | << " inclusive is "
54 | << sum << std::endl;
55 | }
56 |
57 | void basic_cin(){
58 | int sum = 0, value;
59 | while (std::cin >> value)
60 | sum += value;
61 | std::cout << "Sum is: " << sum << std::endl;
62 | }
63 |
64 | void q_1_3(){
65 | std::cout << "Hello, World" << std::endl;
66 | }
67 |
68 | void q_1_4(){
69 | std::cout << "Enter two numbers:" << std::endl;
70 | int v1 = 0, v2 = 0;
71 | std::cin >> v1 >> v2;
72 | std::cout << "The product of " << v1 << " and " << v2
73 | << " is " << v1 * v2 << std::endl;
74 | }
75 |
76 | void q_1_5(){
77 | std::cout << "Enter two numbers:" << std::endl;
78 | int v1 = 0, v2 = 0;
79 | std::cin >> v1 >> v2;
80 | std::cout << "The product of ";
81 | std::cout << v1;
82 | std::cout << " and ";
83 | std::cout << v2;
84 | std::cout << " is ";
85 | std::cout << v1 * v2;
86 | std::cout << std::endl;
87 | }
88 |
89 | void q_1_9(){
90 | int sum = 0, val = 50;
91 | while (val <= 100){
92 | sum += val;
93 | val += 1;
94 | }
95 | std::cout << "Sum of 50 to 100 inclusive is "
96 | << sum << std::endl;
97 | }
98 |
99 | void q_1_10(){
100 | int val = 10;
101 | while (val >= 0){
102 | std::cout << val << " ";
103 | val -= 1;
104 | }
105 | std::cout << std::endl;
106 | }
107 |
108 | void q_1_11(){
109 | int start = 0, end = 0;
110 | std::cout << "Please input two num: ";
111 | std::cin >> start >> end;
112 | if (start <= end) {
113 | while (start <= end){
114 | std::cout << start << " ";
115 | ++start;
116 | }
117 | std::cout << std::endl;
118 | }
119 | else{
120 | std::cout << "start should be smaller than end !!!";
121 | }
122 | }
123 |
124 | void q_1_16(){
125 | int sum = 0;
126 | for (int value = 0; std::cin >> value; )
127 | sum += value;
128 | std::cout << sum << std::endl;
129 | }
130 |
131 | void count_num(){
132 | // 统计输入中每个值连续出现了多少次
133 | int currVal = 0, val = 0;
134 | if (std::cin >> currVal){
135 | int cnt = 1;
136 | while (std::cin >> val){
137 | if (val == currVal)
138 | ++cnt;
139 | else {
140 | std::cout << currVal << " occurs "
141 | << cnt << " times " << std::endl;
142 | currVal = val;
143 | cnt = 1;
144 | }
145 | }
146 | std::cout << currVal << " occurs "
147 | << cnt << " times " << std::endl;
148 | }
149 | }
150 |
151 | void q_1_20(){
152 | for (Sales_item item; std::cin >> item; std::cout << item << std::endl);
153 | }
154 |
155 | void q_1_21(){
156 | Sales_item item_1;
157 | Sales_item item_2;
158 | std::cin >> item_1;
159 | std::cout << item_1 << std::endl;
160 | std::cin >> item_2;
161 | std::cout << item_2 << std::endl;
162 | std::cout << "sum of sale items: " << item_1 + item_2 << std::endl;
163 | }
164 |
165 | void q_1_22(){
166 | Sales_item sum_item;
167 | std::cin >> sum_item;
168 | std::cout << sum_item << std::endl;
169 | for (Sales_item item; std::cin >> item; std::cout << item << std::endl){
170 | sum_item += item;
171 | }
172 | std::cout << "sum of sale items: " << sum_item << std::endl;
173 | }
174 |
175 | void q_1_23(){
176 | Sales_item total;
177 | if (std::cin >> total){
178 | Sales_item trans;
179 | while (std::cin >> trans){
180 | if (total.isbn() == trans.isbn()) {
181 | total += trans;
182 | }
183 | else {
184 | std::cout << total << std::endl;
185 | total = trans;
186 | }
187 | }
188 | std::cout << total << std::endl;
189 | }
190 | else {
191 | std::cerr << "No data?!" << std::endl;
192 | return;
193 | }
194 | }
195 |
196 | int main() {
197 | // basic_io();
198 | // basic_while();
199 | // basic_for();
200 | // basic_if();
201 | // basic_cin();
202 | // q_1_3();
203 | // q_1_4();
204 | // q_1_5();
205 | // /* 正常注释 /* 嵌套注释 */ 正常注释*/
206 | // std::cout << /* "*/" /* "/*" */;
207 | // q_1_9();
208 | // q_1_10();
209 | // q_1_11();
210 | // q_1_16();
211 | // count_num();
212 | // q_1_20();
213 | // q_1_21();
214 | // q_1_22();
215 | q_1_23();
216 | return 0;
217 | }
--------------------------------------------------------------------------------
/cpp_source/ch01/data/add:
--------------------------------------------------------------------------------
1 | 3 7
2 |
--------------------------------------------------------------------------------
/cpp_source/ch01/data/add_item:
--------------------------------------------------------------------------------
1 | 0-201-78345-X 3 20.00
2 | 0-201-78345-X 2 25.00
3 |
--------------------------------------------------------------------------------
/cpp_source/ch01/data/book_sales:
--------------------------------------------------------------------------------
1 | 0-201-70353-X 4 24.99
2 | 0-201-82470-1 4 45.39
3 | 0-201-88954-4 2 15.00
4 | 0-201-88954-4 5 12.00
5 | 0-201-88954-4 7 12.00
6 | 0-201-88954-4 2 12.00
7 | 0-399-82477-1 2 45.39
8 | 0-399-82477-1 3 45.39
9 | 0-201-78345-X 3 20.00
10 | 0-201-78345-X 2 25.00
11 |
--------------------------------------------------------------------------------
/cpp_source/ch01/data/mysum:
--------------------------------------------------------------------------------
1 | 3 4 5 6
2 |
--------------------------------------------------------------------------------
/cpp_source/ch01/data/occurs:
--------------------------------------------------------------------------------
1 | 42 42 42 42 42 55 55 62 100 100 100
2 |
--------------------------------------------------------------------------------
/cpp_source/ch02/Sales_item.h:
--------------------------------------------------------------------------------
1 | /*
2 | * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
3 | * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
4 | * copyright and warranty notices given in that book:
5 | *
6 | * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
7 | *
8 | *
9 | * "The authors and publisher have taken care in the preparation of this book,
10 | * but make no expressed or implied warranty of any kind and assume no
11 | * responsibility for errors or omissions. No liability is assumed for
12 | * incidental or consequential damages in connection with or arising out of the
13 | * use of the information or programs contained herein."
14 | *
15 | * Permission is granted for this code to be used for educational purposes in
16 | * association with the book, given proper citation if and when posted or
17 | * reproduced.Any commercial use of this code requires the explicit written
18 | * permission of the publisher, Addison-Wesley Professional, a division of
19 | * Pearson Education, Inc. Send your request for permission, stating clearly
20 | * what code you would like to use, and in what specific way, to the following
21 | * address:
22 | *
23 | * Pearson Education, Inc.
24 | * Rights and Permissions Department
25 | * One Lake Street
26 | * Upper Saddle River, NJ 07458
27 | * Fax: (201) 236-3290
28 | */
29 |
30 | /* This file defines the Sales_item class used in chapter 1.
31 | * The code used in this file will be explained in
32 | * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
33 | * Readers shouldn't try to understand the code in this file
34 | * until they have read those chapters.
35 | */
36 |
37 | #ifndef SALESITEM_H
38 | // we're here only if SALESITEM_H has not yet been defined
39 | #define SALESITEM_H
40 |
41 | // Definition of Sales_item class and related functions goes here
42 | #include
43 | #include
44 |
45 | class Sales_item {
46 | // these declarations are explained section 7.2.1, p. 270
47 | // and in chapter 14, pages 557, 558, 561
48 | friend std::istream& operator>>(std::istream&, Sales_item&);
49 | friend std::ostream& operator<<(std::ostream&, const Sales_item&);
50 | friend bool operator<(const Sales_item&, const Sales_item&);
51 | friend bool
52 | operator==(const Sales_item&, const Sales_item&);
53 | public:
54 | // constructors are explained in section 7.1.4, pages 262 - 265
55 | // default constructor needed to initialize members of built-in type
56 | Sales_item() = default;
57 | Sales_item(const std::string &book): bookNo(book) { }
58 | Sales_item(std::istream &is) { is >> *this; }
59 | public:
60 | // operations on Sales_item objects
61 | // member binary operator: left-hand operand bound to implicit this pointer
62 | Sales_item& operator+=(const Sales_item&);
63 |
64 | // operations on Sales_item objects
65 | std::string isbn() const { return bookNo; }
66 | double avg_price() const;
67 | // private members as before
68 | private:
69 | std::string bookNo; // implicitly initialized to the empty string
70 | unsigned units_sold = 0; // explicitly initialized
71 | double revenue = 0.0;
72 | };
73 |
74 | // used in chapter 10
75 | inline
76 | bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
77 | { return lhs.isbn() == rhs.isbn(); }
78 |
79 | // nonmember binary operator: must declare a parameter for each operand
80 | Sales_item operator+(const Sales_item&, const Sales_item&);
81 |
82 | inline bool
83 | operator==(const Sales_item &lhs, const Sales_item &rhs)
84 | {
85 | // must be made a friend of Sales_item
86 | return lhs.units_sold == rhs.units_sold &&
87 | lhs.revenue == rhs.revenue &&
88 | lhs.isbn() == rhs.isbn();
89 | }
90 |
91 | inline bool
92 | operator!=(const Sales_item &lhs, const Sales_item &rhs)
93 | {
94 | return !(lhs == rhs); // != defined in terms of operator==
95 | }
96 |
97 | // assumes that both objects refer to the same ISBN
98 | Sales_item& Sales_item::operator+=(const Sales_item& rhs)
99 | {
100 | units_sold += rhs.units_sold;
101 | revenue += rhs.revenue;
102 | return *this;
103 | }
104 |
105 | // assumes that both objects refer to the same ISBN
106 | Sales_item
107 | operator+(const Sales_item& lhs, const Sales_item& rhs)
108 | {
109 | Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
110 | ret += rhs; // add in the contents of (|rhs|)
111 | return ret; // return (|ret|) by value
112 | }
113 |
114 | std::istream&
115 | operator>>(std::istream& in, Sales_item& s)
116 | {
117 | double price;
118 | in >> s.bookNo >> s.units_sold >> price;
119 | // check that the inputs succeeded
120 | if (in)
121 | s.revenue = s.units_sold * price;
122 | else
123 | s = Sales_item(); // input failed: reset object to default state
124 | return in;
125 | }
126 |
127 | std::ostream&
128 | operator<<(std::ostream& out, const Sales_item& s)
129 | {
130 | out << s.isbn() << " " << s.units_sold << " "
131 | << s.revenue << " " << s.avg_price();
132 | return out;
133 | }
134 |
135 | double Sales_item::avg_price() const
136 | {
137 | if (units_sold)
138 | return revenue/units_sold;
139 | else
140 | return 0;
141 | }
142 | #endif
143 |
--------------------------------------------------------------------------------
/cpp_source/ch02/ch02.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by cer
3 | // chapter 2
4 | // 变量和基本类型
5 |
6 | #include
7 | #include "Sales_item.h"
8 |
9 | void basic_class(){
10 | Sales_item book;
11 | std::cin >> book;
12 | std::cout << book << std::endl;
13 | }
14 |
15 | void q_2_3(){
16 | unsigned u = 10, u2 = 42;
17 | std::cout << u2 - u << std::endl;
18 | std::cout << u - u2 << std::endl;
19 | int i = 10, i2 = 42;
20 | std::cout << i2 - i << std::endl;
21 | std::cout << i - i2 << std::endl;
22 | std::cout << i - u << std::endl;
23 | std::cout << u - i << std::endl;
24 | }
25 |
26 | // 重写 1.5.1 1.5.2 1.6
27 |
28 | // 1.5.1
29 |
30 | #include
31 | #include
32 |
33 | struct Sale_data
34 | {
35 | std::string bookNo;
36 | unsigned units_sold = 0;
37 | double revenue = 0.0;
38 | };
39 |
40 | int q_1_5_1()
41 | {
42 | Sale_data book;
43 | double price;
44 | std::cin >> book.bookNo >> book.units_sold >> price;
45 | book.revenue = book.units_sold * price;
46 | std::cout << book.bookNo << " " << book.units_sold << " " << book.revenue << " " << price;
47 |
48 | return 0;
49 | }
50 |
51 | // 1.5.2
52 | int q_1_5_2()
53 | {
54 | Sale_data book1, book2;
55 | double price1, price2;
56 | std::cin >> book1.bookNo >> book1.units_sold >> price1;
57 | std::cin >> book2.bookNo >> book2.units_sold >> price2;
58 | book1.revenue = book1.units_sold * price1;
59 | book2.revenue = book2.units_sold * price2;
60 |
61 | if (book1.bookNo == book2.bookNo)
62 | {
63 | unsigned totalCnt = book1.units_sold + book2.units_sold;
64 | double totalRevenue = book1.revenue + book2.revenue;
65 | std::cout << book1.bookNo << " " << totalCnt << " " << totalRevenue << " ";
66 | if (totalCnt != 0)
67 | std::cout << totalRevenue / totalCnt << std::endl;
68 | else
69 | std::cout << "(no sales)" << std::endl;
70 | return 0;
71 | }
72 | else
73 | {
74 | std::cerr << "Data must refer to same ISBN" << std::endl;
75 | return -1; // indicate failure
76 | }
77 | }
78 |
79 | // 1.6
80 | int q_1_6()
81 | {
82 | Sale_data total;
83 | double totalPrice;
84 | if (std::cin >> total.bookNo >> total.units_sold >> totalPrice)
85 | {
86 | total.revenue = total.units_sold * totalPrice;
87 |
88 | Sale_data trans;
89 | double transPrice;
90 | while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice)
91 | {
92 | trans.revenue = trans.units_sold * transPrice;
93 |
94 | if (total.bookNo == trans.bookNo)
95 | {
96 | total.units_sold += trans.units_sold;
97 | total.revenue += trans.revenue;
98 | }
99 | else
100 | {
101 | std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
102 | if (total.units_sold != 0)
103 | std::cout << total.revenue / total.units_sold << std::endl;
104 | else
105 | std::cout << "(no sales)" << std::endl;
106 |
107 | total.bookNo = trans.bookNo;
108 | total.units_sold = trans.units_sold;
109 | total.revenue = trans.revenue;
110 | }
111 | }
112 |
113 | std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
114 | if (total.units_sold != 0)
115 | std::cout << total.revenue / total.units_sold << std::endl;
116 | else
117 | std::cout << "(no sales)" << std::endl;
118 |
119 | return 0;
120 | }
121 | else
122 | {
123 | std::cerr << "No data?!" << std::endl;
124 | return -1; // indicate failure
125 | }
126 | }
127 |
128 |
129 | int main(){
130 | // q_2_3();
131 | // q_1_5_1();
132 | // q_1_5_2();
133 | q_1_6();
134 | return 0;
135 | }
136 |
137 |
--------------------------------------------------------------------------------
/cpp_source/ch03/ch3.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by cer
3 | // chapter 3
4 | // 字符串、向量和数组
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | using std::cin;
13 | using std::cout;
14 | using std::endl;
15 | using std::string;
16 | using std::vector;
17 | using std::bitset;
18 |
19 | void basic_getline(){
20 | string line;
21 | while (getline(cin, line))
22 | cout << line << endl;
23 | }
24 |
25 | void basic_string(){
26 | string s = "Test String.";
27 | cout << "Size: " << s.size() << endl
28 | << "Empty: " << s.empty() << endl
29 | << "char at index 0:" << s[0] << endl;
30 | for(string::size_type i=0; i v;
38 | for (int i = 0; i != 10; i++){
39 | v.push_back(i);
40 | }
41 | for (vector::iterator iter = v.begin();
42 | iter != v.end(); ++iter){
43 | *iter = 0;
44 | }
45 | // 不能直接输出vector
46 | }
47 |
48 | void basic_bieset(){
49 | bitset<16> bitvec1(0xffff);
50 | string bit_str = "1100";
51 | bitset<32> bitvec2(bit_str); //从右到左读取字符串,只能是string对象,不能是字面值
52 | bitset<16> bitvec3(bit_str, 0, 3); // 只要前三位
53 | cout << bitvec1 << endl
54 | << bitvec2 << endl
55 | << bitvec3 << endl
56 | << bitvec2.test(3) <<" " < v;
62 | int i;
63 | while (cin >> i){
64 | v.push_back(i);
65 | }
66 | }
67 |
68 | void q_3_17(){
69 | vector v;
70 | string s;
71 | while (cin >> s){
72 | v.push_back(s);
73 | }
74 | for (auto &str : v)
75 | {
76 | for (auto &c : str)
77 | {
78 | c = toupper(c);
79 | }
80 | }
81 | for (auto str : v){
82 | cout << str << endl;
83 | }
84 | }
85 |
86 |
87 | void q_3_23(){
88 | vector v(10, 1);
89 | for (auto it=v.begin(); it!=v.end(); it++){
90 | *it *= 2;
91 | }
92 | for (auto one : v){
93 | cout << one <
6 |
7 | using namespace std;
8 |
9 | int q_4_28()
10 | {
11 | cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl << endl;
12 |
13 | cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
14 | cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
15 | cout << "char16_t:\t" << sizeof(char16_t) << " bytes" << endl;
16 | cout << "char32_t:\t" << sizeof(char32_t) << " bytes" << endl << endl;
17 |
18 | cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
19 | cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
20 | cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
21 | cout << "long long:\t" << sizeof(long long) << " bytes" << endl << endl;
22 |
23 | cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
24 | cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
25 | cout << "long double:\t" << sizeof(long double) << " bytes" << endl << endl;
26 |
27 | return 0;
28 | }
29 |
30 | int main(){
31 | q_4_28();
32 | }
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/cpp_source/ch05/ch5.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by cer on 17-9-18.
3 | // chapter 5
4 | // 语句
5 |
6 | #include
7 | #include
8 | using namespace std;
9 |
10 | int divide(int a, int b){
11 | if (b == 0){
12 | // 抛出异常
13 | throw runtime_error("b cannot be 0!");
14 | }
15 | else{
16 | return a / b;
17 | }
18 | }
19 |
20 | int main(){
21 | int a = 1, b = 0, res;
22 | try{
23 | res = divide(a, b);
24 | cout << res << endl;
25 | }catch(runtime_error err){
26 | cout << err.what() << endl;
27 | }
28 | return 0;
29 | }
30 |
31 |
32 |
--------------------------------------------------------------------------------
/cpp_source/ch06/Chapter6.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by cer on 19-1-20.
3 | //
4 |
5 | #ifndef CPP_PRIMER_PRACTICE_CHAPTER6_H
6 | #define CPP_PRIMER_PRACTICE_CHAPTER6_H
7 |
8 | int fact(int val);
9 | int func();
10 |
11 | template
12 | T abs(T i)
13 | {
14 | return i >= 0 ? i : -i;
15 | }
16 |
17 | #endif //CPP_PRIMER_PRACTICE_CHAPTER6_H
18 |
--------------------------------------------------------------------------------
/cpp_source/ch06/ch6.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by cer on 17-9-19.
3 | // chapter 06
4 | // 函数
5 |
6 | #include
7 | #include
8 |
9 | using namespace std
10 |
11 | int fact(int i)
12 | {
13 | return i > 1 ? i * fact(i - 1) : 1;
14 | }
15 |
16 | void interactive_fact()
17 | {
18 | string const prompt = "Enter a number within [1, 13) :\n";
19 | string const out_of_range = "Out of range, please try again.\n";
20 | for (int i; cout << prompt, cin >> i; )
21 | {
22 | if (i < 1 || i > 12)
23 | {
24 | cout << out_of_range;
25 | continue;
26 | }
27 | cout << fact(i) << endl;
28 | }
29 | }
30 |
31 | bool str_subrange(const string &str1, const string &str2){
32 | if(str1.size()==str2.size())
33 | return str1==str2;
34 | string::size_type size={min(str1.size(),str2.size())};
35 | string::size_type i=0;
36 | while(i!=size){
37 | if(str1[i]!=str2[i])
38 | return ; //error! no return value!
39 | }
40 | }
41 |
42 | int main()
43 | {
44 | // interactive_fact();
45 | str_subrange();
46 | return 0;
47 | }
--------------------------------------------------------------------------------
/cpp_source/ch06/fact.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by cer on 19-1-20.
3 | //
4 |
5 | #include "Chapter6.h"
6 | #include
7 |
8 | int fact(int val)
9 | {
10 | if (val == 0 || val == 1) return 1;
11 | else return val * fact(val-1);
12 | }
13 |
14 | int func()
15 | {
16 | int n, ret = 1;
17 | std::cout << "input a number: ";
18 | std::cin >> n;
19 | while (n > 1) ret *= n--;
20 | return ret;
21 | }
22 |
--------------------------------------------------------------------------------
/cpp_source/ch06/factMain.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // Created by cer on 19-1-20.
3 | //
4 |
5 |
6 | #include "Chapter6.h"
7 | #include
8 |
9 | int main()
10 | {
11 | std::cout << "5! is " << fact(5) << std::endl;
12 | std::cout << func() << std::endl;
13 | std::cout << abs(-9.78) << std::endl;
14 | }
15 |
--------------------------------------------------------------------------------
/cpp_source/ch07/ex_7_41:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/stepbystepcode/cpp-primer-learning/a07f5c3f9cb1faa2a489bdd0f2067fe098b1abcf/cpp_source/ch07/ex_7_41
--------------------------------------------------------------------------------
/cpp_source/ch07/ex_7_41.cpp:
--------------------------------------------------------------------------------
1 | #include "ex_7_41.h"
2 |
3 | // constructor
4 | Sales_data::Sales_data(std::istream &is) : Sales_data()
5 | {
6 | std::cout << "Sales_data(istream &is)" << std::endl;
7 | read(is, *this);
8 | }
9 |
10 | // member functions.
11 | Sales_data& Sales_data::combine(const Sales_data& rhs)
12 | {
13 | units_sold += rhs.units_sold;
14 | revenue += rhs.revenue;
15 | return *this;
16 | }
17 |
18 | // friend functions
19 | std::istream &read(std::istream &is, Sales_data &item)
20 | {
21 | double price = 0;
22 | is >> item.bookNo >> item.units_sold >> price;
23 | item.revenue = price * item.units_sold;
24 | return is;
25 | }
26 |
27 | std::ostream &print(std::ostream &os, const Sales_data &item)
28 | {
29 | os << item.isbn() << " " << item.units_sold << " " << item.revenue;
30 | return os;
31 | }
32 |
33 | Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
34 | {
35 | Sales_data sum = lhs;
36 | sum.combine(rhs);
37 | return sum;
38 | }
39 |
--------------------------------------------------------------------------------
/cpp_source/ch07/ex_7_41.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_41_h
2 | #define CP5_ex7_41_h
3 |
4 | #include
5 | #include
6 |
7 | class Sales_data {
8 | friend std::istream &read(std::istream &is, Sales_data &item);
9 | friend std::ostream &print(std::ostream &os, const Sales_data &item);
10 | friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
11 |
12 | public:
13 | Sales_data(const std::string &s, unsigned n, double p):bookNo(s), units_sold(n), revenue(n*p)
14 | { std::cout << "Sales_data(const std::string&, unsigned, double)" << std::endl; }
15 |
16 | Sales_data() : Sales_data("", 0, 0.0f)
17 | { std::cout << "Sales_data()" << std::endl; }
18 |
19 | Sales_data(const std::string &s) : Sales_data(s, 0, 0.0f)
20 | { std::cout << "Sales_data(const std::string&)" << std::endl; }
21 |
22 | Sales_data(std::istream &is);
23 |
24 | std::string isbn() const { return bookNo; }
25 | Sales_data& combine(const Sales_data&);
26 |
27 | private:
28 | inline double avg_price() const;
29 |
30 | private:
31 | std::string bookNo;
32 | unsigned units_sold = 0;
33 | double revenue = 0.0;
34 | };
35 |
36 | inline
37 | double Sales_data::avg_price() const
38 | {
39 | return units_sold ? revenue/units_sold : 0;
40 | }
41 |
42 | // declarations for nonmember parts of the Sales_data interface.
43 | std::istream &read(std::istream &is, Sales_data &item);
44 | std::ostream &print(std::ostream &os, const Sales_data &item);
45 | Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
46 |
47 | #endif
48 |
--------------------------------------------------------------------------------
/cpp_source/ch07/ex_7_41_main.cpp:
--------------------------------------------------------------------------------
1 | #include "ex_7_41.h"
2 | using std::cout; using std::endl;
3 |
4 | int main()
5 | {
6 | cout << "1. default way: " << endl;
7 | cout << "----------------" << endl;
8 | Sales_data s1;
9 |
10 | cout << "\n2. use std::string as parameter: " << endl;
11 | cout << "----------------" << endl;
12 | Sales_data s2("CPP-Primer-5th");
13 |
14 | cout << "\n3. complete parameters: " << endl;
15 | cout << "----------------" << endl;
16 | Sales_data s3("CPP-Primer-5th", 3, 25.8);
17 |
18 | cout << "\n4. use istream as parameter: " << endl;
19 | cout << "----------------" << endl;
20 | Sales_data s4(std::cin);
21 |
22 | return 0;
23 | }
--------------------------------------------------------------------------------
/cpp_source/ch07/screen:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/stepbystepcode/cpp-primer-learning/a07f5c3f9cb1faa2a489bdd0f2067fe098b1abcf/cpp_source/ch07/screen
--------------------------------------------------------------------------------
/cpp_source/ch07/screen.cpp:
--------------------------------------------------------------------------------
1 | # include "screen.h"
2 |
3 | int main()
4 | {
5 | Screen myScreen(5, 5, 'X');
6 | myScreen.move(4, 0).set('#').display(std::cout);
7 | std::cout << "\n";
8 | myScreen.display(std::cout);
9 | std::cout << "\n";
10 |
11 | return 0;
12 | }
--------------------------------------------------------------------------------
/cpp_source/ch07/screen.h:
--------------------------------------------------------------------------------
1 | #ifndef CH07_screen
2 | #define CH07_screen
3 |
4 | #include
5 | #include
6 |
7 | class Screen {
8 | public:
9 | using pos = std::string::size_type;
10 |
11 | Screen() = default; // 1
12 | Screen(pos ht, pos wd):height(ht), width(wd), contents(ht*wd, ' '){ } // 2
13 | Screen(pos ht, pos wd, char c):height(ht), width(wd), contents(ht*wd, c){ } // 3
14 |
15 | char get() const { return contents[cursor]; }
16 | char get(pos r, pos c) const { return contents[r*width+c]; }
17 | inline Screen& move(pos r, pos c);
18 | inline Screen& set(char c);
19 | inline Screen& set(pos r, pos c, char ch);
20 |
21 | const Screen& display(std::ostream &os) const { do_display(os); return *this; }
22 | Screen& display(std::ostream &os) { do_display(os); return *this; }
23 |
24 | private:
25 | void do_display(std::ostream &os) const { os << contents; }
26 |
27 | private:
28 | pos cursor = 0;
29 | pos height = 0, width = 0;
30 | std::string contents;
31 | };
32 |
33 | inline Screen& Screen::move(pos r, pos c)
34 | {
35 | cursor = r*width + c;
36 | return *this;
37 | }
38 |
39 | inline Screen& Screen::set(char c)
40 | {
41 | contents[cursor] = c;
42 | return *this;
43 | }
44 |
45 | inline Screen& Screen::set(pos r, pos c, char ch)
46 | {
47 | contents[r*width+c] = ch;
48 | return *this;
49 | }
50 |
51 | #endif
52 |
--------------------------------------------------------------------------------
/cpp_source/ch08/ch8.cpp:
--------------------------------------------------------------------------------
1 | // chapter 08
2 | // 标准IO库
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | using namespace std;
9 |
10 | void loading(){
11 | for (int i = 0; i != 11; i++){
12 | cout << "loading " << (i * 10) << "%\r" << flush;
13 | sleep(1);
14 | }
15 | cout << "loaded! " << endl;
16 | }
17 |
18 | void file_io(){
19 | string in_file_name = "test_in_file";
20 | string out_file_name = "test_out_file";
21 | ifstream infile(in_file_name.c_str());
22 | ofstream outfile(out_file_name.c_str());
23 | if (!infile){
24 | cerr << "err: unable to open input file: "
25 | << in_file_name << endl;
26 | }
27 | else{
28 | // 按词读取
29 | // string word;
30 | // while(infile >> word) {
31 | // cout << word << endl;
32 | // }
33 | // 按行读取
34 | string line;
35 | while(getline(infile,line)){
36 | cout << line << endl;
37 | // 按行写入
38 | outfile << line << endl;
39 | }
40 | }
41 |
42 | infile.close();
43 | outfile.close();
44 | }
45 |
46 | int main(){
47 | file_io();
48 | return 0;
49 | }
--------------------------------------------------------------------------------
/cpp_source/ch09/date:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/stepbystepcode/cpp-primer-learning/a07f5c3f9cb1faa2a489bdd0f2067fe098b1abcf/cpp_source/ch09/date
--------------------------------------------------------------------------------
/cpp_source/ch09/date.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using namespace std;
6 | class My_date{
7 | private:
8 | unsigned year, month, day;
9 | public:
10 | My_date(const string &s){
11 |
12 | unsigned tag;
13 | unsigned format;
14 | format = tag = 0;
15 |
16 | // 1/1/1900
17 | if(s.find_first_of("/")!= string :: npos)
18 | {
19 | format = 0x01;
20 | }
21 |
22 | // January 1, 1900 or Jan 1, 1900
23 | if((s.find_first_of(',') >= 4) && s.find_first_of(',')!= string :: npos){
24 | format = 0x10;
25 | }
26 | else{ // Jan 1 1900
27 | if(s.find_first_of(' ') >= 3
28 | && s.find_first_of(' ')!= string :: npos){
29 | format = 0x10;
30 | tag = 1;
31 | }
32 | }
33 |
34 | switch(format){
35 |
36 | case 0x01:
37 | day = stoi(s.substr(0, s.find_first_of("/")));
38 | month = stoi(s.substr(s.find_first_of("/") + 1, s.find_last_of("/")- s.find_first_of("/")));
39 | year = stoi(s.substr(s.find_last_of("/") + 1, 4));
40 |
41 | break;
42 |
43 | case 0x10:
44 | if( s.find("Jan") < s.size() ) month = 1;
45 | if( s.find("Feb") < s.size() ) month = 2;
46 | if( s.find("Mar") < s.size() ) month = 3;
47 | if( s.find("Apr") < s.size() ) month = 4;
48 | if( s.find("May") < s.size() ) month = 5;
49 | if( s.find("Jun") < s.size() ) month = 6;
50 | if( s.find("Jul") < s.size() ) month = 7;
51 | if( s.find("Aug") < s.size() ) month = 8;
52 | if( s.find("Sep") < s.size() ) month = 9;
53 | if( s.find("Oct") < s.size() ) month =10;
54 | if( s.find("Nov") < s.size() ) month =11;
55 | if( s.find("Dec") < s.size() ) month =12;
56 |
57 | char chr = ',';
58 | if(tag == 1){
59 | chr = ' ';
60 | }
61 | day = stoi(s.substr(s.find_first_of("123456789"), s.find_first_of(chr) - s.find_first_of("123456789")));
62 |
63 | year = stoi(s.substr(s.find_last_of(' ') + 1, 4));
64 | break;
65 | }
66 | }
67 |
68 | void print(){
69 | cout << "day:" << day << " " << "month: " << month << " " << "year: " << year;
70 | }
71 | };
72 | int main()
73 | {
74 | My_date d("Jan 1 1900");
75 | d.print();
76 | return 0;
77 | }
--------------------------------------------------------------------------------
/cpp_source/ch10/biggies:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/stepbystepcode/cpp-primer-learning/a07f5c3f9cb1faa2a489bdd0f2067fe098b1abcf/cpp_source/ch10/biggies
--------------------------------------------------------------------------------
/cpp_source/ch10/biggies.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | // from ex 10.9
7 | void elimdups(std::vector &vs)
8 | {
9 | std::sort(vs.begin(), vs.end());
10 | auto new_end = std::unique(vs.begin(), vs.end());
11 | vs.erase(new_end, vs.end());
12 | }
13 |
14 | void biggies(std::vector &vs, std::size_t sz)
15 | {
16 | using std::string;
17 |
18 | elimdups(vs);
19 |
20 | // sort by size, but maintain alphabetical order for same size.
21 | std::stable_sort(vs.begin(), vs.end(), [](string const& lhs, string const& rhs){
22 | return lhs.size() < rhs.size();
23 | });
24 |
25 | // get an iterator to the first one whose size() is >= sz
26 | auto wc = std::find_if(vs.begin(), vs.end(), [sz](string const& s){
27 | return s.size() >= sz;
28 | });
29 |
30 | // print the biggies
31 | std::for_each(wc, vs.end(), [](const string &s){
32 | std::cout << s << " ";
33 | });
34 | }
35 |
36 | int main()
37 | {
38 | // ex10.16
39 | std::vector v
40 | {
41 | "1234","1234","1234","hi~", "alan", "alan", "cp"
42 | };
43 | std::cout << "ex10.16: ";
44 | biggies(v, 3);
45 | std::cout << std::endl;
46 |
47 | return 0;
48 | }
--------------------------------------------------------------------------------
/cpp_source/ch10/inserter:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/stepbystepcode/cpp-primer-learning/a07f5c3f9cb1faa2a489bdd0f2067fe098b1abcf/cpp_source/ch10/inserter
--------------------------------------------------------------------------------
/cpp_source/ch10/inserter.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | using std::list; using std::copy; using std::cout; using std::endl;
7 |
8 | template
9 | void print(Sequence const& seq)
10 | {
11 | for (const auto& i: seq)
12 | std::cout << i << " ";
13 | std::cout << std::endl;
14 | }
15 |
16 | int main()
17 | {
18 | std::vector vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
19 |
20 | // uses inserter
21 | list lst1;
22 | copy(vec.cbegin(), vec.cend(), inserter(lst1, lst1.begin()));
23 | print(lst1);
24 |
25 | // uses back_inserter
26 | list lit2;
27 | copy(vec.cbegin(), vec.cend(), back_inserter(lit2));
28 | print(lit2);
29 |
30 | // uses front_inserter
31 | list lst3;
32 | copy(vec.cbegin(), vec.cend(), front_inserter(lst3));
33 | print(lst3);
34 | }
--------------------------------------------------------------------------------
/cpp_source/ch11/ex_11_3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/stepbystepcode/cpp-primer-learning/a07f5c3f9cb1faa2a489bdd0f2067fe098b1abcf/cpp_source/ch11/ex_11_3
--------------------------------------------------------------------------------
/cpp_source/ch11/ex_11_3.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include