|
|
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
int main ()
{
ifstream InFile("article.txt");
ofstream OutFile1("3_1_1out.txt");
ofstream OutFile2("3_1_2out.txt");
OutFile1<<"in lexicographic order:"<<endl;
OutFile2<<"in descending frequency order:"<<endl;
map<string,int> word_count;
string word;
while(InFile>>word)
{
++word_count[word];
}
map<string,int>::iterator map_it=word_count.begin();
while(map_it!=word_count.end())
{
OutFile1 <<(*map_it).first << "\tEnter\t"
<<(*map_it).second<< "\ttimes "<<endl;
map_it++;
}
typedef map<string,int >::value_type value_type;
vector<value_type>temp;
temp.reserve(word_count.size());
copy(word_count.begin(),word_count.end(),back_inserter(temp));
struct pred
{
bool operator()(const value_type& a, const value_type& b)
{
return a.second<b.second;
}
}
sort(temp.begin(),temp.end(),pred());
return 0;
}
|
|