tsp_cpp/snippets/compte_mots.cpp

89 lines
1.5 KiB
C++
Raw Permalink Normal View History

#include <map>
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
int main()
{
ifstream is("cyrano.txt");
string mot;
map<string, int> compteMots;
while (is >> mot)
{
compteMots[mot]++;
}
is.close();
multimap<int,string> countMap;
transform(compteMots.begin(),compteMots.end(),inserter(countMap,countMap.begin()),
[](pair<string,int> paire) -> ::pair<int,string>
{
return make_pair(paire.second,paire.first);
});
for(std::map<string, int>::iterator it = compteMots.begin() ;
it != compteMots.end();
++it)
{
if (it->second > 5)
{
cout << setw(20) << it->first << '\t'
<< it->second << endl;
}
}
cout << endl << endl;
cout << "Compte ordonné croissant" << endl;
ofstream sortie("Fréquences triées.txt");
for(const pair<const int,string> & compte : countMap)
{
if (compte.first > 5)
{
cout << setw(20) << compte.second << '\t'
<< compte.first << endl;
sortie << setw(20) << compte.second << '\t'
<< compte.first << endl;
}
}
sortie.close();
cout << endl << endl;
cout << "Compte ordonné décroissant" << endl;
for(auto it = countMap.rbegin();it!=countMap.rend();++it)
{
if((*it).first > 5)
{
cout << setw(20) << (*it).second << '\t'
<< (*it).first << endl;
}
}
}