4、QList类,QLinkedList类

QList类

  对于不同数据类型,QList<T>采取不同的存储策略

  • 如果T是一个指针类型或者指针大小的基本类型(该基本类型占有的字节数和指针类型占有的字节数相同),QList<T>将数值直接存储在它的数组中。
  • 如果QList<T>存储对象的指针,则该指针只想实际存储的对象。
复制代码
//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;
    for (x = qlist.begin(); x !=qlist.end(); x++) {
        qDebug() << (*x);
        *x =(*x)*10+6;
        qDebug() << (*x);
    }

    QList<int>::const_iterator qciter;
    for (qciter = qlist.begin(); qciter !=qlist.constEnd();qciter++) {
        qDebug() << *qciter;
    }


    qDebug() << endl;
    //想qlist添加元素
    qlist.append(666);
    QList<int>::iterator itr1;
    for (itr1 = qlist.begin(); itr1 !=qlist.constEnd();itr1++) {
        qDebug() << *itr1;
    }

    qDebug() << endl;
    //查询qlist当中的元素
    qDebug() << qlist.at(3);
    qDebug() << qlist.contains(666);


    //修改qlist里的值
    qlist.replace(3,888);
    qDebug() << qlist;


    //删除qlist里的元素
    qlist.removeAt(0);
    qlist.removeAt(2);
    qlist.removeFirst();
    qlist.removeLast();
    qDebug() << qlist;
复制代码

 

QLinkedList类

QLinkedList<T>是一个链式列表,它是以非连续的内存块保存数据。所以不能是用下标,只能用迭代器访问数据项,与QList相比,当对一个很大的列表插入数据时,QLinkedList效率更高。

复制代码
//QLinkedList

    QLinkedList<QString> qAllMonth;

    for (int i = 0; i < 12; i++) {
        qAllMonth << QString("%1%2").arg("Month:").arg(i);
    }


    //读写迭代器
    QLinkedList<QString>::iterator itow=qAllMonth.begin();

    for(;itow !=qAllMonth.end();itow++){
        qDebug() << *itow;
    }



    //只读迭代器
    QLinkedList<QString>::const_iterator itr=qAllMonth.begin();
    for(;itr !=qAllMonth.end();itr++){
        qDebug() << *itr;
    }
复制代码

 

posted @   秃头的C#  阅读(73)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示