当前位置:网站首页>QT learning 11 string classes in QT
QT learning 11 string classes in QT
2022-06-29 11:35:00 【A little black sauce】
Qt Study 11 Qt String class in
an issue rooted in history
- C Language I won't support it In the real sense character string
- C Language use A character array and A set of functions Implement string operation
- C Language I won't support it Custom type , therefore Cannot get string type
Solution
from C To C++ The evolutionary process of introduced Custom type
stay C++ Medium can Complete the definition of string type through class
C++ Whether the native type system in contains string types ?
Standard library STL
- STL yes In a sense Need and C++ Together with Standard library
- STL It's a set Completed by template technology C++ Class library
- STL Contains common Algorithm and data structure
- STL It contains String class
Qt VS STL
STL The specific Implementation depends on the compiler manufacturer
STL Of “ standard ” It's just that The interface is standard
- Same global function
- The same algorithm class and data structure class
- The same class member function
Different manufacturers The compiler of STL Differences exist
Depend on STL Developed C++ The behavior of programs on different platforms may vary
Pre development choices
- Whether the project needs to use Current library support ?
- STL?Qt?MFC? Private libraries ?
- Whether the project needs to be in Migration between different platforms ?
- Linux?Windows?Android?
- Whether the project needs Graphical user interface ?
- GUI Applications ? Command line application ? Background services ?
Qt String class in
- Qt String class in
- use Unicode code
- Use Implicit sharing technology To save memory and unnecessary data copies
- Cross platform use , There is no need to consider the platform compatibility of strings
QString VS string
- QString Direct support for The conversion between strings and numbers
- QString Direct support for String size comparison
- QString Direct support for Conversion between different character codes
- QString Direct support for std::string and std::wstring Mutual conversion of
- QString Direct support for Applications of regular expressions
- ...
Code example
#include <QDebug>
void Sample_1()
{
QString s = "add";
s.append(" "); // "add "
s.append("Qt"); // "add Qt"
s.prepend(" "); // " add Qt"
s.prepend("C++"); // "C++ add Qt"
qDebug() << s;
s.replace("add", "&"); // "C++ & Qt"
qDebug() << s;
}
void Sample_2()
{
QString s = "";
int index = 0;
s.sprintf("%d. I'm %s, thank you!", 1, "Delphi Tang"); // "1. I'm Delphi Tang, thank you!"
qDebug() << s;
index = s.indexOf(",");
s = s.mid(0, index); // "1. I'm Delphi Tang"
qDebug() << s;
index = s.indexOf(".");
s = s.mid(index + 1, s.length()); // " I'm Delphi Tang"
s = s.trimmed(); // "I'm Delphi Tang"
qDebug() << s;
index = s.indexOf(" ");
s = s.mid(index + 1, s.length()); // "Delphi Tang"
qDebug() << s;
}
void Sample_3(QString* a, int len)
{
for(int i = 0; i < len; i++) {
for(int j=i+1; j<len; j++) {
if( a[j] < a[i] ) {
QString tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
}
int main()
{
qDebug() << "Sample_1:";
Sample_1();
qDebug() << endl;
qDebug() << "Sample_2:";
Sample_2();
qDebug() << endl;
qDebug() << "Sample_3:";
QString company[5] = {
QString("Oracle"),
QString("Borland"),
QString("Microsoft"),
QString("IBM"),
QString("D.T.Software")
};
Sample_3(company, 5);
for(int i = 0; i < 5; i++) {
qDebug() << company[i];
}
return 0;
}
Qt Medium QString
- QString stay Qt The library is almost ubiquitous
- be-all Qt Graphical user components all depend on QString
Add the message corresponding to the calculator program
/* QCalculatorUI.cpp */
// Content in previous code
void QCalculatorUI::onButtonClicked()
{
QPushButton* btn = (QPushButton*)sender();
QString clickText = btn->text();
if ( clickText == "<-" ) {
QString text = m_edit->text();
if ( text.length() > 0 ) {
text.remove(text.length()-1, 1);
m_edit->setText(text);
}
}
else if ( clickText == "C" ) {
m_edit->setText("");
}
else if ( clickText == "=" ) {
}
else {
m_edit->setText(m_edit->text() + clickText);
}
}
Summary
- application development Most of the time It's all going on string manipulation
- Qt Than STL It is more suitable for cross platform development scenarios
- Qt Medium QString Than STL in string It's bigger and easier to use
- Qt Graphical user components all depend on QString
- Required during project development Comprehensive selection of libraries to be used
边栏推荐
- 什么?漫画居然能免费看全本了,这还不学起来一起做省钱小能手
- Map merges the same keys and values into a list
- 海泰前沿技术|隐私计算技术在医疗数据保护中的应用
- matlab基础 max 求一维或二维数组的最大值+sleep(pause)
- The necessary operation for those big guys to fly 666 inadvertently at the bash command line terminal
- ModbusTCP协议网络学习型单路红外模块(双层板)
- CTO专访:合见工软深化产品布局 加速国产EDA技术革新
- How to properly manage temporary files when writing shell scripts
- Several types of MySQL index invalidation and their solutions
- What is the experience of working in an IT company in Japan?
猜你喜欢

BS-GX-018 基于SSM实现在校学生考试系统

Live broadcast by technical experts from China Kuwait Fangde: how to build a private cloud platform based on openstack and CEPH| Issue 27

TTL串口学习型红外遥控模块可扩展成网络控制

BS-GX-017基于SSM实现的在线考试管理系统

软件工程导论——第五章——总体设计

数据分析方法与思维:漏斗分析

Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career

(JS) array de duplication

How to identify the exact length and width of the contour

专访 SUSS NiFT 负责人:Web3 的未来离不开“人人为我,我为人人”的治理
随机推荐
Exclusive interview with head of suss NIFT: the future of Web3 is inseparable from the governance of "everyone for me, I for everyone"
crypto 1~5
(JS) pure function in array
关于IP定位查询接口的测评Ⅱ
How to properly manage temporary files when writing shell scripts
软件工程导论——第五章——总体设计
涂鸦云开发 demo 登录
Creating postgre enterprise database by ArcGIS
ModbusTCP协议WIFI无线学习型单路红外模块(小壳版)
【HBZ分享】Semaphore 与 CountDownLatch原理
Qt学习10 初探 Qt 中的消息处理
(JS) status mode
在日本的 IT 公司工作是怎样一番体验?
【无标题】我在密谋一件大事
高效远程办公的基石:有效沟通 |社区征文
MySQL 索引失效的几种类型以及解决方式
How to obtain method parameter values through WinDbg
What is the experience of working in an IT company in Japan?
(JS) array flat
【每日3题(2)】生成交替二进制字符串的最少操作数