Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // To compile: gcc --std=gnu++11 freq.cpp -lstdc++ -o freq
- #include <iostream>
- #include <map>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <fstream>
- #include <cctype>
- using namespace std;
- int main(int argc, char *argv[]) {
- ifstream in;
- ofstream out;
- if (argc < 3) {
- cerr << "too less parametrs \n";
- return -1;
- }
- in.open(argv[1]);
- if (!in.is_open()) {
- cerr << "can't open " << argv[1] << "\n";
- }
- out.open(argv[2]);
- if (!out.is_open()) {
- cerr << "can't open " << argv[2] << "\n";
- }
- string line;
- map<string, int> word_map;
- while (getline(in, line)) {
- string lc_word;
- for(auto ch : line) {
- if (isalpha(ch)) {
- lc_word += tolower(ch);
- continue;
- }
- if (lc_word.size() > 0) {
- word_map[lc_word]++;
- lc_word = "";
- }
- }
- if (lc_word.size() > 0) {
- word_map[lc_word]++;
- lc_word = "";
- }
- }
- std::vector<pair<string, int>> out_vector;
- for (auto item : word_map) {
- out_vector.push_back(item);
- }
- stable_sort(out_vector.begin(), out_vector.end(), [](const pair<string, int>& a, const pair<string, int>& b)->bool{return a.second > b.second;});
- for (auto item : out_vector) {
- out << item.second << " " << item.first << "\n";
- }
- in.close();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement