Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 21, 2024. It is now read-only.

Commit 2b1acf3

Browse files
committed
Finish Chapter 11
1 parent 362f796 commit 2b1acf3

19 files changed

+535
-0
lines changed

‎ch11/Sales_data.h‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#ifndef SALES_DATA_H
2+
#define SALES_DATA_H
3+
4+
#include <iostream>
5+
#include <fstream>
6+
#include <string>
7+
8+
struct Sales_data;
9+
std::istream& read(std::istream& is, Sales_data& data);
10+
// Do not need to overload the read() function to take parameter of std::ifstream or std::fstream.
11+
// They inherit from std::istream and polymorphism applies.
12+
13+
class Sales_data {
14+
friend Sales_data add(Sales_data& data1, Sales_data& data2);
15+
friend std::istream& read(std::istream& is, Sales_data& data);
16+
friend std::ostream& print(std::ostream& os, const Sales_data & data);
17+
18+
std::string bookNo;
19+
unsigned units_sold = 0;
20+
double revenue = 0.0;
21+
22+
public:
23+
Sales_data() = default;
24+
Sales_data(const std::string& s, unsigned n, double p) : bookNo(s), units_sold(n), revenue(n * p) {}
25+
Sales_data(const std::string& s) : bookNo(s) {}
26+
Sales_data(std::istream& is);
27+
28+
const std::string isbn() const { return bookNo; }
29+
Sales_data& combine(const Sales_data&);
30+
double avg_price() const;
31+
};
32+
33+
// define constructors
34+
Sales_data::Sales_data(std::istream& is) {
35+
read(is, *this);
36+
}
37+
38+
// define member function
39+
Sales_data& Sales_data::combine(const Sales_data& that) {
40+
units_sold += that.units_sold;
41+
revenue += that.revenue;
42+
return *this;
43+
}
44+
45+
double Sales_data::avg_price() const {
46+
if (units_sold) {
47+
return revenue / units_sold;
48+
}
49+
return 0.0;
50+
}
51+
52+
// define non member functions
53+
Sales_data add(Sales_data& data1, Sales_data& data2) {
54+
Sales_data sum = data1;
55+
sum.combine(data2);
56+
return sum;
57+
}
58+
59+
std::istream& read(std::istream& is, Sales_data& data) {
60+
double price;
61+
is >> data.bookNo >> data.units_sold >> price;
62+
data.revenue = price * data.units_sold;
63+
return is;
64+
}
65+
66+
std::ostream& print(std::ostream& os, const Sales_data& data) {
67+
os << data.isbn() << " " << data.units_sold << " " << data.avg_price() << std::endl;
68+
return os;
69+
}
70+
71+
#endif

