当前位置:网站首页>QT 如何统计字符串中字母出现的频次
QT 如何统计字符串中字母出现的频次
2022-07-23 02:27:00 【hellokandy】
问题背景:在一串字符串中,找出所有的字母,然后统计字符串中出现的每个字母的频次。比如说:字符串abbccc,一共出现了三个字母(a、b、c),每个字母对应出现的频次是:1、2、3。
1、编码实现
#pragma once
#include <QMap>
#include <QDebug>
#include <QScopedPointer>
namespace
{
bool isLetter(char c)
{
if ((c >= 'A' && c <= 'Z')
|| c >= 'a' && c <= 'z')
{
return true;
}
return false;
}
}
/*! * \brief LetterStatsPrivate class. */
class LetterStatsPrivate : public QMap<QChar, int>
{
public:
LetterStatsPrivate() {
}
void StatisticResults()
{
qDebug() << QString("size(): %1").arg(size());
QMap<QChar, int>::const_iterator itr;
for (itr = constBegin(); itr != constEnd(); ++itr)
{
qDebug() << itr.key() << "\t" << itr.value();
}
}
};
/*! * \brief LetterStats class.Character stats */
class LetterStats
{
public:
static QMap<QChar, int> GetStatisticResults(const QString& str)
{
QMap<QChar, int> result;
LetterStats ls(str);
ls.d->swap(result);
return result;
}
private:
LetterStats(const QString& content)
: d(new LetterStatsPrivate())
{
for (int i = 0; i < content.size(); ++i)
{
QChar ch = content.at(i);
if (/*ch.isLetter()*/ isLetter(ch.toLatin1()))
{
QChar chLower = ch.toLower();
int num = d->contains(chLower) ? d->value(chLower) + 1 : 1;
d->insert(chLower, num);
}
}
d->StatisticResults();
};
QScopedPointer<LetterStatsPrivate> d;
};
2、如何使用?
#include <QtCore/QCoreApplication>
#include "LetterStats.hpp"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
const QString word("hello, [email protected]#$%^&*我是中国人\r\n123");
QMap<QChar, int> resultMap = LetterStats::GetStatisticResults(word);
qDebug() << "resultMap.size()=" << resultMap.size();
return a.exec();
}
3、输出结果
"size(): 7"
'd' 1
'e' 1
'h' 1
'l' 3
'o' 2
'r' 1
'w' 1
resultMap.size()= 7
边栏推荐
猜你喜欢

31岁才转行程序员,目前34了,我来说说我的经历和一些感受吧...

平台型企业如何解决分账核算业务呢?

LEADTOOLS 20-22 Crack-DotNetCore!!!

2022-07-22:以下go语言代码输出什么?A:1;B:1.5;C:编译错误;D:1.49。 package main import “fmt“ func main() { var i

A convnet for the 2020s paper reading

Q-Vision+Kvaser CAN/CAN FD/LIN总线解决方案

可视化全链路日志追踪

Ardunio——ULN2003驱动板和直流电机风扇——控制风扇转速

LEADTOOLS 20-22 Crack-DotNetCore!!!

实现多层级条件查询(类似京东多层级添加查询)
随机推荐
NN in pytorch Modulelist and nn.sequential
第一次改开源中间件keycloak总个结
PNA peptide nucleic acid modified polypeptide z-gly-pro-pna | d-phe-pip-arg-pna | TOS Gly Pro Arg PNA
Leetcode 110. balanced binary tree
Use recursive string inversion and Full Permutation
完成端口的几个重要问题
Ardunio——ULN2003驱动板和直流电机风扇——控制风扇转速
新规出|一建证书公路与水利专业含金量上升
Liunx上杀死一进程
canal 配置01
Peptide modified PNA peptide nucleic acid bz-d-phe-val-arg-pna|l-phe-val-arg-pna
【Node中间层实践小记(一)】----搭建项目框架
隐藏网站服务器响应头中 PHP 版本信息
canal 02
The new regulation issued the first construction certificate, and the gold content of highway and water conservancy specialty increased
Airiot Q & A issue 5 | how to use low code business flow engine?
网络安全基础之DNS与DHCP
Peptide nucleic acid coupled polypeptide ile Glu Gly Arg PNA (s-2222) | BOC Leu Gly Arg PNA
Installation, configuration and use of sentry
中信期货是正规的期货公司吗,开户是否安全?