Qt中的泛型容器

一、关于Qt泛型容器

Qt库提供了一组基于模板的一般化的容器类。这些容器可以存储指定的类型的元素。

这些容器比STL容器更轻更安全更容易使用。这些类是隐式共享的,它们都是可重入,它们进行了速度优化,用更少的内存和最小的内联代码扩展,生成更小的可执行文件。此外,当所有的线程仅仅以只读的方式访问它们时,它们是线程安全的。

为了遍历容器的元素,你可以用 Java-style iterators 或者 STL-style iterators. 。 Java-style iterators更容易使用和提供更高级的函数。STL-style iterators 效率稍微高一些,而且可以和Qt的或STL的通用算法一起使用。

Qt也提供关键字foreach 使得很容易遍历容易内的元素,类似于C++11提供的range-for循环,功能相当可以互换使用。

 

二、实战演练

问题:分别以几个典型顺序容器:QVector 、QList、QLinklist和一个典型关联容器:QMap为例进行基本操作,并分别示例Java-style iterators 和 STL-style iterators的用法。

示例代码如下:

#include <QCoreApplication>
#include <QList>
#include <QVector>
#include <QLinkedList>
#include <QMap>
#include <QDebug>

using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QList<int> myList;
QVector<QString> myList2;
QLinkedList<int> myList3;

for(int i=0;i<10;i++)
myList.append(i);

myList.removeOne(5);//删除值为5的元素
myList.removeAt(5);//删除位置为5的元素

qDebug()<<endl<<"QList:";
foreach(int e,myList)
{
qDebug()<<e;
}
qDebug()<<endl;

myList2.append(QString("hello"));
myList2.append(QString("world!"));
qDebug()<<endl<<"QVector:";
for(auto str:myList2)
{
qDebug()<<str;

}
qDebug()<<endl;

for(int i=0;i<10;i++)
{
myList3<<i;
}
qDebug()<<endl<<"QLinkedList:";
foreach(int e,myList3)
{
qDebug()<<e;
}
qDebug()<<endl;

//iterator操作
//C++ style
/*
auto iter=myList.begin();
for(;iter!=myList.end();iter++)
{
qDebug()<<*iter;
}
for(iter=myList.end()-1;iter>=myList.begin();iter--)
{
qDebug()<<*iter;
}
*/
//Qt style
QListIterator<int> list_iter(myList);
list_iter.toFront();
while(list_iter.hasNext())
{
qDebug()<<list_iter.next();

}
qDebug()<<endl;
list_iter.toBack();

while(list_iter.hasPrevious())
{
qDebug()<<list_iter.previous();
}

//QMap
QMap<int,QString> map_string;
map_string.insert(1,"student1");
map_string.insertMulti(1,"student1_1");
map_string.insert(2,"student2");
map_string.insert(3,"student3");


for(auto e :map_string)
{
qDebug()<<e;
}

qDebug()<<endl<<"QMap:";
foreach(int key,map_string.keys())
{
qDebug()<<map_string[key];
}
QMap<int,QString>::iterator map_iter;
for(map_iter=map_string.begin();map_iter!=map_string.end();map_iter++)
{
qDebug()<<map_iter.key()<<": "<<map_iter.value();
}
return a.exec();

}


三、Qt泛型容器一览


Qt提供了以下顺序容器:QList, QLinkedList, QVector, QStack,和 QQueue. 。对于大多数程序而言,QList 是最好用的,即使它以数组列表实现,它提供了非常快的在前面和后面追加的函数。如果你真的需要一个连接表,可以用QLinkedList;。如果你想让元素占用连续的内存空间,可以用 QVector. QStack and QQueue,它们提供了后进先出和先进先出。

Qt也提供了关联式容器: QMap, QMultiMap, QHash, QMultiHash,and QSet. 。Multi容器支持多个value关联到单一的key。Hash容器提供了通过hash函数快速查找取代二分查找。

特殊情况下, QCache 和 QContiguousCache 提供了在有限的cache 中高效的查找对象。

见下表;

Class

Summary

QList<T>

This is by far the most commonly used container class. It stores a list of values of a given type (T) that can be accessed by index. Internally, the QList is implemented using an array, ensuring that index-based access is very fast.

Items can be added at either end of the list using QList::append() and QList::prepend(), or they can be inserted in the middle using QList::insert(). More than any other container class,QList is highly optimized to expand to as little code as possible in the executable.QStringList inherits from QList<QString>.

QLinkedList<T>

This is similar to QList, except that it uses iterators rather than integer indexes to access items. It also provides better performance than QList when inserting in the middle of a huge list, and it has nicer iterator semantics. (Iterators pointing to an item in a QLinkedList remain valid as long as the item exists, whereas iterators to a QList can become invalid after any insertion or removal.)

QVector<T>

This stores an array of values of a given type at adjacent positions in memory. Inserting at the front or in the middle of a vector can be quite slow, because it can lead to large numbers of items having to be moved by one position in memory.

QStack<T>

This is a convenience subclass of QVector that provides "last in, first out" (LIFO) semantics. It adds the following functions to those already present in QVector: push(), pop(), and top().

QQueue<T>

This is a convenience subclass of QList that provides "first in, first out" (FIFO) semantics. It adds the following functions to those already present in QList: enqueue(), dequeue(), and head().

QSet<T>

This provides a single-valued mathematical set with fast lookups.

QMap<Key, T>

This provides a dictionary (associative array) that maps keys of type Key to values of type T. Normally each key is associated with a single value. QMap stores its data in Key order; if order doesn't matter QHash is a faster alternative.

QMultiMap<Key, T>

This is a convenience subclass of QMap that provides a nice interface for multi-valued maps, i.e. maps where one key can be associated with multiple values.

QHash<Key, T>

This has almost the same API as QMap, but provides significantly faster lookups. QHash stores its data in an arbitrary order.

QMultiHash<Key, T>

This is a convenience subclass of QHash that provides a nice interface for multi-valued hashes.

 

————————————————
版权声明:本文为CSDN博主「Ctrlturtle」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hechao3225/article/details/53035375/

posted @ 2021-09-02 22:37  China Soft  阅读(533)  评论(0编辑  收藏  举报