Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <set>
- #include <algorithm>
- #include <fstream>
- template <typename ElemType>
- class Printer {
- std::ostream& out;
- char delimiter;
- public:
- Printer(std::ostream& outStream = std::cout, char delimChar = 0) : out{ outStream }, delimiter{ delimChar }{};
- void operator()(ElemType elem) const {
- out << elem;
- if (delimiter) { out << delimiter; };
- }
- };
- std::ostream& operator<<(std::ostream& out, std::pair<char, int> el)
- {
- return out << el.first << "\t:\t" << el.second << '\n';;
- }
- int main()
- {
- auto comp = [](std::pair<char, int> el1, std::pair<char, int> el2) { return el1.second < el2.second; };
- std::map<char, int> map;
- //for(int i{0}; i<1000; ++i)
- //{
- // ++map[('a' + rand() % 25)];
- //}
- //
- //std::cout << "### sorted by key ###\n";
- //for (auto mapEl{ map.begin() }; mapEl != map.end(); ++mapEl)
- //{
- // //std::cout << mapEl->first << "\t:\t" << mapEl->second << '\n';
- // std::cout << *mapEl;
- //}
- //return 0;
- std::ifstream inf{ "../Study/Study.cpp" };
- std::istreambuf_iterator<char> infIt(inf);
- std::istreambuf_iterator<char> infEOF;
- //std::ostream_iterator<char> outIt(std::cout, " ");
- //
- //std::copy(infIt, infEOF, outIt);
- //return 0;
- for (; infIt != infEOF; ++infIt)
- {
- ++map[*infIt];
- }
- std::cout << "### sorted by key ###\n";
- for (auto mapEl{ map.begin() }; mapEl != map.end(); ++mapEl)
- {
- //std::cout << mapEl->first << "\t:\t" << mapEl->second << '\n';
- std::cout << *mapEl;
- }
- std::cout << "### sorted by value ###\n";
- std::set<std::pair<char, int>, decltype(comp)> set(map.begin(), map.end(), comp);
- // for(auto setEl{set.begin()}; setEl != set.end(); ++setEl)
- // {
- // //std::cout << setEl->second << "\t:\t" << setEl->first << '\n';
- // std::cout << *setEl;
- // }
- // std::for_each(set.begin(),set.end(),[](auto el){ std::cout << el;});
- std::for_each(set.begin(), set.end(), Printer<std::pair<char, int>>(std::cout, ' '));
- // std::ostream_iterator<std::pair<char,int>> outIt(std::cout, "");
- // std::copy(set.begin(),set.end(), outIt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement