Witam Wszystkich
Potrzebuje pomocy i nie mam za bardzo pojęcia jak to ugryźć. Mianowicie program wyświetla wyniki powtórzeń, ale chciałbym posortować mapę od największej ilości powtórzeń i wyświetlić 20 wyników. poniżej kod:
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
using namespace std;
class WordCounter
{
public:
int value;
WordCounter() : value( 0 ) {}
void operator++ (int) { value++; }
};
ostream& operator<<(ostream& st, WordCounter& wc )
{
return st << wc.value;
}
const string path = "/home/grzegorz/ownCloud/Nauka/wszib/sem6/WI/Zadania/Word Count/potop.txt";
int main()
{
map<string, WordCounter> counter;
ifstream input;
input.open( path.c_str() );
if ( !input )
{
cout << "Error in opening file\n";
return 0;
}
string tok;
while ( true )
{
input >> tok;
if ( input )
{
counter[ tok ]++;
}
else break;
}
map< string, WordCounter,less<string> >::iterator it;
for ( it = counter.begin();
it != counter.end();
it++ )
{
cout << (*it).first
<< " "
<< (*it).second
<< endl;
}
return 0;
}