- 元素个数小于1000的QList类对象能实现链表中的快速插入与查找操作
- 前端插入prepend(),后端插入append()
- QList是一种链表模板,是Qt的一种泛型容器类
- QList成员函数基于索引实现,QLinkedList成员函数基于迭代器实现,固QList比QLinkedList更快
示例源码:
#include <QCoreApplication>
#include <QDebug>
#include <QTextStream>
static QTextStream cout (stdout,QIODevice::WriteOnly);
#include <iostream>
#include <QtGlobal>
#include <QByteArray>
#include <QList>
#include <QStringList>
void test()
{
qDebug() << "HelloWorld-qdebug";
cout << "Helloword-QTextStream" <<endl;
std::cout<<"Helloword-std::cout"<<std::endl;
}
void test_types()
{
qint8 signed_char_type;//signed char 有符号8bit
qint16 signed_short_type;//signed short 有符号16bit
qint32 signed_int_type;//signed int 有符号32bit
qint64 signed_long_long_int_type;//signed long long int 有符号64bit
qDebug() <<"sizeof(qint8)=" <<sizeof (signed_char_type)
<<"sizeof(qint16)=" <<sizeof (signed_short_type)
<<"sizeof(qint32)=" <<sizeof(signed_int_type)
<<"sizeof(qint64)=" <<sizeof (signed_long_long_int_type)<<endl;;
qptrdiff qint32_or_qint64_type;//32位系统 qint32 64位系统 qint64
qreal double_or_float_type;//默认是double,如果加上-qreal float选项,那么是float
qintptr same_as_qptrdiff;//32位系统 qint32 64位系统 qint64
quintptr quint32_or_quint64_type;//32位系统 quint32 64位系统 quint64
qDebug() <<"sizeof(qptrdiff)=" <<sizeof (qint32_or_qint64_type)
<<"sizeof(qreal)=" <<sizeof (double_or_float_type)
<<"sizeof(qintptr)=" <<sizeof(same_as_qptrdiff)
<<"sizeof(quintptr)=" <<sizeof (quint32_or_quint64_type)<<endl;
quint8 unsigned_char_type;//unsigned char 无符号8bit
quint16 unsigned_short_type;//unsigned short 无符号16bit
quint32 unsigned_int_type;//unsigned int 无符号32bit
quint64 unsigned_long_long_int_type;//unsigned long long int 无符号64bit
qDebug() <<"sizeof(quint8)=" <<sizeof (unsigned_char_type)
<<"sizeof(quint16)=" <<sizeof (unsigned_short_type)
<<"sizeof(quint32)=" <<sizeof(unsigned_int_type)
<<"sizeof(quint64)=" <<sizeof (unsigned_long_long_int_type)<<endl;
qlonglong same_as_qint64_type;//相当于qint64
qulonglong same_as_quint64_type;//相当于quint64
qDebug() <<"sizeof(qlonglong)=" <<sizeof (same_as_qint64_type)
<<"sizeof(qulonglong)=" <<sizeof (same_as_quint64_type)<<endl;
uchar unsigned_char_type_same_quint8;//unsigned char 无符号8bit
ushort unsigned_short_type_same_quint16;//unsigned short 无符号16bit
uint unsigned_int_type_same_quint32;//unsigned int 无符号32bit
ulong unsigned_long_type;//unsigned long 无符号32bit
qDebug() <<"sizeof(uchar)=" <<sizeof (unsigned_char_type_same_quint8)
<<"sizeof(ushort)=" <<sizeof (unsigned_short_type_same_quint16)
<<"sizeof(uint)=" <<sizeof(unsigned_int_type_same_quint32)
<<"sizeof(ulong)=" <<sizeof (unsigned_long_type)<<endl;
}
void test_qbytearray()
{
QByteArray bArray("Hello QByteArray"); // '\0'
int len = bArray.length();
char cLast = bArray[17];
qDebug()<<"string len:"
<<len
<<"last char:"
<<cLast
<<endl;
}
void test_qbytearray_resize()
{
QByteArray ba;
ba.resize(3);
ba[0]=0x3c;
ba[1]=0xb8;
ba[2]=0x64;
qDebug()<<endl<<"ba length:"<<ba.length()<<endl;
}
void test_qbytearray_fill()
{
QByteArray ba("hello");
qDebug()<<"ba before fill:"<<ba<<endl;
ba.fill('K');
qDebug()<<"ba after fill:"<<ba<<endl;
ba.fill('K',2);
qDebug()<<"ba after fill two char:"<<ba<<endl;
ba.fill('Q',10);
qDebug()<<"ba after fill ten char Q:"<<ba
<<"qbytearray length:"<<ba.length()
<<"sizeof(ba)="<<sizeof (ba)<<endl //ba is a point of QByteArray
<<"sizeof(QByteArray[])="<<sizeof ( (char)ba[1])<<endl;
ba.data()[7]='A';
qDebug()<<ba.at(4)<<ba.data()[5]<<ba.constData()[7]<<endl;
}
void test_qbytearray_right_left_mid()
{
QByteArray ba("HelloWorld2023");
qDebug()<<ba.left(5);
qDebug()<<ba.right(4);
qDebug()<<ba.mid(5,5);
}
void test_qbytearray_size()
{
QByteArray ba("HelloWorld");
qDebug()<<ba.length();
qDebug()<<ba.size();
qDebug()<<ba.count();
QByteArray ba1("He\0llo");
qDebug()<<ba1.length();
}
void test_datatype_convert()
{
QByteArray ba("HelloWorld");
QByteArray ba_hex = ba.toHex();
qDebug() << "HelloWorld to Hex: "<<ba_hex<<endl; //48656c6c6f576f726c64
QByteArray ba_fromhex = QByteArray::fromHex("48656c6c6f576f726c64");
qDebug() << "QByteArray from Hex: "<<ba_fromhex<<endl;
int digit = 255;
uint u_digit = 255u;
qlonglong qll_digit = 255ll;
qulonglong qull_digit = 255ull;
qDebug()<<"number base 10 on int :"<< QByteArray::number(digit);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit);
qDebug()<<"number base 10 on int :"<< QByteArray::number(78);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(78u);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(78ll);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(78ull);
qDebug()<<"number base 10 on int :"<< QByteArray::number(digit,16);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit,16);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit,16);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit,16);
qDebug()<<"number base 10 on int :"<< QByteArray::number(digit,8);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit,8);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit,8);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit,8);
qDebug()<<"number base 10 on int :"<< QByteArray::number(digit,2);
qDebug()<<"number base 10 on uint :"<<QByteArray::number(u_digit,2);
qDebug()<<"number base 10 on qlonglong :"<<QByteArray::number(qll_digit,2);
qDebug()<<"number base 10 on qulonglong :"<<QByteArray::number(qull_digit,2);
QByteArray num;
qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit);
qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit);
qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit);
qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit);
qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit,16);
qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit,16);
qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit,16);
qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit,16);
qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit,8);
qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit,8);
qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit,8);
qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit,8);
qDebug()<<"QByteArray::setNum base 10 on int :"<< num.setNum(digit,2);
qDebug()<<"QByteArray::setNum base 10 on uint :"<<num.setNum(u_digit,2);
qDebug()<<"QByteArray::setNum base 10 on qlonglong :"<<num.setNum(qll_digit,2);
qDebug()<<"QByteArray::setNum base 10 on qulonglong :"<<num.setNum(qull_digit,2);
double dbNum=12345.66;
qDebug()<<"number base 10 on double of e format(prec=6) :"<< QByteArray::number(dbNum,'e');
qDebug()<<"number base 10 on double of e format(prec=3) :"<< QByteArray::number(dbNum,'e',3);
qDebug()<<"number base 10 on double of E format(prec=6) :"<< QByteArray::number(dbNum,'E');
qDebug()<<"number base 10 on double of e format(prec=3) :"<< QByteArray::number(dbNum,'E',3);
qDebug()<<"number base 10 on double of f format(prec=6) :"<< QByteArray::number(dbNum,'f');
qDebug()<<"number base 10 on double of f format(prec=3) :"<< QByteArray::number(dbNum,'f',3);
qDebug()<<"number base 10 on double of f format(prec=3) :"<< QByteArray::number(dbNum,'f',1);
qDebug()<<"number base 10 on double of g format(prec=6) :"<< QByteArray::number(dbNum,'g');
qDebug()<<"number base 10 on double of g format(prec=3) :"<< QByteArray::number(dbNum,'g',3);
qDebug()<<"number base 10 on double of G format(prec=6) :"<< QByteArray::number(dbNum,'G');
qDebug()<<"number base 10 on double of G format(prec=3) :"<< QByteArray::number(dbNum,'G',3);
QByteArray abc="HelloWorld";
qDebug()<<"HelloWorld ->toLower() : "<<abc.toLower();
qDebug()<<"HelloWorld ->toUpper() : "<<abc.toUpper();
QByteArray strNum="-123456";
QByteArray strNumU="123456";
qDebug()<<"strNum ->toInt() : "<<strNum.toInt();
qDebug()<<"strNum ->toUInt() : "<<strNumU.toUInt();
bool isBase=false;
QByteArray strDigit="255";
QByteArray strBin="11111111";
qDebug()<<"digit ->toInt() base on 10 : "<<strDigit.toInt(&isBase)<<",isBase:"<<isBase;
qDebug()<<"digit ->toInt() base on 16 : "<<strDigit.toInt(&isBase,16)<<",isBase:"<<isBase;
qDebug()<<"digit ->toInt() base on 8 : "<<strDigit.toInt(&isBase,8)<<",isBase:"<<isBase;
qDebug()<<"digit ->toInt() base on 2 : "<<strBin.toInt(&isBase,2)<<",isBase:"<<isBase;
QByteArray strDouble="3.14159";
qDebug()<<"strDouble ->toDouble() : "<<strDouble.toDouble(&isBase)<<",isBase:"<<isBase;
qDebug()<<"strDouble ->toFloat() : "<<strDouble.toFloat(&isBase)<<",isBase:"<<isBase;
qDebug()<<QByteArray::number(strDouble.toDouble());
qDebug()<<QByteArray::number(strDouble.toDouble(),'g');
qDebug()<<QByteArray::number(strDouble.toDouble(),'g',3);
qDebug()<<QByteArray::number(strDouble.toDouble(),'G');
qDebug()<<QByteArray::number(strDouble.toDouble(),'G',3);
qDebug()<<QByteArray::number(strDouble.toDouble(),'f');
qDebug()<<QByteArray::number(strDouble.toDouble(),'f',3);
qDebug()<<QByteArray::number(strDouble.toDouble(),'e');
qDebug()<<QByteArray::number(strDouble.toDouble(),'e',3);
qDebug()<<QByteArray::number(strDouble.toDouble(),'E');
qDebug()<<QByteArray::number(strDouble.toDouble(),'E',3);
}
typedef struct User{
int id;
std::string name;
}USER;
void test_qbytearray_charpoint_std_string_qstring()
{
USER u;
u.id=1001;
u.name="hello";
QByteArray arr;
arr.append((char*)&u,sizeof (u));
USER *pUser = (USER*)arr.data();
qDebug() <<pUser->id<<pUser->name.c_str();
QByteArray ba("HelloWorld");
char *pBa = ba.data();
qDebug() << pBa;
qDebug() << *pBa;
while (*pBa) {
qDebug()<< "item of Array : " << *pBa;
++pBa;
}
char *p = &ba.data()[5];
qDebug() <<p;
std::string str = ba.toStdString();
qDebug()<<"std::string ->"<<str.c_str();
QByteArray baStd = QByteArray::fromStdString(std::string("this is a std string"));
qDebug()<<"fromStdString ->"<<baStd;
QString qstr = QString("HelloWorld");
qDebug()<<"QString to QByteArray ->"<<qstr.toLatin1();
QByteArray tmpBa = "flkdsjlkfjkdsjfldsjkfjk";
qstr=tmpBa;
qDebug()<<qstr;
if(tmpBa.isEmpty())
{
qDebug()<<"";
}
if(QByteArray("").isEmpty())
{
qDebug()<<"";
}
if(QByteArray().isEmpty())
{
qDebug()<<"";
}
QByteArray ba_index("HelloWorld");
qDebug() << ba_index.indexOf('l');
qDebug() << ba_index.lastIndexOf('l');
char *ppp = new char('W');
qDebug() << ba_index.indexOf(ppp);
qDebug() << ba_index.indexOf(QString("r"));
qDebug() << ba_index.indexOf(QByteArray("ello"));
qDebug() << ba_index.lastIndexOf(ppp);
qDebug() << ba_index.lastIndexOf(QString("r"));
qDebug() << ba_index.lastIndexOf(QByteArray("ello"));
ba_index.insert(5,'X');
qDebug()<<ba_index;
ba_index.insert(5,5,'G');
qDebug()<<ba_index;
ba_index.insert(5,"INPUT");
qDebug()<<ba_index;
ba_index.insert(5,"HEHEHEHE",2);
qDebug()<<ba_index;
ba_index.insert(2,QByteArray("ANSI"));
qDebug()<<ba_index;
ba_index.insert(3,QString("nice"));
qDebug()<<ba_index;
}
void test_qchar_qstring_qlist_qstringlist()
{
char c1= 'G';
//针对汉字的uniocde编码不要加单引号,加上了单引号会被认为是一个字符 ,而汉字是两个字符
QChar ch1=0x4E2D;//汉字的中字
ushort us1=0x4E2D;
qDebug()<<c1<<ch1;
QChar qc1 = QChar(0x4E,0X2D);
QChar qc2 = QChar(us1);
QChar qc3 = QChar(short(168));
QChar qc4 = QChar(255);
QChar qc5 = QChar(-127);
QChar qc6 = QChar(QChar::LineFeed);
QChar qc7 = QChar(QLatin1Char('A'));
bool isDigit = qc1.isDigit();
if(qc2.isLetter()){
}
auto x = qc7.isNumber() ? 123 : 456;
auto y = qc6.isLetterOrNumber() ? "yes" : "no";
qc7.toLower();
qc5.toUpper();
qc6.unicode();
qc3.toLatin1(); //QChar to char
char mych = qc7.toLatin1();
char tmpch = 'R';
qc7=tmpch;
QChar cc(tmpch);
QString qs1();
QString str("hello");
QString qs2(qc7.unicode());
QString qs3(qc1);
QString qs4(5,QChar('Q'));
QString qs5(QLatin1String("Hello"));
QString qs6(QString("Hello"));
str.swap(qs4);
qDebug() <<str<<qs4;
qDebug() <<qs4.count()<<qs4.length()<<qs4.size();
if(qs6.isEmpty()){
}
qs4.resize(100);
qDebug() <<qs4.count()<<qs4.length()<<qs4.size();
qDebug() <<qs4;
QString qs8;
qs8.resize(50,QChar('W'));
qDebug() <<qs8.count()<<qs8.length()<<qs8.size();
qDebug() <<qs8;
int a=1;
QString b=" kjflkdsjlf ";
QString qs9("id:{%1},anme:{%2}");
qDebug() <<qs9.arg(a).arg(b);
b.append("fdjsflksdj");
b.prepend("jklfsdjklfjldsj");
b.remove("kjfl");
b.trimmed();
b.clear();
qDebug() <<b.toLocal8Bit();
QString qqq("1234");
qqq.toUInt();
qDebug()<< qqq.compare("123");
const char *pchar = "hello";
b = QString( QLatin1String(pchar));
qDebug()<< b;
QByteArray q8b = b.toLocal8Bit();
b.fromLocal8Bit("fldsjfldsjfldslk");
b.fromLocal8Bit(q8b);
std::string stdStr = b.toStdString();
pchar = b.toStdString().c_str();
pchar = b.toStdString().data();
char buf[2048]={0};
strcpy(buf,b.toStdString().data());
qDebug()<<buf;
b.fromStdString(std::string("Hello"));
b.clear();
std::string strCH("China中国");
qDebug()<<strCH.c_str();
if(b.isEmpty()){
b = b.fromStdString(strCH);
qDebug()<<b;
}
std::string c_strCH =std::string("中文");
QString str_CH("中文");
qDebug()<<c_strCH.c_str()<<str_CH.toStdString().c_str();
b.clear();
b=b.fromStdString("中文");
qDebug()<<b.toStdString().c_str();
}
void test_qlist_qstringlist()
{
QList<QString> list;//链表声明
//链表赋值
list <<QString("one")
<<QString("two")
<<QString("three")
<<QString("four")
<<QString("fire");
qDebug()<<list;
//遍历并访问元素
for(int i=0;i<list.count();i++){
qDebug()<<list[i];
}
for(int i=0;i<list.size();i++){
qDebug()<<list.at(i);
}
list.insert(3,"4444");
qDebug()<<list;
list.replace(3,"8888");
qDebug()<<list;
list.removeAt(3);
qDebug()<<list;
list.move(0,4);
qDebug()<<list;
list.swap(2,3);
qDebug()<<list;
list.prepend("zero");
list.append("ten");
qDebug()<<list;
list.removeFirst();
list.removeLast();
qDebug()<<list;
qDebug()<<list.indexOf("fire");
qDebug()<<list.contains("hello");
qDebug()<<list.count();
QStringList qstring_list;
qstring_list<<"hello"<<"world"<<QStringLiteral("广")<<QStringLiteral("州");
qstring_list.append("nice");
for(QString v:qstring_list){
qDebug()<<v;
}
for(int i=0;i<qstring_list.size();i++){
qDebug()<<qstring_list.at(i);
}
QStringList m;
m <<QStringLiteral("一")
<<QStringLiteral("二")
<<QStringLiteral("三")
<<QStringLiteral("四")
<<QStringLiteral("五");
qDebug()<<m;
m.insert(3,QStringLiteral("八"));
qDebug()<<m;
qDebug()<<m.join(',');
qDebug()<<m.join('-');
QString strArray = "c++,C#,VB,Java,javascript,typescript,lua,go";
QStringList arrList=strArray.split(',');
for(QString v:arrList){
qDebug()<<v;
}
qDebug()<<arrList.indexOf("c++");
qDebug()<<arrList.replaceInStrings("c","Cplusplus");
qDebug()<<arrList.filter("java");
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
test();
test_types();
test_qbytearray();
test_qbytearray_resize();
test_qbytearray_fill();
test_qbytearray_right_left_mid();
test_qbytearray_size();
test_datatype_convert();
test_qbytearray_charpoint_std_string_qstring();
test_qchar_qstring_qlist_qstringlist();
test_qlist_qstringlist();
return a.exec();
}
标签:Qt5.12,toInt,QStringList,链表,插入,base,include,QList
From: https://blog.51cto.com/remotedev/6149256