Qt--容器模板类
来源:互联网 发布:淘宝客服后台操作 编辑:程序博客网 时间:2024/06/05 03:03
程序 = 数据结构 + 算法
一个良好的程序必须是以数据为中心组织的,为数据和特定的应用场景选择一种合适的容器是编程的基本功。
Qt中提供的容器模板类类似于STL,它提供了Java-style iterators and STL-style iterators两种风格的迭代器,此外还提供了foreach关键字。
Container Classes
sequential containers:
QVector<T>
QVector的内存模型是预先分配好大小的连续数组,所以可以通过索引快速访问,但如果在头部或者中间插入删除需要大量移动数据,效率很慢,此外如果添加超过预设大小,会引起内存重新分配,将原先内存中的数据全部拷贝过来QList<T>
QList虽然也是通过连续内存来保存T数据,当它是通过数组保存T指针来指向实际在内存中的位置,所以也可以通过索引访问,插入删除速度亦非常快速,Qt中大量使用了QList,可见其便利性和速度结合的不错。QLinkedList<T>
QLinkedList内存模型就是双向链表了,只能通过迭代访问,链表的特性就是插入删除无限制咯QStack<T>
QStack是QVector的子类,是后进先出的数据模型,只在结尾插入,结尾删除,所以继承自QVector效率最好了QQueue<T>
QQueue是QList的子类,是先进先出的数据模型,主要在尾部插入,头部取值删除,所以继承自QList也是不二之选
associative containers:
QSet<T>
QSet是没有重复元素的集合,它以未指定的顺序存储值,并提供非常快速的值查找。QSet内部是基于QHash实现的QMap<Key, T>
QMultiMap<Key, T>
QHash<Key, T>
QMultiHash<Key, T>
QMap和QHash提供非常类似的功能,都提供了键值对的快速查找。差异在于:
- QMap基于红-黑树的字典,QHash基于哈希表
- QHash比QMap更快的查找速度
- 在对QHash进行迭代时,项是任意排序的;在QMap中,项是按键排序的
- QHash的关键类型必须提供运算符==()和全局QHash(key)函数;QMap的关键类型必须提供操作符<(),以指定排序规则。
QMultiMap和QMultiHash则提供了一对多的映射。
Iterator Classes
Java-style iterators
Read-only iterator是在类名后面加上Iterator,如QListIterator<T>
Read-write iterator,形式如QMutableListIterator<T>
提供的接口有:
- toFront()
Moves the iterator to the front of the list (before the first item) - toBack()
Moves the iterator to the back of the list (after the last item) - hasNext()
Returns true if the iterator isn’t at the back of the list - next()
Returns the next item and advances the iterator by one position - peekNext()
Returns the next item without moving the iterator - hasPrevious()
Returns true if the iterator isn’t at the front of the list - previous()
Returns the previous item and moves the iterator back by one position - peekPrevious()
Returns the previous item without moving the iterator
*此处我是拷贝的Qt帮助文档,并没有翻译,因为我觉得程序员必须具备看英文文档的能力
STL-Style Iterators
形式类似于
QList::const_iterator (Read-only iterator )
QList::iterator (Read-write iterator)
提供了运算符操作:
- *i
Returns the current item - ++i
Advances the iterator to the next item - i += n
Advances the iterator by n items - –i
Moves the iterator back by one item - i -= n
Moves the iterator back by n items - i - j
Returns the number of items between iterators i and j
foreach
foreach实际是一个宏定义
# ifndef foreach# define foreach Q_FOREACH# endif#define Q_FOREACH(variable, container) \for (QForeachContainer<typename QtPrivate::remove_reference<decltype(container)>::type> _container_((container)); \ _container_.control && _container_.i != _container_.e; \ ++_container_.i, _container_.control ^= 1) \ for (variable = *_container_.i; _container_.control; _container_.control = 0)
形式上是双层for循环,实际第二层for只会循环一次,因为第三条语句container.control = 0必将导致判断条件container.control不生效,那么使用双层for循环,仅仅是为了将*_container_.i赋值给variable。
使用示例:
QList<QString> list; ... QString str; foreach (str, list) qDebug() << str;
阅读全文
1 0
- Qt--容器模板类
- Qt 容器类
- QT容器类
- Qt 容器类
- Qt容器类
- Qt容器类
- Qt 容器类
- Qt容器类
- Qt中的容器类
- Qt中的容器类
- QT容器类
- Qt容器类
- QT容器类总结
- Qt容器类
- QT容器类
- QT容器类
- QT容器类
- QT容器类
- (二)R的数据类型
- oracle判断一个字符串中是否包含另外一个字符串
- Android工具转载之Google推荐的图片加载库Glide介绍
- spring七大模块
- map存储到文件
- Qt--容器模板类
- orecle11gR2客户端安装
- C语言的基础知识
- css背景样式---借鉴w3shool.com.cn
- Eclipse 推荐字体YaHei.Consolas安装配置说明
- 状态码定义
- 完美解决angularjs的ng-click事件不起作用问题
- HBase regions分布不均匀的解决
- CMake交叉编译配置