QList和QVector使用

版权声明:若无来源注明,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使用

时间: 2024-10-02 18:28:55

QList和QVector使用的相关文章

QList介绍(QList比QVector更快,这是由它们在内存中的存储方式决定的。QStringList是在QList的基础上针对字符串提供额外的函数。at()操作比操作符[]更快,因为它不需要深度复制)非常实用

FROM:http://apps.hi.baidu.com/share/detail/33517814 今天做项目时,需要用到QList来存储一组点.为此,我对QList类的说明进行了如下翻译. QList是一种表示链表的模板类.QList<T>是Qt的一种泛型容器类.它以链表方式存储一组值,并能对这组数据进行快速索引,还提供了快速插入和删除等操作.QList.QLinkedList和QVector提供的操作极其相似:* 对大多数操作来说,我们用QList就可以了.其API是基于索引(inde

QList和QVector等容器的区别:(转)

源地址:https://blog.csdn.net/qq_33266987/article/details/53333373 Qlist.QVector 与 list.vector似乎不太类似: list插入删除很快,vector查询很快. 但QList 是基于index标签存储它的元素项在内存(虽然内存不连续,这点与list 是一样的),但可以使用 [ ]运算符, 也可以使用.at()操作. QList和QVector等容器的区别: 1.大多数情况下可以用QList.像prepend()和in

Qt容器类的对象模型及应用(线性结构篇:对于QList来说,sharable默认是false的,但对于接下来讲的QVector来说,sharable默认是true)

用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类来使用,但却不知道这种选择是不是最优,这对于对性能要求不苛刻的应用,这种选择不会有任何影响,但是若性能要求苛刻的应用,无疑直接影响到你的系统的成败.上述提及的容器类本质上是对数据结构中的线性结构的不同的实现,本文通过介绍各容器类的实现原理,让我们知道什么时候该用什么样的容器,让大家不再感到迷茫. 1

QVector 和vector的比较(QVector默认使用隐式共享,而且有更多的函数提供)

QVector和vector的比较: Qvector默认使用隐式共享,可以用setSharable改变其隐式共享.使用non-const操作和函数将引起深拷贝.at()比operator[](),快,因为它不进行深拷贝.Qvector取值都会检查越界问题. 看看简单的例子: QVector<int>  vecA; QVector<int>  vecB; vecA.push_back(1); vecA.push_back(10); vecB= vecA; cout<<&q

Qt学习总结(C鱼)之QList和QMap容器类

QList<T> QList<T>容器是一个数组列表,特点如下: 1.大多数情况下可以用QList.像prepend().append()和insert()这种操作,通常QList比QVector快的多.这是因为QList是基于index标签存储它的元素项在内存中(虽然内存不连续,这点与STL的list 是一样的),比那种依赖iterator迭代的容器类更快捷,而且你的代码也更少. 2.当迭代器指向QList中的一个项目后,如果QList进行了插入或者删除操作,那么这个迭代器就无效

Understand the Qt containers(有对应表)

Container classes are one of the cornerstones of object-oriented programming, invaluable tools that free us from having to permanently think about memory management. Qt comes with its own set of container classes, closely modeled after those in the S

Qt容器类(总结)

Introduction Qt库提供了一组基于模板的一般化的容器类.这些容器可以存储指定的类型的元素.例如,如果你需要一个可变大小的Qstring数组,可以用QVector<QString>.. 这些容器比STL容器更轻更安全更容易使用.如果你不熟悉STL或者更喜欢以Qt的方式做事,你可以用这些类取代STL类. 这些类是隐式共享的,它们都是可重入,它们进行了速度优化,用更少的内存和最小的内联代码扩展,生成更小的可执行文件.此外,当所有的线程仅仅以只读的方式访问它们时,它们是线程安全的. 为了遍

QtCore Module&#39;s Classes

Qt Core C++ Classes Provides core non-GUI functionality. More... Reference These are links to the API reference materials. C++ Classes Animation Classes Threading Classes Container Classes Plugin Classes Implicitly Shared Classes State Machine Classe

Qt容器类的对象模型及应用(线性结构篇)(好多图,比较清楚)

用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类来使用,但却不知道这种选择是不是最优,这对于对性能要求不苛刻的应用,这种选择不会有任何影响,但是若性能要求苛刻的应用,无疑直接影响到你的系统的成败.上述提及的容器类本质上是对数据结构中的线性结构的不同的实现,本文通过介绍各容器类的实现原理,让我们知道什么时候该用什么样的容器,让大家不再感到迷茫. 1