-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-container.ixx
109 lines (87 loc) · 1.86 KB
/
11-container.ixx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
export module _11_container;
#include <iostream>;
#include <vector>;
#include <list>
#include <forward_list>
#include <map>;
#include <unordered_map>;
#include <unordered_set>;
using namespace std;
// 11.2 vector
struct Entry {
string name;
int number;
};
vector<Entry> phonebook = {
{"Davild", 123},
};
ostream& operator<<(ostream& os, const Entry& e) {
return os << "{\"" << e.name << "\", " << e.number << "";
}
void print_book(const vector<Entry>& book) {
for (int i = 0; i != book.size(); i++) {
cout << book[i] << '\n';
}
for (const auto& x : book) {
cout << x << '\n';
}
vector<int> v1 = { 1,2,3, };
vector<string> v2;
vector<double> v4(1.2);
v1.at(10); // -> error £º·¶Î§¼ì²é
}
// 11.3 list
int f3() {
list<Entry> phonebook = { {"david"s, 123} };
string s = "xxx";
for (const auto& x : phonebook) {
if (x.name == s) return x.number;
}
return 0;
for (auto p = phonebook.begin(); p != phonebook.end(); p++) {
if ((*p).name == s) return (*p).number;
}
return 0;
list<Entry>::iterator p;
Entry ee;
phonebook.insert(p, ee);
phonebook.erase(p);
forward_list<Entry> fl;
fl.push_front(ee);
}
// 11.4 map
// rb-tree
void f4() {
map<string, int> phonebook{
{"david", 1234}
};
phonebook.insert({ "bob", 1122 });
}
// 11.5 unordered_map (hash)
void f5() {
unordered_map<string,int> phonebook;
phonebook.insert({ "bob", 0 });
int num = phonebook["bob"];
}
struct Record {
string name;
int product_code;
};
struct Rhash {
size_t operator()(const Record& r) const {
return hash<string>()(r.name) ^ hash<int>()(r.product_code);
}
};
void f51() {
unordered_set<Record, Rhash> my_set;
}
namespace std {
template<> struct hash<Record> {
using argument_type = Record;
using result_type = std::size_t;
size_t operator()(const Record& r) const {
return hash<string>()(r.name) ^ hash<int>()(r.product_code);
}
};
}
// 11.6 ÈÝÆ÷¸ÅÊö