64,609
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <map>
#include <string>
#include<vector>
#include<algorithm>
using std::string;
using std::pair;
using std::map;
using std::vector;
using std::cin;
using std::cout;
using std::endl;
typedef pair<string,int> PAIR;
bool comp( PAIR& x, PAIR&y)
{
return x.second > y.second;
}
int main()
{
map<string,int> word_count;
string word;
cout<<"input word (Ctrl+z exit)"<<endl;
while (cin>>word)
++word_count[word];
cout<<"结果 :"<<endl<<endl;
for( map<string,int>::iterator C =word_count.begin(); C!=word_count.end(); ++C)
cout<<(*C).first<<" "<<(*C).second<<endl;
//---------resort-----------
vector<PAIR> vec;
for( map<string,int>::iterator C =word_count.begin(); C!=word_count.end(); ++C)
{
vec.push_back( make_pair(C->first,C->second) );
}
sort(vec.begin(),vec.end(),comp);
}
for (vector<PAIR>::size_type it=0;it!=vec.size();++it )
{
cout<< vec[it].first <<" "<<cout<<(vec[it].second)<<endl ;
}
再问这个second怎么是个是六位数。没想通