当前位置:网站首页>How QT counts the frequency of letters in a string
How QT counts the frequency of letters in a string
2022-07-24 01:37:00 【hellokandy】
The problem background : In a string , Find all the letters , Then count the frequency of each letter in the string . for instance : character string abbccc, There are three letters in total (a、b、c), The frequency of each letter is :1、2、3.
1、 coded
#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、 How to use ?
#include <QtCore/QCoreApplication>
#include "LetterStats.hpp"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
const QString word("hello, [email protected]#$%^&* I'm Chinese \r\n123");
QMap<QChar, int> resultMap = LetterStats::GetStatisticResults(word);
qDebug() << "resultMap.size()=" << resultMap.size();
return a.exec();
}
3、 Output results
"size(): 7"
'd' 1
'e' 1
'h' 1
'l' 3
'o' 2
'r' 1
'w' 1
resultMap.size()= 7
边栏推荐
- Summary of HCIA knowledge points
- Hcip day 6 notes
- Basic network solutions for small and medium-sized hospitals
- 代码阅读方法与最佳实践
- How to synchronize MySQL database when easycvr platform is upgraded to the latest version v2.5.0?
- Exchange 2013 SSL证书安装文档
- MySQL Basics (operators, sorting and paging, multi table queries, functions)
- Parsing yaml configuration files using C language and libcyaml Library
- Research on retinal vascular segmentation based on GAN using few samples
- vantUI,Axiso,常见问题及使用:
猜你喜欢
随机推荐
MD5 encryption and decryption website test, is MD5 encryption still safe?
机房建设资料
代码阅读方法与最佳实践
Why can't HMI panels of botu V17 and below connect with CPUs of 1500 firmware version 2.9 or 1200 firmware version 4.5?
OSPF(第六天笔记)
Hcip seventh day notes
Exchange 2010通配符SSL证书安装文档
php7 垃圾回收机制详解
win11系统之win11亮点
Kotlin foundation from introduction to advanced series explanation (basic chapter) keyword: suspend
HCIP实验
Hcip day 9 notes
Summary of HCIA knowledge points
Second and third order trade expressions of linear algebra
Add of cmake_ dependencies
OSPF(第四天笔记)
中小型医院基础网络解决方案
HCIP第一天笔记
HCIP第五天笔记
Arm architecture and programming 4 -- serial port (based on Baiwen arm architecture and programming tutorial video)









