Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // только стандартная кодировка
- void WordsCounter( const std::string name_in, const std::string name_out )
- {
- std::map<std::string, ull> words_map;
- ull max_count = std::numeric_limits<ull>::max();
- std::string word = "";
- std::string line;
- std::ifstream input_stream( name_in );
- try
- {
- for( line; std::getline( input_stream, line ); )
- {
- for( const auto& symb: line )
- if( std::isalpha( static_cast<unsigned char>( symb ) ) )
- word.push_back( std::tolower( static_cast<unsigned char>( symb ) ) );
- else
- {
- if( word.length() )
- {
- if( words_map.find( word ) != words_map.end() )
- words_map[word] = words_map[word] == max_count ? -1 : words_map[word] + 1;
- else
- words_map[word] = 1;
- word = "";
- }
- }
- }
- input_stream.close();
- }
- catch( std::exception const& e )
- {
- std::cout << "Err in reading file " << e.what() << endl;
- input_stream.close();
- }
- std::vector< std::pair<std::string, ull> > res_word_list( words_map.begin(), words_map.end() );
- std::sort( res_word_list.begin(), res_word_list.end(),
- [](const pair<std::string, ull> &p1, const pair<std::string, ull> &p2)
- {
- return p1.second > p2.second || p1.second == -1;
- });
- std::ofstream output_stream( name_out );
- try
- {
- for( const auto& word_info: res_word_list )
- {
- if( word_info.second == -1 )
- output_stream << "Очень много";
- else
- output_stream << word_info.second;
- output_stream << " "<< word_info.first;
- output_stream << std::endl;
- }
- output_stream.close();
- }
- catch( std::exception const& e )
- {
- std::cout << "Err in writing file " << e.what() << endl;
- output_stream.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement