Qt模块化笔记之core——容器类归纳

容器类用于容纳其它类型的数据或者对象,如一个列表中有多个字符串。

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的模式:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. QList<QString> list;  
  2. list << "winxp" << "win7" << "mac"<<"kubuntu";  

可看成存储后的效果如下,左边数字一栏是索引(整数,自动添加),右边是所对应的值:

0winxp
1win7
2mac
3kubuntu
取值时可用:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. QString a = list.at(0);//返回winxp  
  2. QString b = list[2];//返回mac  
其它如first()、last()函数都可以便捷地取到相应的第一位和最后一位的值

如果删除或插入一行,索引所对应的值都将改变。

如删除"win7",则:

0winxp
1mac
2kubuntu

QMap <Key, T>也可以实现上面 QList <T>的功能:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. QMap<int, QString> map;  
  2. map.insert(0,"winxp");  
  3. map.insert(1,"win7" );  
  4. map.insert(2,"mac");  
  5. map.insert(3,"kubuntu");  
取值时,可用:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. QString a = map["2"];//返回"mac"  




查看 QList <T>的公有函数,它提供了大量的方法来操作它所容纳的元素

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.     QList()  
  2.     QList(const QList<T> & other)  
  3.     QList(QList<T> && other)  
  4.     QList(std::initializer_list<T> args)  
  5.     ~QList()  
  6. void           append(const T & value)//往容器最后添加一个数据或对象  
  7. void        append(const QList<T> & value)  
  8. void    prepend(const T & value)//在第一个前添加  
  9. void    push_back(const T & value)//功能与append相同  
  10. void        push_front(const T & value)//与prepend相同  
  11. void    insert(int i, const T & value)//在第i位置插入  
  12.   
  13. void    replace(int i, const T & value)//将第i位的数据替换成value  
  14. void    pop_back()//pop意为拿出,即删除尾部一个数据,以下几个删除功能将对应位置元素删除后,元素都重新索引  
  15. void    pop_front()//删除头部一个  
  16. int     removeAll(const T & value)//清空与value值相同的元素,返回删除个数  
  17. void    removeAt(int i)//删除某个i位置的元素  
  18. bool    removeOne(const T & value)//删除第一个匹配到的值  
  19. void     removeFirst()//删除第一个元素  
  20. void    removeLast()//删除最后一个元素  
  21. void    reserve(int alloc)//未知,求教  
  22.   
  23. void    move(int from, int to)//移动元素,效果如:list << "A" << "B" << "C" << "D" << "E" << "F";list.move(1, 4);最终list: ["A", "C", "D", "E", "B", "F"]  
  24. void    clear()  
  25.   
  26. bool    contains(const T & value) const//判断是否包含某值  
  27. int count(const T & value) const  
  28. int count() const//返回list中元素个数  
  29. int length() const//与count相同  
  30. bool    empty() const  
  31. bool    endsWith(const T & value) const//是否以某值结尾  
  32. int     indexOf(const T & value, int from = 0) const//某值的索引  
  33. bool        isEmpty() const  
  34. int     lastIndexOf(const T & value, int from = -1) const  
  35. QList<T>  mid(int pos, int length = -1) const  
  36. int     size() const//与count相同  
  37. bool        startsWith(const T & value) const  
  38. void        swap(QList<T> & other)  
  39. void        swap(int i, int j)  
  40.   
  41. T & operator[](int i)//以下标方式得到某索引所对应的值,如list[0]表示第一个位置的值  
  42. T   takeAt(int i)//删除i索引对应的值,并返回该值,以下两个类似  
  43. T   takeFirst()  
  44. T   takeLast()  
  45. T & back()//得到列表末尾的值  
  46. T & first()  
  47. T & front()  
  48. T & last()  
  49. T   value(int i) const  
  50. T   value(int i, const T & defaultValue) const  
  51. const T &   operator[](int i) const  
  52. const T &   at(int i) const  
  53. const T &   back() const  
  54. const T &   first() const  
  55. const T &   front() const  
  56. const T &   last() const  
  57. QSet<T>       toSet() const  
  58. std::list<T>  toStdList() const  
  59. QVector<T>    toVector() const  
  60. QList<T>  operator+(const QList<T> & other) const  
  61. QList<T> &    operator+=(const QList<T> & other)  
  62. QList<T> &    operator+=(const T & value)  
  63. QList<T> &    operator<<(const QList<T> & other)  
  64. QList<T> &    operator<<(const T & value)  
  65. QList<T> &    operator=(const QList<T> & other)  
  66. QList &     operator=(QList<T> && other)  
  67. bool        operator==(const QList<T> & other) const  
  68. bool        operator!=(const QList<T> & other) const  
  69.   
  70. const_iterator  begin() const  
  71. const_iterator  cbegin() const  
  72. const_iterator  cend() const  
  73. const_iterator  constBegin() const  
  74. const_iterator  constEnd() const  
  75. const_iterator  end() const  
  76. iterator    end()  
  77. iterator    erase(iterator pos)  
  78. iterator    erase(iterator begin, iterator end)  
  79. iterator    begin()  
  80. iterator    insert(iterator before, const T & value)  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值