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

QList和QVector使用的更多相关文章

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

    FROM:http://apps.hi.baidu.com/share/detail/33517814 今天做项目时,需要用到QList来存储一组点.为此,我对QList类的说明进行了如下翻译. QL ...

  2. QList和QVector等容器的区别

    QList和QVector等容器的区别. 1.大多数情况下可以用QList.像prepend()和insert()这种操作,通常QList比QVector快的多.这是因为QList是基于index标签 ...

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

    源地址:https://blog.csdn.net/qq_33266987/article/details/53333373 Qlist.QVector 与 list.vector似乎不太类似: li ...

  4. QList, QLinkedList, QVector, QStack, QQueue的区别,以前也没见过QCache,而且可以自定义cost

    http://doc.qt.io/qt-4.8/containers.html http://doc.qt.io/qt-4.8/qcache.html

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

    用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类 ...

  6. QVector 和vector的比较

    QVector和vector的比较: Qvector默认使用隐式共享,可以用setSharable改变其隐式共享.使用non-const操作和函数将引起深拷贝.at()比operator[](),快, ...

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

    QVector和vector的比较: Qvector默认使用隐式共享,可以用setSharable改变其隐式共享.使用non-const操作和函数将引起深拷贝.at()比operator[](),快, ...

  8. Qt中容器类应该如何存储对象(最好使用对象指针类型,如:QList<TestObj*>,而不要使用 QList<TestObj> 这样的定义,建议采用 智能指针QSharedPointer)

    Qt提供了丰富的容器类型,如:QList.QVector.QMap等等.详细的使用方法可以参考官方文档,网上也有很多示例文章,不过大部分文章的举例都是使用基础类型:如int.QString等.如果我们 ...

  9. Qt:QVector

    0.说明 template <typename T> class QVector QVector是存储同一个类型T数据的模板类,其功能是动态数组,数据在其中的存储是一系列连续的存储空间. ...

随机推荐

  1. TCP/IP协议中的UDP与TCP的区别

    TCP面向连接,UDP面向非连接即发送数据前不需要建立链接TCP提供可靠的服务(数据传输),UDP无法保证,它没有TCP的接受确认.窗口等机制,因此也不需要交换控制信息:发生丢包也一概不负责.TCP面 ...

  2. C语言read函数的那些坑

    今天在复习UNIX文件系统,用到那个read函数,但是无意中却掉到一个坑里了,用了一个多小时才找到问题根源,这里记录一下. 问题是这样的:我需要使用read和write函数把键盘输入的信息复制到输出. ...

  3. 浅谈style.height、clientHeight、offsetHeight、scrollHeight

    先分别介绍以下,以下资料来自MDN HTMLElement.offsetHeight 是一个只读属性,它返回该元素的像素高度,高度包含该元素的垂直内边距和边框,且是一个整数. Element.clie ...

  4. 20155212 ch02 课下作业

    20155212 ch02 课下作业 T1 题目 参考附图代码,编写一个程序 "week0601学号.c",判断一下你的电脑是大端还是小端 相关知识 小端法:最低有效字节在最前面 ...

  5. 20155338 2016-2017-2《Java程序设计》实验四Android程序开发实验报告

    2016-2017-2 20155338 <Java程序设计>实验四Android程序开发实验报告 实验过程及成果展示 1.修改res目录下的layout文件夹中的activity_mai ...

  6. 【转载】Direct3D纹理映射

    原文:Direct3D纹理映射 更详细的文章:DirectX中的纹理映射相关技术 (转)   创建纹理对象 1: HRESULT CreateTexture( 2:   UINT Width,//宽度 ...

  7. Nginx入门篇(七)之Nginx+keepalived高可用集群

    一.keepalived介绍 keepalived软件最开始是转为负载均衡软件LVS而设计,用来管理和监控LVS集群系统中各个服务节点的状态,后来又加入了可实现高可用的VRRP功能.所以Keepali ...

  8. bzoj1854 [Scoi2010]游戏 ([SCOI2010]连续攻击游戏)

    bzoj1854 [Scoi2010]游戏 ([SCOI2010]连续攻击游戏) 据说正解是并查集???我不会 这不是一道匈♂牙利好题吗??? 一个装备的两个属性都向它连边,然后跑一遍匈♂牙利 注意: ...

  9. Visual Studio 设置C#语言代码格式

    设置代码大括号不另起一行: 工具 -> 选项 -> 文本编辑器 -> C# -> 代码样式 -> 格式设置

  10. Windows10 Oracle ODBC安装配置

    项目紧迫,需在短时间内交付成果,新团队成员,吐嘈之前数据库设计太low,很难看懂数据库表结构间的关系,为了使新同事更好的了解数据库表结构,特意使用powerDesigner对oracle.mysql数 ...