2014年02月14日 星期五 09:26
QString类似C++标准库的string,但两者并不能替换。我在上一篇文章已经提到,如果想要完全发挥Qt的优势,我们需要暂时忘掉C++的标准库,尽量不要将Qt与STL混合使用。QString也提供了转换为标准库string的方法(toStdString)。QString的方法更加丰富,使得我们在使用字符串时更加方便快捷。
示例程序如下:
#include <QCoreApplication>
#include <QtCore>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream qout(stdout);
QString s1("Hello World");
qout << s1 << endl;
QString s2(10,'!');
qout << s2 << endl;
s1.append(" ").append(s2);
qout << s1 << endl;
for(auto c : s1){
qout << c << " ";
}
qout << endl;
s1.replace("World","Universe");
qout << s1 << endl;
std::cout << s1.toStdString() << std::endl;
QString s3("12345");
qulonglong i3=s3.toULongLong();
qout << i3 << endl;
QString s4;
s4.setNum(12345);
qout << (s3 == s4 ) << endl;
s4.clear();
qout << s4.size() << endl;
return a.exec();
}
参考资料:
2014年02月14日 星期五 10:26
对。我觉得QString比string好用一万倍。
Zeuux © 2025
京ICP备05028076号