QT  - 讨论区

标题:Qt Tutorial 004:QList的使用方法

2014年02月17日 星期一 09:30

在Qt的顺序容器中,QList是最常被使用的一个。在Qt的官方文档中这样说:

  • For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory. It also expands to less code in your executable.
  • If you need a real linked list, with guarantees of constant time insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList.
  • If you want the items to occupy adjacent memory positions, use QVector.

示例代码如下:

#include <QCoreApplication>
#include <QList>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QList<int> list;
    for(int i=0;i<10;i++){
        list.push_back(i);
    }
    qDebug() << list.size();
    qDebug() << list;
    int sum=0;
    for(auto i : list){
        sum+=i;
    }
    qDebug() << sum;
    for(int i=1;i<10;i++){
        list.push_front(0-i);
    }
    qDebug() << list;
    sum=0;
    for(int i=0;i<list.size();i++){
        sum+=list.at(i);
    }
    qDebug() << sum;

    foreach(int x,list){
        qDebug() << x;
    }

    list.clear();

    return a.exec();
}

参考资料:

http://qt-project.org/doc/qt-4.8/qlist.html

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号