David Blume's GitList
Repositories
testcode.git
Code
Commits
Branches
Tags
Search
Tree:
c7de061
Branches
Tags
c++11
main
start
testcode.git
product
mmmm.cpp
Added a histogram thing.
dblume
commited
c7de061
at 2023-10-18 23:38:10
mmmm.cpp
Blame
History
Raw
// Most Malleable Memory Management Method in C++, by Bjorn Fahller // https://www.youtube.com/watch?v=ptMFLSAkRj0 // This is before PMR-ing it. #include <iostream> #include <vector> #include <string> #include <unordered_map> #include <algorithm> #if __cplusplus == 202002L #include <ranges> #endif class histogram { public: void add(const std::string& word) { ++counter_map_[word]; } void print_top(size_t n, std::ostream& os) const { using count = std::pair<std::string, size_t>; std::vector<count> popular(counter_map_.begin(), counter_map_.end()); #if __cplusplus == 202002L std::ranges::partial_sort(popular, popular.begin() + n, std::greater{}, &count::second); for(const auto& stat: popular | std::ranges::views::take(n)) { os << stat.first << '\t' << stat.second << '\n'; } #endif } private: std::unordered_map<std::string, size_t> counter_map_; }; int test_mmmm() { std::string word; histogram hist; while(std::cin >> word) { hist.add(word); } hist.print_top(5, std::cout); return 0; }