zoukankan      html  css  js  c++  java
  • QList和QVector使用

    博客地址已更改,文章数量较多不便批量修改,若想访问源文请到 coologic博客 查阅,网址:www.coologic.cn

    如本文记录地址为 techieliang.com/A/B/C/ 请改为 www.coologic.cn/A/B/C/ 即可查阅

    版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
    本文标题:QList和QVector使用     本文地址:http://techieliang.com/2017/12/563/

    1. 介绍

    QVector

    The QVector class is a template class that provides a dynamic array.

    QVector<T> is one of Qt’s generic container classes. It stores its items in adjacent memory locations and provides fast index-based access.

    QList<T>, QLinkedList<T>, QVector<T>, and QVarLengthArray<T> provide similar APIs and functionality. They are often interchangeable, but there are performance consequences. Here is an overview of use cases:

    QVector should be your default first choice. QVector<T> will usually give better performance than QList<T>, because QVector<T> always stores its items sequentially in memory, where QList<T> will allocate its items on the heap unless sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. See the Pros and Cons of Using QList for an explanation.
    However, QList is used throughout the Qt APIs for passing parameters and for returning values. Use QList to interface with those APIs.
    If you need a real linked list, which guarantees constant time insertions mid-list and uses iterators to items rather than indexes, use QLinkedList.

    Note: QVector and QVarLengthArray both guarantee C-compatible array layout. QList does not. This might be important if your application must interface with a C API.

    Note: Iterators into a QLinkedList and references into heap-allocating QLists remain valid as long as the referenced items remain in the container. This is not true for iterators and references into a QVector and non-heap-allocating QLists.

    Here’s an example of a QVector that stores integers and a QVector that stores QString values:

    更多请见QVector

    QList:

    The QList class is a template class that provides lists.

    QList<T> is one of Qt’s generic container classes. It stores items in a list that provides fast index-based access and index-based insertions and removals.

    QList<T>, QLinkedList<T>, and QVector<T> provide similar APIs and functionality. They are often interchangeable, but there are performance consequences. Here is an overview of use cases:

    QVector should be your default first choice. QVector<T> will usually give better performance than QList<T>, because QVector<T> always stores its items sequentially in memory, where QList<T> will allocate its items on the heap unless sizeof(T) <= sizeof(void*) and T has been declared to be either a Q_MOVABLE_TYPE or a Q_PRIMITIVE_TYPE using Q_DECLARE_TYPEINFO. See the Pros and Cons of Using QList for an explanation.
    However, QList is used throughout the Qt APIs for passing parameters and for returning values. Use QList to interface with those APIs.
    If you need a real linked list, which guarantees constant time insertions mid-list and uses iterators to items rather than indexes, use QLinkedList.

    Note: QVector and QVarLengthArray both guarantee C-compatible array layout. QList does not. This might be important if your application must interface with a C API.

    Note: Iterators into a QLinkedList and references into heap-allocating QLists remain valid as long as the referenced items remain in the container. This is not true for iterators and references into a QVector and non-heap-allocating QLists.

    更多请见QList

    QList<T>, QLinkedList<T>, and QVector<T>等使用操作近似,下述范例仅提供QList的

    2. QList使用

    2.1. 简单范例

    1. #include <QList>
    2. #include <QDebug>
    3. QList<QString> m_list;
    4. m_list.append("a");//尾插入
    5. m_list.append("b");
    6. m_list.append("c");
    7. qDebug()<<m_list.at(0)<<
    8. m_list[1]<<
    9. m_list[m_list.size()-1];
    10. qDebug()<<m_list.first()<<m_list.last();//取首尾值
    11. m_list.prepend("t");//头插入
    12. qDebug()<<m_list.at(0);
    13. qDebug()<<m_list.takeAt(2);//取出值(会删除原值)
    14. qDebug()<<m_list.at(2);

    运行结果

    1. "a" "b" "c"
    2. "a" "c"
    3. "t"
    4. "b"
    5. "c"

    2.2. 其他函数

    insert在指定位置插入
    swap交换两个位置的值
    contains是否包含判断
    count查找某个值的个数
    indexOf找某个值对应的位置

    2.3. 迭代器风格

    支持Java和STL风格,详情请见Qt容器介绍

    转载请以链接形式标明本文标题和地址:Techie亮博客 » QList和QVector使用
  • 相关阅读:
    看过的代码
    ScipyLectures-simple学习笔记
    机器学习1一个月2017/11/24-2017/12/24
    机器学习课程 matlab 练习
    win7 win8 快捷键直接调出任务管理器
    java 关于getProperty()方法中反斜杠问题
    把myeclipse中html/jsp文件的视图调到只看代码
    Win7 server2008 共享文件夹 不输入网络密码
    别用visual editor了,用WindowBuilder
    visual editor ve1.5下载
  • 原文地址:https://www.cnblogs.com/techiel/p/7994361.html
Copyright © 2011-2022 走看看