‎ch11/ex11_10.cpp‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "headers.h"
2+
3+
bool int_vector_iter_comp(const vector<int>::iterator iter1, const vector<int>::iterator iter2) {
4+
return *iter1 < *iter2;
5+
}
6+
7+
bool int_list_iter_comp(const list<int>::iterator iter1, const list<int>::iterator iter2) {
8+
return *iter1 < *iter2;
9+
}
10+
11+
int main() {
12+
// 1. Test vector<int>::iterator as key type
13+
14+
// Without providing custom comparison function
15+
map<vector<int>::iterator, int> default_comparison;
16+
17+
// Providing custom comparison function, check C++ Primer p247
18+
// Basic version
19+
map<vector<int>::iterator, int,
20+
bool (*)(const vector<int>::iterator iter1, const vector<int>::iterator iter2)>
21+
basic(int_vector_iter_comp);
22+
23+
// use decltype
24+
map<vector<int>::iterator, int, decltype(int_vector_iter_comp)*> with_decltype(&int_vector_iter_comp);
25+
26+
// Use type alias or using
27+
typedef bool my_predicate(const vector<int>::iterator iter1, const vector<int>::iterator iter2);
28+
map<vector<int>::iterator, int, my_predicate*> with_typedef(&int_vector_iter_comp);
29+
30+
using my_predicate_pointer_type = bool (*)(const vector<int>::iterator iter1, const vector<int>::iterator iter2);
31+
map<vector<int>::iterator, int, my_predicate_pointer_type> with_using(&int_vector_iter_comp);
32+
33+
vector<int> v = {1, 2, 3};
34+
35+
default_comparison.insert(pair<vector<int>::iterator, int>({v.end(), 0}));
36+
default_comparison.insert(pair<vector<int>::iterator, int>({v.begin(), 0}));
37+
default_comparison.insert(pair<vector<int>::iterator, int>({v.begin(), 1}));
38+
default_comparison.insert(pair<vector<int>::iterator, int>({v.begin() + 1, 1}));
39+
40+
cout << "size: " << default_comparison.size() << endl;
41+
for (auto& p : default_comparison) {
42+
cout << *(p.first) << ": " << p.second << endl;
43+
}
44+
45+
basic.insert(pair<vector<int>::iterator, int>({v.end(), 0}));
46+
basic.insert(pair<vector<int>::iterator, int>({v.begin(), 0}));
47+
basic.insert(pair<vector<int>::iterator, int>({v.begin(), 1}));
48+
basic.insert(pair<vector<int>::iterator, int>({v.begin() + 1, 1}));
49+
50+
cout << "size: " << basic.size() << endl;
51+
for (auto& p : basic) {
52+
cout << *(p.first) << ": " << p.second << endl;
53+
}
54+
55+
with_decltype.insert(pair<vector<int>::iterator, int>({v.end(), 0}));
56+
with_decltype.insert(pair<vector<int>::iterator, int>({v.begin(), 0}));
57+
with_decltype.insert(pair<vector<int>::iterator, int>({v.begin(), 1}));
58+
with_decltype.insert(pair<vector<int>::iterator, int>({v.begin() + 1, 1}));
59+
60+
cout << "size: " << with_decltype.size() << endl;
61+
for (auto& p : with_decltype) {
62+
cout << *(p.first) << ": " << p.second << endl;
63+
}
64+
65+
with_typedef.insert(pair<vector<int>::iterator, int>({v.end(), 0}));
66+
with_typedef.insert(pair<vector<int>::iterator, int>({v.begin(), 0}));
67+
with_typedef.insert(pair<vector<int>::iterator, int>({v.begin(), 1}));
68+
with_typedef.insert(pair<vector<int>::iterator, int>({v.begin() + 1, 1}));
69+
70+
cout << "size: " << with_typedef.size() << endl;
71+
for (auto& p : with_typedef) {
72+
cout << *(p.first) << ": " << p.second << endl;
73+
}
74+
75+
// 2. Test list<int>::iterator as key type
76+
map<list<int>::iterator, int> my_map;
77+
map<list<int>::iterator, int, decltype(int_list_iter_comp)*> my_map2;
78+
79+
list<int> l{1, 2, 3};
80+
81+
// my_map.insert(pair<list<int>::iterator, int>({l.begin(), 1})); // Error, as list<int>::iterator has no < operator defined
82+
my_map2.insert(pair<list<int>::iterator, int>({l.begin(), 1})); // ok, if you supply the comparison function
83+
}

‎ch11/ex11_12.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "headers.h"
2+
3+
int main() {
4+
string s;
5+
int i;
6+
vector<pair<string,int>> v;
7+
while(cin >> s >> i){
8+
pair<string, int> p{s, i};
9+
v.push_back(p);
10+
}
11+
for (auto &p : v){
12+
cout << p.first << "-" << p.second << endl;
13+
}
14+
}

‎ch11/ex11_14.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "headers.h"
2+
3+
using name_birthday_pair = pair<string, string>;
4+
5+
void new_family(map<string, vector<name_birthday_pair>> &family_book, const string &lastname, const vector<name_birthday_pair> &children) {
6+
family_book[lastname] = children;
7+
}
8+
9+
void new_children(map<string, vector<name_birthday_pair>> &family_book, const string &lastname, const vector<name_birthday_pair> &new_children) {
10+
for (auto &c : new_children) {
11+
family_book[lastname].push_back(c);
12+
}
13+
}
14+
15+
void print_family_book(map<string, vector<name_birthday_pair>> family_book) {
16+
for (auto &p : family_book) {
17+
cout << "[Family] " << p.first << ": ";
18+
for (auto &name_birthday_p : p.second) {
19+
cout << name_birthday_p.first << "(" << name_birthday_p.second << "), ";
20+
}
21+
cout << endl;
22+
}
23+
}
24+
25+
int main() {
26+
map<string, vector<name_birthday_pair>> family_book;
27+
28+
new_family(family_book, "James", {make_pair("Bronny", "2001年01月01日"), make_pair("Bryce", "2005年01月01日"), make_pair("Jr.", "2007年01月01日")});
29+
new_family(family_book, "Bryant", {make_pair("Natalie", "2002年01月01日"), make_pair("Bianka", "2006年01月01日"), make_pair("Gianna.", "2008年01月01日")});
30+
print_family_book(family_book);
31+
cout << "[News] New children to Bryant's family!" << endl;
32+
new_children(family_book, "Bryant", {make_pair("Capri", "2017年01月01日")});
33+
print_family_book(family_book);
34+
}

