Qt 常用容器
来源:互联网 发布:淘宝网休闲运动套装 编辑:程序博客网 时间:2024/05/17 05:11
主要说Qt的以下几种容器
1.QList<T>
2.QLinkedList<T>
3.Map<T>
和一些常用的容器方法的使用
qSort
qCopy
qFind
1.QList<T>泛型集合是最常用的一种容器
看一下它的常用 操作
添加删除和两个迭代器
QListIterator和QMutableListIterator
#include <QCoreApplication>#include<QList>#include<QDebug>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QList<int> listInt; //添加 for(int i =0;i<10;i++) { listInt.append(i); //也可以这样添加元素 //listInt<<i; } //删除 qDebug()<<"删除3"; listInt.removeAt(3); //查询 foreach (int item, listInt) { qDebug()<<item; } qDebug()<<"Iterator"; //迭代器 QListIterator<int> iterator(listInt); while(iterator.hasNext()) { qDebug()<<iterator.next(); if(iterator.hasNext()) qDebug()<<"the Next is :"<<iterator.peekNext(); } //返转 iterator.toBack(); while(iterator.hasPrevious()) { qDebug()<<iterator.previous(); } qDebug()<<"可变迭代器QMutableListIterator"; //可变的迭代器 QMutableListIterator<int> mutableiterator(listInt); mutableiterator.insert(13); mutableiterator.insert(14); mutableiterator.insert(15); while(mutableiterator.hasNext()) { int i= mutableiterator.next(); if(i==2||i==6) { mutableiterator.remove(); } } //查询 foreach (int item, listInt) { qDebug()<<item; } return a.exec();}
2.QLinkedList<T>
QLinkedList<T>和QList<T>差不多,不同的一点是它是用迭代器做的访问项
也就是说QList<int> list只以通过这样访问它的内容list[i]而QLinkedList不可以只能用Iterator
性能上它要高于QList<T>
#include <QCoreApplication>#include<QLinkedList>#include<QDebug>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QLinkedList<int> link; link<<1<<2<<2<<3<<4; qDebug()<<"迭代器访问QLinkedListIterator"; QLinkedListIterator<int> iterator(link); while(iterator.hasNext()) { qDebug()<< iterator.next(); } //删除第一个2 link.removeOne(2); //添加两个3这两种方式一样 link.push_back(3); link.append(3); //删除所有的3 link.removeAll(3); qDebug()<<"普通访问foreach"; foreach (int item, link) { qDebug()<< item; } qDebug()<<"迭代器QMutableLinkedListIterator"; QMutableLinkedListIterator<int> mutableIter(link); while(mutableIter.hasNext()) { int i= mutableIter.next(); if(i==1) { mutableIter.insert(90); } if(i==4) { mutableIter.remove(); } qDebug()<<i; } qDebug()<<"迭代器QMutableLinkedListIterator重新访问"; mutableIter.toFront(); while(mutableIter.hasNext()) { int i= mutableIter.next(); qDebug()<<i; } //mutable return a.exec();}
3Map<T>
map类型是一个键值对 key/value组成 其它的和上边的两个集合没什么区别
#include <QCoreApplication>#include<QMap>#include<QDebug>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QMap<int,QString> map; map.insert(1,"a"); map.insert(2,"b"); map.insert(3,"c"); QMutableMapIterator<int,QString> mutableIte(map); while(mutableIte.hasNext()) { mutableIte.next(); qDebug()<<mutableIte.key()<<" "<<mutableIte.value(); } return a.exec();}
下边说一下常用的集合操作方法
qSort
qCopy
qFind
#include <QCoreApplication>#include<QList>#include<QDebug>#include<QVector>int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QList<int> listStrs; listStrs<<10<<5<<8<<2<<7; qSort(listStrs); foreach (int i, listStrs) { qDebug()<<i; } qDebug()<<"____________________________"; listStrs.clear(); listStrs<<10<<5<<8<<2<<7; qSort(listStrs.begin()+1,listStrs.end()-1); foreach (int i, listStrs) { qDebug()<<i; } qDebug()<<"______________qCopy______________"; QVector<int> newVec(5); qCopy(listStrs.begin(),listStrs.end(),newVec.begin()); foreach (int i, newVec) { qDebug()<<i; } qDebug()<<"______________qFind______________"; listStrs.clear(); listStrs<<2<<5<<8<<2<<7; QList<int>::const_iterator iterFin=qFind(listStrs,2); if(iterFin!=listStrs.end()) { qDebug()<<*iterFin; } else { qDebug()<<"notFound!"; } return a.exec();}
0 0
- Qt 常用容器
- Qt学习笔记常用容器
- Qt中的常用容器类
- QT容器
- qt 容器
- Qt:容器
- QT 容器
- Qt容器
- QT容器
- 容器常用
- 常用容器
- 常用容器
- QT序列式容器
- Qt 容器类
- QT的容器
- QT容器类
- Qt 容器类
- Qt容器类
- ExtDeprecationWarning: Importing flask.ext.bootstrap is deprecated, use flask_bootstrap instead.
- 笔试题72. LeetCode OJ (59)
- Fragment 与FragmentActivity通信封装
- 关于freeMark转义字符的处理
- RHEL6.1 安装 Oracle10gr2 (图文、解析)
- Qt 常用容器
- 内部类、成员内部类、局部内部类
- Bitcode导致的编译报错
- grunt sass的invalid GBK character 错误解决方法
- ext6.0+cordova+android 第一章 基础Cordova基础
- kettle
- JNI各类型参数互相调用
- 故障案例--mongodb3.0 mongorestore恢复数据库时hang住
- Jquery Pagination Ajax分页插件