QList和QVector使用
本文标题: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. 简单范例
- #include <QList>
- #include <QDebug>
- QList<QString> m_list;
- m_list.append("a");//尾插入
- m_list.append("b");
- m_list.append("c");
- qDebug()<<m_list.at(0)<<
- m_list[1]<<
- m_list[m_list.size()-1];
- qDebug()<<m_list.first()<<m_list.last();//取首尾值
- m_list.prepend("t");//头插入
- qDebug()<<m_list.at(0);
- qDebug()<<m_list.takeAt(2);//取出值(会删除原值)
- qDebug()<<m_list.at(2);
运行结果
- "a" "b" "c"
- "a" "c"
- "t"
- "b"
- "c"
2.2. 其他函数
insert在指定位置插入
swap交换两个位置的值
contains是否包含判断
count查找某个值的个数
indexOf找某个值对应的位置
2.3. 迭代器风格
支持Java和STL风格,详情请见Qt容器介绍
QList和QVector使用的更多相关文章
- QList介绍(QList比QVector更快,这是由它们在内存中的存储方式决定的。QStringList是在QList的基础上针对字符串提供额外的函数。at()操作比操作符[]更快,因为它不需要深度复制)非常实用
FROM:http://apps.hi.baidu.com/share/detail/33517814 今天做项目时,需要用到QList来存储一组点.为此,我对QList类的说明进行了如下翻译. QL ...
- QList和QVector等容器的区别
QList和QVector等容器的区别. 1.大多数情况下可以用QList.像prepend()和insert()这种操作,通常QList比QVector快的多.这是因为QList是基于index标签 ...
- QList和QVector等容器的区别:(转)
源地址:https://blog.csdn.net/qq_33266987/article/details/53333373 Qlist.QVector 与 list.vector似乎不太类似: li ...
- 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
- Qt容器类的对象模型及应用(线性结构篇:对于QList来说,sharable默认是false的,但对于接下来讲的QVector来说,sharable默认是true)
用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类 ...
- QVector 和vector的比较
QVector和vector的比较: Qvector默认使用隐式共享,可以用setSharable改变其隐式共享.使用non-const操作和函数将引起深拷贝.at()比operator[](),快, ...
- QVector 和vector的比较(QVector默认使用隐式共享,而且有更多的函数提供)
QVector和vector的比较: Qvector默认使用隐式共享,可以用setSharable改变其隐式共享.使用non-const操作和函数将引起深拷贝.at()比operator[](),快, ...
- Qt中容器类应该如何存储对象(最好使用对象指针类型,如:QList<TestObj*>,而不要使用 QList<TestObj> 这样的定义,建议采用 智能指针QSharedPointer)
Qt提供了丰富的容器类型,如:QList.QVector.QMap等等.详细的使用方法可以参考官方文档,网上也有很多示例文章,不过大部分文章的举例都是使用基础类型:如int.QString等.如果我们 ...
- Qt:QVector
0.说明 template <typename T> class QVector QVector是存储同一个类型T数据的模板类,其功能是动态数组,数据在其中的存储是一系列连续的存储空间. ...
随机推荐
- PHP-学习笔记-进阶
PHP-学习笔记-进阶 PHP类和对象之定义类的方法 访问控制的关键字代表的意义为: public:公开的 protected:受保护的 private:私有的 我们可以这样定义方法: class C ...
- android studio 插件开发(自动生成框架代码插件)
android studio 插件开发 起因 去年公司开始上新项目,正好android在架构这方面的讨论也开始多了起来,于是mvp架构模型就进入我们技术选择方案里面,mvp有很多好处,但是有一个非常麻 ...
- FPGA之CORDIC算法实现_代码实现(下)
关于FPGA之CORDIC算法的纯逻辑实现,博主洋葱洋葱“https://www.cnblogs.com/cofin/p/9188629.html”以及善良的一休军“https://blog.csdn ...
- leetcode-744-Find Smallest Letter Greater Than Target(改进的二分查找)
题目描述: Given a list of sorted characters letters containing only lowercase letters, and given a targe ...
- 20155223 2016-2017-2 《Java程序设计》第10周学习总结
20155223 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 Java Socket编程 网络上的两个程序通过一个双向的通讯连接实现数据的交换,这个双向 ...
- 20155328 2016-2017-2 《Java程序设计》第三周学习总结
20155328 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 类是对象的设计图,对象是类的实例.用class定义类,用new新建一个对象. 一个原始码中可 ...
- OpenStack入门篇(八)之镜像服务Glance
一.Glance的概述 Glance是为虚拟机的创建提供镜像的服务,我们基于Openstack是构建基本的IaaS平台对外提供虚拟机,而虚拟机在创建时必须为选择需要安装的操作系统,Glance服务就是 ...
- Scrapy爬取美女图片续集 (原创)
上一篇咱们讲解了Scrapy的工作机制和如何使用Scrapy爬取美女图片,而今天接着讲解Scrapy爬取美女图片,不过采取了不同的方式和代码实现,对Scrapy的功能进行更深入的运用.(我的新书< ...
- selenium自动化之处理浏览器警告弹窗
有的网站会弹出类似如下图的警告弹窗,你会发现这种弹窗在html源码中怎么也定位不到,接下来将介绍这种弹窗的处理方式. 其实这种弹窗是不属于html的元素的,他是属于浏览器自带的弹窗,所以用定位元素的方 ...
- Python解包参数列表及 Lambda 表达式
解包参数列表 当参数已经在python列表或元组中但需要为需要单独位置参数的函数调用解包时,会发生相反的情况.例如,内置的 range() 函数需要单独的 start 和 stop 参数.如果它们不能 ...