-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-alg.ixx
149 lines (119 loc) · 2.89 KB
/
12-alg.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <vector>;
#include <string>;
#include <list>;
#include <algorithm>;
#include <iomanip>;
#include <iostream>;
#include <fstream>;
#include <set>;
#include <map>
#include <ranges>;
#include <execution>;
export module _12_alg;
using namespace std;
struct Entry {
string name;
int number;
};
bool operator<(const Entry& x, const Entry& y) {
return x.name < y.name;
}
bool operator==(const Entry& x, const Entry& y) {
return x.name == y.name;
}
void f(vector<Entry>& vec, list<Entry>& lst) {
sort(vec.begin(), vec.end());
unique_copy(vec.begin(), vec.end(), lst.begin());
}
list<Entry> f(vector<Entry> vec) {
list<Entry> res;
sort(vec.begin(), vec.end());
unique_copy(vec.begin(), vec.end(), back_inserter(res));
return res;
}
// 12.2 迭代器
bool has_c(string& s, char c) {
auto p = find(s.begin(), s.end(), c);
return p != s.end();
}
vector<string::iterator> find_all(string& s, char c) {
vector<string::iterator> res;
for (auto p = s.begin(); p != s.end(); p++) {
if (*p == c) res.push_back(p);
}
return res;
}
template<typename C, typename V>
vector<typename C::iterator> find_all(C& c, V v) {
vector<typename C::iterator> res;
for (auto p = c.begin(); p != c.end(); p++) {
if (*p == c) res.push_back(p);
}
return res;
}
// 12.3 迭代器类型
// 12.4 流迭代器
void f4() {
ostream_iterator<string> oo{ cout };
*oo = "hello";
oo++;
*oo = "world";
istream_iterator<string> ii = { cin };
istream_iterator<string> eos = {}; // 结束
}
int f41() {
string from, to;
cin >> from >> to;
ifstream is(from);
istream_iterator<string> ii { is };
istream_iterator<string> eos{};
ofstream os{ to };
ostream_iterator<string> oo{ os, "\n" };
vector<string> b{ ii, eos };
sort(b.begin(), b.end());
unique_copy(b.begin(), b.end(), oo);
return !is.eof() || !os;
}
int f42() {
string from, to;
cin >> from >> to;
ifstream is{ from };
ofstream os{ to };
set<string> b{ istream_iterator<string> {is}, istream_iterator<string>{} };
copy(b.begin(), b.end(), ostream_iterator<string>{os, "\n"});
return !is.eof() || !os;
}
// 12.5 谓词
struct Greater_than {
int val;
Greater_than(int v) : val{ v } {}
bool operator()(const pair<string, int>& r)const { return r.second > val; }
};
void f5(map<string, int>& m) {
auto p = find_if(m.begin(), m.end(), Greater_than{ 42 });
auto p1 = find_if(m.begin(), m.end(), [](const pair<string, int>& r) {return r.second > 42; }); // lambda
}
// 12.7 概念(concept)
template<typename R>
requires sortable<R>
void sort(R& r) {
return sort(begin(r), end(r));
}
// 12.8 容器算法
namespace estd {
template<typename C>
void sort(C& c) {
sort(c.begin(), c.end());
}
template<typename C, typename Pred>
void sort(C& c, Pred p) {
sort(c.begin(), c.end(), p);
}
}
// 12.9 并行算法
void f(vector<int> v) {
sort(v.begin(), v.end());
sort(execution::seq, v.begin(), v.end());
sort(execution::par, v.begin(), v.end());
sort(execution::unseq, v.begin(), v.end());
}