Advertisement
avr39ripe

Pv913FileLetterFreq

Aug 19th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <algorithm>
  5. #include <fstream>
  6.  
  7. template <typename ElemType>
  8. class Printer {
  9.     std::ostream& out;
  10.     char delimiter;
  11. public:
  12.     Printer(std::ostream& outStream = std::cout, char delimChar = 0) : out{ outStream }, delimiter{ delimChar }{};
  13.     void operator()(ElemType elem) const {
  14.         out << elem;
  15.         if (delimiter) { out << delimiter; };
  16.     }
  17. };
  18.  
  19. std::ostream& operator<<(std::ostream& out, std::pair<char, int> el)
  20. {
  21.     return out << el.first << "\t:\t" << el.second << '\n';;
  22. }
  23.  
  24. int main()
  25. {
  26.     auto comp = [](std::pair<char, int> el1, std::pair<char, int> el2) { return el1.second < el2.second; };
  27.     std::map<char, int> map;
  28.     //for(int i{0}; i<1000; ++i)
  29.     //{
  30.     //  ++map[('a' + rand() % 25)];
  31.     //}
  32.     //
  33.     //std::cout << "### sorted by key ###\n";
  34.     //for (auto mapEl{ map.begin() }; mapEl != map.end(); ++mapEl)
  35.     //{
  36.     //  //std::cout << mapEl->first << "\t:\t" << mapEl->second << '\n';
  37.     //  std::cout << *mapEl;
  38.     //}
  39.     //return 0;
  40.  
  41.     std::ifstream inf{ "../Study/Study.cpp" };
  42.     std::istreambuf_iterator<char> infIt(inf);
  43.     std::istreambuf_iterator<char> infEOF;
  44.  
  45.     //std::ostream_iterator<char> outIt(std::cout, " ");
  46.     //
  47.     //std::copy(infIt, infEOF, outIt);
  48.  
  49.     //return 0;
  50.  
  51.     for (; infIt != infEOF; ++infIt)
  52.     {
  53.         ++map[*infIt];
  54.     }
  55.  
  56.     std::cout << "### sorted by key ###\n";
  57.     for (auto mapEl{ map.begin() }; mapEl != map.end(); ++mapEl)
  58.     {
  59.         //std::cout << mapEl->first << "\t:\t" << mapEl->second << '\n';
  60.         std::cout << *mapEl;
  61.     }
  62.  
  63.     std::cout << "### sorted by value ###\n";
  64.     std::set<std::pair<char, int>, decltype(comp)> set(map.begin(), map.end(), comp);
  65.  
  66.     //  for(auto setEl{set.begin()}; setEl != set.end(); ++setEl)
  67.     //  {
  68.     //      //std::cout << setEl->second << "\t:\t" << setEl->first << '\n';
  69.     //      std::cout << *setEl;
  70.     //  }
  71.  
  72.     //  std::for_each(set.begin(),set.end(),[](auto el){ std::cout << el;});
  73.  
  74.     std::for_each(set.begin(), set.end(), Printer<std::pair<char, int>>(std::cout, ' '));
  75.  
  76.     //  std::ostream_iterator<std::pair<char,int>> outIt(std::cout, "");
  77.     //  std::copy(set.begin(),set.end(), outIt);
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement