title: “ QList和QVector使用\t\t”
tags:

  • qlist
  • qt
  • qvector
    url: 563.html
    id: 563
    categories:
  • Qt
    date: 2017-12-06 14:13:53

介绍

QVector

The QVector class is a template class that provides a dynamic array. QVector is one of Qt’s generic container classes. It stores its items in adjacent memory locations and provides fast index-based access. QList, QLinkedList, QVector, and QVarLengthArray 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 will usually give better performance than QList, because QVector always stores its items sequentially in memory, where QList 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 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, QLinkedList, and QVector 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 will usually give better performance than QList, because QVector always stores its items sequentially in memory, where QList 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, QLinkedList, and QVector等使用操作近似,下述范例仅提供QList的

QList使用

简单范例

include

include

QList m_list;
m_list.append(“a”);//尾插入
m_list.append(“b”);
m_list.append(“c”);
qDebug()< m_list[1]<<
m_list[m_list.size()-1];
qDebug()< m_list.prepend(“t”);//头插入
qDebug()< qDebug()< qDebug()<<m_list.at(2);

运行结果

“a” “b” “c”
“a” “c”
“t”
“b”
“c”

其他函数

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

迭代器风格

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