Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iterator>
- #include <map>
- #include <vector>
- #include <algorithm>
- int main(int argc, char *argv[])
- {
- if (argc < 3)
- {
- std::cerr << "Not enough arguments" << std::endl;
- return 1;
- }
- std::ifstream f(argv[1]);
- if (f.is_open())
- {
- std::map<std::string, int> m;
- std::stringstream wss;
- wss << f.rdbuf();
- f.close();
- std::string buf = "";
- std::string line = wss.str();
- for (auto it : line)
- {
- if ((it > 64 && it < 91) || (it > 96 && it < 123))
- {
- buf += tolower(it);
- }
- else
- {
- if (buf.length() > 0)
- {
- m[buf]++;
- buf.clear();
- }
- }
- }
- std::vector<std::pair<std::string, int> > v(m.size());
- std::copy(m.begin(), m.end(), v.begin());
- m.clear();
- std::sort
- (
- v.begin(), v.end(),
- [](std::pair<std::string, int> const & a, std::pair<std::string, int> const & b)
- {
- return a.second != b.second ? a.second > b.second : a.first < b.first;
- }
- );
- std::ofstream of(argv[2]);
- if (of.is_open())
- {
- for (std::pair<std::string, int> p : v)
- {
- of << p.second << " " << p.first << std::endl;
- }
- of.close();
- }
- else
- {
- std::cerr << "Can't open file " << argv[2] << std::endl;
- return 1;
- }
- }
- else
- {
- std::cerr << "Can't open file " << argv[1] << std::endl;
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement