统计文章中单词出现次数c++(map)

功能是统计文章中单词出现次数,注意不能加标点符号,输入0为停止输入

#include 
#include 
#include
using namespace std;

int main()
{
	map<string,int> m;
	string word[500];
	int i, j;
	cout << "Input the essay (0为终止符)" << endl;
	for (i = 0;; i++)
	{
		cin >> word[i];
		if (word[i] == "0")
			break;
	}
	for (j = 0; j < i; j++)
	{
		map<string, int>::iterator iter = m.find(word[j]);
		if (iter != m.end())
		{
			iter->second++;
		}
		else
		{
			m.insert(pair<string, int>(word[j], 1));
		}
	}
	map<string, int>::iterator iter = m.begin();
	for (iter; iter != m.end(); iter++)
	{
		cout << iter->first << "  " << iter->second << endl;
	}
	return 0;
}


有时间加一下去除标点的功能,暂时这样吧,有点累了(⊙﹏⊙)

你可能感兴趣的:(C++学习笔记)