字符统计2(统计出现频率最大的字符)

#include <iostream>
#include<string>
#include<queue>

using namespace std;

struct CharCount
{
	char c;
	int freq;
	bool operator< (const CharCount &rop) const
	{
		return freq < rop.freq;
	}

};

/*
 *	[A, Z] ===> [65, 90]
 *	[a, z] ===> [97, 122]
 *
*/
int char_count(void)
{
	int count[52] = {0};
	string str;
	while (cin >> str)
	{
		char c;
		memset(count, 0, sizeof count);
		int len = str.length();
		for (int i = 0; i < len; i++)
		{
			c = str[i];
			if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122))  /* check the char */
			{
				c - 'A' > 25 ? count[26+c-'a']++ : count[c-'A']++;
			}
		}
	    priority_queue<CharCount> que ;
		while (!que.empty())
			que.pop();
		for (i =0; i < 52; i++)
		{
			/*
			cout << char (i > 25 ? i - 26 + 97 : i + 65) << "  " << count[i] << "\t";
			if (count[i])
				cout << char (i > 25 ? i - 26 + 97 : i + 65) << ": " << count[i] << "\t";
			*/

			CharCount temp;
			temp.freq = count[i];
			temp.c = char (i > 25 ? i - 26 + 97 : i + 65);
			que.push(temp);
		}
		cout << que.top().c << ": " << que.top().freq << "\n";
	}
	return 0;
}




int main()
{
	return	char_count();
}


当多个字符频率一致时候,具体显示哪一个字符应该是由c++中的priority_queue的实现决定的。

和上一个版本相比基本思想没有变化。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值