e120db9bb2d44681ee48917763a852bc245abab5
dblume Added a histogram thing.

dblume authored 1 year ago

1) // Most Malleable Memory Management Method in C++, by Bjorn Fahller
2) // https://www.youtube.com/watch?v=ptMFLSAkRj0
3) // This is before PMR-ing it.
4) #include <iostream>
5) #include <vector>
6) #include <string>
7) #include <unordered_map>
8) #include <algorithm>
9) #if __cplusplus == 202002L
dblume Add SeparateDefinitionBlock...

dblume authored 1 month ago

10) #include <ranges>
dblume Added a histogram thing.

dblume authored 1 year ago

11) #endif
12) 
dblume Add SeparateDefinitionBlock...

dblume authored 1 month ago

13) class histogram
14) {
15)   public:
16)     void add(const std::string& word)
17)     {
dblume Added a histogram thing.

dblume authored 1 year ago

18)         ++counter_map_[word];
19)     }
20) 
dblume Add SeparateDefinitionBlock...

dblume authored 1 month ago

21)     void print_top(size_t n, std::ostream& os) const
22)     {
dblume Added a histogram thing.

dblume authored 1 year ago

23)         using count = std::pair<std::string, size_t>;
24)         std::vector<count> popular(counter_map_.begin(), counter_map_.end());
25) #if __cplusplus == 202002L
26)         std::ranges::partial_sort(popular,
27)                                   popular.begin() + n,
28)                                   std::greater{},
29)                                   &count::second);
dblume Add SeparateDefinitionBlock...

dblume authored 1 month ago

30)         for (const auto& stat : popular | std::ranges::views::take(n)) {
dblume Added a histogram thing.

dblume authored 1 year ago

31)             os << stat.first << '\t' << stat.second << '\n';
32)         }
33) #endif
34)     }
dblume Add SeparateDefinitionBlock...

dblume authored 1 month ago

35) 
36)   private:
dblume Added a histogram thing.

dblume authored 1 year ago

37)     std::unordered_map<std::string, size_t> counter_map_;
38) };
39) 
40) int test_mmmm()
41) {
42)     std::string word;
43)     histogram hist;
dblume Add SeparateDefinitionBlock...

dblume authored 1 month ago

44)     while (std::cin >> word) {