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

Qt也提供了关联式容器: QMapQMultiMapQHashQMultiHash,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 QVectorpush()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 QListenqueue()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.




  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ctrlturtle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值