Accelerrated C++ Exercise 3-3

#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
using std::map;
class wordcount 
{ 
public: 
	wordcount(string s,int t):word(""),times(0)
	{ 
		word=s; 
		times=t; 
	} 
	int operator < (wordcount &w){return times<w.times;} 
	int operator > (wordcount &w){return times>w.times;} 
	string word; 
	int times; 
}; 
int Comper( wordcount str1, wordcount str2 ) 
{ 
	return str1 > str2; 
} 
int main() 
{ 
	string s; 
	map<string,int> word; 
	vector<wordcount> word_count; 
	cout<<"Place enter your string "
		"end_of_file of  \"Ctrl+Z\":"<<endl;
	while(cin>>s)
		word[s]++; 
	for(map<string,int>::iterator itr=word.begin();itr!=word.end();itr++) 
	{ 
		wordcount w(itr->first,itr->second); 
		word_count.push_back(w); 
	} 
	sort(word_count.begin(),word_count.end(),Comper); 
	for(vector<wordcount>::size_type i=0;i<word_count.size();i++) 
	{ 
		cout<<word_count[i].word<<" "<<word_count[i].times<<endl; 
	} 
	system("pause");
	return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值