统计一篇文章单词的个数(map)

标签: map

 统计一篇英文文章中单词出现的频率(为简单起见,假定依次从键盘输入该文章)

关键字是string类型

#include<bits/stdc++.h>
using namespace std;
int main()  
{
	map<string, int> wordCount;      //map
	string word;                     //string
	while ( cin >> word )
		++wordCount[word];           //单词频率统计
	map<string, int>::iterator it ;
	for ( it= wordCount.begin();  it != wordCount.end();  ++it)
		cout<<"Word: "<<(*it).first     
		<<" \tCount:"<<(*it).second<<endl;
	return 0;
}

输出:

原文链接:加载失败,请重新获取