C++-STL(15)-词频统计


输入一个字符:返回每个字符的个数
比如:
输入:"abc1111111111dajogpajogjao%%%%%$4jgcccaaAAAAEEEE1111111&&&&&&&&&&&&&&&&&&&&&";
输出:

map<string, int> scount;//汉字 两字节
map<char, int> ccount;//非汉字,一字节
wordcount[arr[i]]++。map记录值即可。key :字符;value:个数。 
汉字,ASCII>128 或者>258 就看输入编码字符集了。

int mygetNumtest_s(string str)
{	
	map<string, int> scount;
	map<char, int> ccount;
	int i = 0;
	for (int i = 0; i < str.size(); i++)
	{
		string  strc;
		if (str[i] >= 0 && str[i] <= 128)//非汉字 一个字节
		{
			ccount[str[i]]++;
		}
		else                         //  汉字
		{
			strc=str.substr(i, 2);
			scount[strc]++;
			i++;
		}
	}
	for (auto it = ccount.begin(); it != ccount.end(); it++)
	{
		cout << "[" << it->first << "] = " << it->second << endl;//iter->first:key,iter->second:value
	}
	cout << "********************" << endl;
	for (auto it = scount.begin(); it != scount.end(); it++)
	{
		cout << "[" << it->first << "] = " << it->second << endl;//iter->first:key,iter->second:value
	}
	return 1;


}


 

posted @ 2020-02-18 18:44  jasmineTang  阅读(108)  评论(0)    收藏  举报