‎ch11/ex11_15.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "headers.h"
2+
#include "Sales_data.h"
3+
4+
bool compareIsbn(const Sales_data &d1, const Sales_data & d2){
5+
return d1.isbn() < d2.isbn();
6+
}
7+
8+
int main() {
9+
multiset<Sales_data, decltype(compareIsbn) *> bookstore(compareIsbn);
10+
11+
multiset<Sales_data, decltype(compareIsbn) *>::const_iterator iter = bookstore.begin();
12+
}

‎ch11/ex11_23.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "headers.h"
2+
3+
using name_birthday_pair = pair<string, string>;
4+
5+
void new_family(multimap<string, vector<name_birthday_pair>> &family_book, const string &lastname, const vector<name_birthday_pair> &children) {
6+
family_book.insert({lastname, children});
7+
}
8+
9+
void print_family_book(multimap<string, vector<name_birthday_pair>> family_book) {
10+
for (auto &p : family_book) {
11+
cout << "[Family] " << p.first << ": ";
12+
for (auto &name_birthday_p : p.second) {
13+
cout << name_birthday_p.first << "(" << name_birthday_p.second << "), ";
14+
}
15+
cout << endl;
16+
}
17+
}
18+
19+
int main() {
20+
multimap<string, vector<name_birthday_pair>> family_book;
21+
22+
new_family(family_book, "James", {make_pair("Bronny", "2001年01月01日"), make_pair("Bryce", "2005年01月01日"), make_pair("Jr.", "2007年01月01日")});
23+
new_family(family_book, "Bryant", {make_pair("Natalie", "2002年01月01日"), make_pair("Bianka", "2006年01月01日"), make_pair("Gianna.", "2008年01月01日")});
24+
print_family_book(family_book);
25+
}

‎ch11/ex11_26.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "headers.h"
2+
3+
int main() {
4+
map<int, string> m = {{1, "ss"}, {2, "sz"}};
5+
using KeyType = map<int, std::string>::key_type;
6+
7+
cout << "type to subscript: " << typeid(KeyType).name() << endl;
8+
cout << "returned from the subscript operator: " << typeid(decltype(m[1])).name() << endl;
9+
10+
return 0;
11+
}

‎ch11/ex11_28.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "headers.h"
2+
3+
int main(){
4+
5+
map<string, vector<int>> m;
6+
map<string, vector<int>>::iterator found_iter = m.find("hi");
7+
}

‎ch11/ex11_3.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "headers.h"
2+
3+
int main() {
4+
map<string, int> counting_book;
5+
set<string> exculsion_set = {"and", "but", "or"};
6+
7+
ifstream ifs("./word_count_input.txt");
8+
string word;
9+
while (ifs >> word) {
10+
if (exculsion_set.find(word) == exculsion_set.end()) {
11+
++counting_book[word];
12+
}
13+
}
14+
15+
for (auto &kv_pair : counting_book) {
16+
cout << kv_pair.first << ": " << kv_pair.second << "; " << endl;
17+
}
18+
}

‎ch11/ex11_31.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "headers.h"
2+
3+
void find_and_erase(multimap<string, string> &m, string k) {
4+
auto iter = m.find(k);
5+
if (iter == m.end()) {
6+
cout << "Element not found!\n";
7+
} else {
8+
m.erase(iter);
9+
cout << "Element with value: " << iter -> second << " found and erased!\n";
10+
}
11+
}
12+
13+
int main() {
14+
using author_work_type = pair<string, string>;
15+
multimap<string, string> m{
16+
author_work_type{"Shakespear", "King Lear"},
17+
author_work_type{"Marquez", "One hundred years of solitude"},
18+
author_work_type{"Marquez", "Love in the Time of Cholera"},
19+
author_work_type{"Shakespear", "Macbeth"},
20+
author_work_type{"Shakespear", "Hamlet"},
21+
author_work_type{"Yu Hua", "A life"},
22+
author_work_type{"Hemingway", "The sun also rises"},
23+
author_work_type{"Hemingway", "A fareware to arms"},
24+
};
25+
26+
// erase
27+
find_and_erase(m, "Shakespear");
28+
cout << m.find("Shakespear") -> second << endl;
29+
find_and_erase(m, "Fred");
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /