People, give hints how to do it at all. It is necessary to count words from 5 characters in a text file and give out the 10 most frequent words
Dana is such a structure
// Class with the functionality required for Ü9. class WordCounter { // Read file with given name. void readFile(String fileName); // Update counts per word (wordCounts below) for given string of words. void updateWordCounts(Array<String> words); // Sort words by counts, and return the k words with the largest counts. Array<Pair<String,Integer>> computeFrequentWords(Integer k); // Word counts. Map<String, Integer> wordCounts; } and sample code that performs similar rear. It filters movies from textual documents by popularity and year of release.
void StlDemo::readMovieInfo(std::string fileName) { std::ifstream file(fileName.c_str()); std::string line; while (true) { std::getline(file, line); if (file.eof()) break; // Parse line into title, year, popularity. size_t pos1 = line.find("\t"); if (pos1 == std::string::npos) continue; size_t pos2 = line.find("\t", pos1 + 1); if (pos2 == std::string::npos) continue; Movie movie; movie.title = line.substr(0, pos1); movie.year = atoi(line.substr(pos1 + 1, pos2 - pos1).c_str()); movie.popularity = atoi(line.substr(pos2 + 1).c_str()); // Insert into map. moviesByYear[movie.year].push_back(movie); // std::cout << "\"" << movie.title << "\", " // << movie.year << ", " << movie.popularity << std::endl; } } // ___________________________________________________ void StlDemo::sortByPopularity() { for (auto& pair : moviesByYear) { auto& movies = pair.second; std::sort(movies.begin(), movies.end(), [](const Movie& x, const Movie& y) { return x.popularity > y.popularity; }); } }
map... Well, and what will not work (if you write "nothing happens" - I personally will give the first voice to close the question ...), we will look. - Harry