Hello. I have such a task:
- Find the 2 longest combo words
- Find all combined words in the file
- Count the total number of words in the file
That is, I enter a file in the program that contains words one by one in a line without spaces, say 1000 words. It is necessary that the program read all the words, find ordinary words (for example, cat) and a combined word (for example, catdog). The program must recognize this combined word and write it into an array for counting, and of all the combined, it must find the 2 longest.
I have the starting code, the program just searches for the 2 longest words, but not the combined ones:
#include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <set> using namespace std; struct Lcompare : binary_function<string, string, bool> { bool operator()(const string& p, const string& q) const { return p.size() > q.size(); } }; int main() { setlocale(LC_ALL, "Russian"); multiset<string, Lcompare > words; ifstream ifs("words.txt"); if (!ifs) { cout << "Ошибка, файл не найден \nПожалуйста повторите попытку" << endl; } else { while (!ifs.eof()) { string s; ifs >> s; words.insert(s); } // вывести на экран multiset<string, Lcompare >::iterator it = words.begin(); for (int i = 1; i <= 2 && it != words.end(); i++) cout << *it++ << endl; } system("pause"); return 0; }