容器类用于容纳其它类型的数据或者对象,如一个列表中有多个字符串。
C++中的容器类包括“顺序存储结构”和“关联存储结构”,前者包括vector,list,deque等;后者包括set,map,multiset,multimap等。
与标准C++一样,Qt提供了类似的类,列表如下:
Class | Summary |
---|
QList<T> | 最常用的窗口类,能应付大多数情况。<T>指它容纳的数据类型, 如QString、int等,通过整数索引能访问它的元素。 QStringList 继承自:QList<QString>,专门用于容纳字符串。 |
QLinkedList<T> | 链表,通迭代器访问元素。当将元素插入大量数据中间中某个位置时,效率更高。 |
QVector<T> | c++中vector类的重新实现。数据连续地存储在内存中。在头部或中间插入数据时,效率低下,因要移动大量数据。 |
QStack<T> | QVector的子类。 操作数据时,后进先出,似乎与C++中的队列相同。 "last in, first out" (LIFO) |
QQueue<T> | QList的子类。先进先出。"first in, first out" (FIFO). |
QSet<T> | 能快速查找,查看它和c++的set的区别:http://blog.csdn.net/small_qch/article/details/7384966 |
QMap<Key, T> | 关联式容器映射,一个key对应着一个T值 QMap按Key的顺序存储数据; 如果顺序不重要,QHash更快更适合。 |
QMultiMap<Key, T> | QMap的子类。一个key可对应着多个T值 |
QHash<Key, T> | 与QMap几乎一致, 但大大提升查找速度. QHash以随意顺序存储数据 |
QMultiHash<Key, T> | QHash子类,一个key可对应着多个T值 |
列表中,
QList
<T>中的T表示所要存储的数据类型,如字符串,可用
QList
<QString>,整数的话,可以
QList
<int>,其它依此类推.另一类
QMap
<Key, T>中的Key也是可以多种类型的
至于在实际应用中,选用哪个:一般速度与存储空间是不能兼得的。
下面看典型的两个:QList<T>与QMap<Key, T>的使用:
QList的模式:
- QList<QString> list;
- list << "winxp" << "win7" << "mac"<<"kubuntu";
可看成存储后的效果如下,左边数字一栏是索引(整数,自动添加),右边是所对应的值:
取值时可用:
- QString a = list.at(0);
- QString b = list[2];
其它如first()、last()函数都可以便捷地取到相应的第一位和最后一位的值
如果删除或插入一行,索引所对应的值都将改变。
如删除"win7",则:
用
QMap
<Key, T>也可以实现上面
QList
<T>的功能:
- QMap<int, QString> map;
- map.insert(0,"winxp");
- map.insert(1,"win7" );
- map.insert(2,"mac");
- map.insert(3,"kubuntu");
取值时,可用:
查看
QList
<T>的公有函数,它提供了大量的方法来操作它所容纳的元素
- QList()
- QList(const QList<T> & other)
- QList(QList<T> && other)
- QList(std::initializer_list<T> args)
- ~QList()
- void append(const T & value)
- void append(const QList<T> & value)
- void prepend(const T & value)
- void push_back(const T & value)
- void push_front(const T & value)
- void insert(int i, const T & value)
-
- void replace(int i, const T & value)
- void pop_back()
- void pop_front()
- int removeAll(const T & value)
- void removeAt(int i)
- bool removeOne(const T & value)
- void removeFirst()
- void removeLast()
- void reserve(int alloc)
-
- void move(int from, int to)
- void clear()
-
- bool contains(const T & value) const
- int count(const T & value) const
- int count() const
- int length() const
- bool empty() const
- bool endsWith(const T & value) const
- int indexOf(const T & value, int from = 0) const
- bool isEmpty() const
- int lastIndexOf(const T & value, int from = -1) const
- QList<T> mid(int pos, int length = -1) const
- int size() const
- bool startsWith(const T & value) const
- void swap(QList<T> & other)
- void swap(int i, int j)
-
- T & operator[](int i)
- T takeAt(int i)
- T takeFirst()
- T takeLast()
- T & back()
- T & first()
- T & front()
- T & last()
- T value(int i) const
- T value(int i, const T & defaultValue) const
- const T & operator[](int i) const
- const T & at(int i) const
- const T & back() const
- const T & first() const
- const T & front() const
- const T & last() const
- QSet<T> toSet() const
- std::list<T> toStdList() const
- QVector<T> toVector() const
- QList<T> operator+(const QList<T> & other) const
- QList<T> & operator+=(const QList<T> & other)
- QList<T> & operator+=(const T & value)
- QList<T> & operator<<(const QList<T> & other)
- QList<T> & operator<<(const T & value)
- QList<T> & operator=(const QList<T> & other)
- QList & operator=(QList<T> && other)
- bool operator==(const QList<T> & other) const
- bool operator!=(const QList<T> & other) const
-
- const_iterator begin() const
- const_iterator cbegin() const
- const_iterator cend() const
- const_iterator constBegin() const
- const_iterator constEnd() const
- const_iterator end() const
- iterator end()
- iterator erase(iterator pos)
- iterator erase(iterator begin, iterator end)
- iterator begin()
- iterator insert(iterator before, const T & value)