一QList类
- 对于不同的数据类型,QList<T>采取不尸的存储策略,存储策略如下!如果T是一个指针类型或指针大小的基本类型(该基本类型占有的字节数和指针类型占有的字节数相同),QList<T>将数值直接存储在它的数组当中,
- 如果 QList<T>存储对象的指针则该指针指向实际存储的对象。
#include <QCoreApplication> #include <QDebug> using namespace Qt; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //QList类 QList<int> qlist;//初始化一个空的QList<int>列表 for(int i = 0; i< 10;i++ ) { qlist.insert(qlist.end(),i+10); } qDebug()<<qlist; //通过QList<int>::iterator读写迭代器 QList<int>::iterator x; qDebug()<<"输出result"; qDebug()<<endl; for(x = qlist.begin();x !=qlist.end(); x++) { qDebug()<<(*x); *x = (*x)*10+6; } //初始化一个QList<int>const_iterator只读迭代起 qDebug()<<"输出result"; qDebug()<<endl; QList<int>::const_iterator qciter; //输出列表所有的值 for(qciter = qlist.constBegin();qciter !=qlist.constEnd();qciter++) { qDebug()<<*qciter; } //向qlist添加元素 qlist.append(666); QList<int>::iterator itr1; qDebug()<<endl; qDebug()<<"输出result2:"; for(itr1 = qlist.begin();itr1 !=qlist.end();itr1++) { qDebug()<<*itr1; } //查询qlist当中元素 qDebug()<<endl; qDebug()<<"输出result3:"; qDebug()<<qlist.at(3); qDebug()<<qlist.contains(77); qDebug()<<qlist.contains(166); //修改qlist列表的元素值 qDebug()<<endl; qDebug()<<"输出result4:"; qlist.replace(5,888); qDebug()<<qlist; //删除元素 qDebug()<<endl; qDebug()<<"输出result5:"; qlist.removeAt(0); qlist.removeFirst(); qlist.removeAt(6); qDebug()<<qlist; return a.exec(); }
一QLinkedList
- QLinkedList<T>是一个链式列表,它以非连续的内存块保存数据。QLinkedList<T>不能使用下标,只能使用迭代器访问它的数据项。与 QList 相比,当对一个很大的列表进行插入操作时,QLinkedList,具有更高的效率。