43-QList类& QLinkedList类

一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,具有更高的效率。

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值