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
|
#include <math.h>
#include <stc/cstr.h>
#define i_val_str
#include <stc/cvec.h>
#define i_key_str
#define i_val int
#include <stc/cmap.h>
int main1()
{
c_auto (cvec_str, words)
c_auto (cmap_str, word_map)
{
words = c_make(cvec_str, {
"this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"
});
c_foreach (w, cvec_str, words) {
cmap_str_emplace(&word_map, cstr_str(w.ref), 0).ref->second += 1;
}
c_foreach (i, cmap_str, word_map) {
printf("%d occurrences of word '%s'\n",
i.ref->second, cstr_str(&i.ref->first));
}
}
return 0;
}
#ifdef __cplusplus
#include <string>
#include <iostream>
#include <vector>
#include <unordered_map>
int main2()
{
std::vector<std::string> words = {
"this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"
};
std::unordered_map<std::string, size_t> word_map;
for (const auto &w : words) {
word_map[w] += 1;
}
for (const auto &pair : word_map) {
std::cout << pair.second
<< " occurrences of word '"
<< pair.first << "'\n";
}
return 0;
}
int main() {
main1();
puts("");
main2();
}
#else
int main() {
main1();
}
#endif
|