统计一篇文章单词的个数(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;
}

输出:

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/81412712