回答編集履歴
1
追記
test
CHANGED
@@ -15,3 +15,143 @@
|
|
15
15
|
...
|
16
16
|
|
17
17
|
```
|
18
|
+
|
19
|
+
[余計なお世話] list使ってre-writeしてみた:
|
20
|
+
|
21
|
+
```C++
|
22
|
+
|
23
|
+
#include <iostream>
|
24
|
+
|
25
|
+
#include <fstream>
|
26
|
+
|
27
|
+
#include <string>
|
28
|
+
|
29
|
+
#include <list>
|
30
|
+
|
31
|
+
#include <utility>
|
32
|
+
|
33
|
+
#include <algorithm>
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
class WordCountList { // 単語とその出現回数をノードにもち,アルファベット順につなげた連結リスト
|
38
|
+
|
39
|
+
using Node = std::pair<std::string,int>;
|
40
|
+
|
41
|
+
std::list<Node> words;
|
42
|
+
|
43
|
+
static bool node_less(const Node& a, const Node& b) { return a.first < b.first; }
|
44
|
+
|
45
|
+
void clear() { words.clear(); } //ノードを全て削除
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
public:
|
50
|
+
|
51
|
+
WordCountList() { }
|
52
|
+
|
53
|
+
~WordCountList() { clear(); };
|
54
|
+
|
55
|
+
int size() const { return words.size(); };
|
56
|
+
|
57
|
+
// 単語を挿入. すでにあれば,出現回数をインクリメント
|
58
|
+
|
59
|
+
void insert(const std::string& w) {
|
60
|
+
|
61
|
+
Node node(w,1);
|
62
|
+
|
63
|
+
auto iter = std::lower_bound(words.begin(), words.end(), node);
|
64
|
+
|
65
|
+
if ( iter != words.end() && iter->first == w ) iter->second++;
|
66
|
+
|
67
|
+
else words.insert(iter, node);
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
// 単語を削除. 1回減算,或は,ノード削除
|
72
|
+
|
73
|
+
void remove(const std::string& w) {
|
74
|
+
|
75
|
+
Node node(w, 0);
|
76
|
+
|
77
|
+
auto iter = std::lower_bound(words.begin(), words.end(), node);
|
78
|
+
|
79
|
+
if ( iter != words.end() && iter->first == w) {
|
80
|
+
|
81
|
+
if ( --iter->second <= 0 ) words.erase(iter);
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
//単語と出現回数をアルファベット順に昇順表示
|
88
|
+
|
89
|
+
void print() const {
|
90
|
+
|
91
|
+
for ( auto iter = words.cbegin(); iter != words.cend(); ++iter ) {
|
92
|
+
|
93
|
+
std::cout << iter->first << "(" << iter->second << ") ";
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
//単語と出現回数をアルファベット順とは反対に降順表示
|
100
|
+
|
101
|
+
void printR() const {
|
102
|
+
|
103
|
+
for (auto iter = words.crbegin(); iter != words.crend(); ++iter) {
|
104
|
+
|
105
|
+
std::cout << iter->first << "(" << iter->second << ") ";
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
};
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
int main() {
|
116
|
+
|
117
|
+
using namespace std;
|
118
|
+
|
119
|
+
int stop = 0; // loop stop flag;
|
120
|
+
|
121
|
+
int n, ct, num;
|
122
|
+
|
123
|
+
char select;
|
124
|
+
|
125
|
+
string input, w;
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
WordCountList wlist;
|
130
|
+
|
131
|
+
while (!stop && cout << " Select I/R/S/P/p/C/W/T/t/Q-->" && cin >> select) {
|
132
|
+
|
133
|
+
switch (select) {
|
134
|
+
|
135
|
+
case 'I': case 'i': cin >> w; wlist.insert(w); break;
|
136
|
+
|
137
|
+
case 'R': case 'r': cin >> w; wlist.remove(w); break;
|
138
|
+
|
139
|
+
case 'S': case 's': cout << "List length = " << wlist.size() << " nodes" << endl; break;
|
140
|
+
|
141
|
+
case 'p': wlist.print(); break; // ascending order
|
142
|
+
|
143
|
+
case 'P': wlist.printR(); break; // descending order
|
144
|
+
|
145
|
+
case 'Q': case 'q': stop = 1; break;
|
146
|
+
|
147
|
+
default: cerr << "Command Error\n"; stop = 1;
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
return 0;
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
```
|