主要说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 =;i<;i++)
{
listInt.append(i);
//也可以这样添加元素
//listInt<<i;
}
//删除
qDebug()<<"删除3";
listInt.removeAt();
//查询
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();
mutableiterator.insert();
mutableiterator.insert();
while(mutableiterator.hasNext())
{
int i= mutableiterator.next();
if(i==||i==)
{
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<<<<<<<<<<;
qDebug()<<"迭代器访问QLinkedListIterator";
QLinkedListIterator<int> iterator(link);
while(iterator.hasNext())
{
qDebug()<< iterator.next();
}
//删除第一个2
link.removeOne();
//添加两个3这两种方式一样
link.push_back();
link.append();
//删除所有的3
link.removeAll();
qDebug()<<"普通访问foreach";
foreach (int item, link) {
qDebug()<< item;
} qDebug()<<"迭代器QMutableLinkedListIterator";
QMutableLinkedListIterator<int> mutableIter(link); while(mutableIter.hasNext())
{
int i= mutableIter.next();
if(i==)
{
mutableIter.insert();
}
if(i==)
{
mutableIter.remove();
}
qDebug()<<i;
}
qDebug()<<"迭代器QMutableLinkedListIterator重新访问";
mutableIter.toFront();
while(mutableIter.hasNext())
{
int i= mutableIter.next();
qDebug()<<i;
}
//mutable
return a.exec();
}
a

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(,"a");
map.insert(,"b");
map.insert(,"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<<<<<<<<<<;
qSort(listStrs);
foreach (int i, listStrs) {
qDebug()<<i;
}
qDebug()<<"____________________________";
listStrs.clear();
listStrs<<<<<<<<<<;
qSort(listStrs.begin()+,listStrs.end()-);
foreach (int i, listStrs) {
qDebug()<<i;
} qDebug()<<"______________qCopy______________";
QVector<int> newVec();
qCopy(listStrs.begin(),listStrs.end(),newVec.begin());
foreach (int i, newVec) {
qDebug()<<i;
}
qDebug()<<"______________qFind______________";
listStrs.clear();
listStrs<<<<<<<<<<;
QList<int>::const_iterator iterFin=qFind(listStrs,);
if(iterFin!=listStrs.end())
{
qDebug()<<*iterFin;
}
else
{
qDebug()<<"notFound!";
}
return a.exec();
}

Qt学习笔记常用容器的更多相关文章

  1. Java学习笔记--常用容器

    容器 1. 出现原因 解决程序运行时需要创建新对象,在程序运行前不知道运行的所需的对象数量甚至是类型的问题. Java中提供了一套集合类来解决这些问题包括:List.Set.Queue.Map 2. ...

  2. springmvc学习笔记(常用注解)

    springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...

  3. Qt学习笔记-Widget布局管理

    Qt学习笔记4-Widget布局管理       以<C++ GUI Programming with Qt 4, Second Edition>为参考 实例:查找对话框 包含三个文件,f ...

  4. qt学习笔记(五) QGraphicsPixmapItem与QGraphicsScene的编程实例 图标拖动渐变效果

    应大家的要求,还是把完整的project文件贴出来,大家省点事:http://www.kuaipan.cn/file/id_48923272389086450.htm 先看看执行效果,我用的群创7寸屏 ...

  5. QT学习笔记(一)——Helloworld

    QT学习笔记(一)--Helloworld 一.调试的基本方法: Log调试法 --在代码中加入一定的打印语句 --打印程序状态和关键变量的值 断点调试法: --在开发环境中的对应代码行加上断点 -- ...

  6. qt学习笔记(七)之数据库简介(所有支持数据库类型的列表)

    笔者最近用Qt写公司的考勤机.本来要求是要基于frameBuffer下用自己开发的easyGUI来进行上层应用开发,但是考虑到easyGUI提供的接口不是很多,就考虑用Qt来开发,顺带练练手. 废话不 ...

  7. Docker学习笔记 - Docker容器内部署redis

    Docker学习笔记(2-4)Docker应用实验-redist server 和client的安装使用 一.获取redis容器(含客户端和服务端) 二.创建服务端容器 1.在终端A中运行redis- ...

  8. Qt学习笔记(2)-利用StackWidget实现选项卡式页面

    学习笔记第二篇,利用Qt实现选项卡式的页面,效果如图1.1-图1.3所示.程序实现的功能是通过点击状态栏实现不同页面的切换,实际上Qt中自带有Tab选项卡式的控件,本文利用StackWidge实现类似 ...

  9. Docker学习笔记 - Docker容器之间的连接

    学习目标: 容器之间可以相互连接访问:: --link redis:redisAlias 准备工作 FROM ubuntu:14.04 RUN apt-get install -y ping RUN  ...

随机推荐

  1. Java中的基本数据类型

    什么是基本数据类型 就是我们在编程的时候经常需要用到的数据类型,如整型,浮点型等,把这些数据类型专门拿出来特殊对待,并想象成所谓的“基本类型”. Java中有哪些基本数据类型

  2. Flex各类型坐标转换(全局、本地、内容坐标间转换)

    Flex包含3种坐标:全局坐标.本地坐标.内容坐标 全局坐标:stage级别,坐标原点为舞台的左上角,如MouseEvent的stageX.stageY坐标. 本地坐标:组件级别的坐标系,相对坐标,坐 ...

  3. HttpModule

    HttpModule是如何工作的 当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于HTTP请求来讲,Htt ...

  4. SQL server基础知识(表操作、数据约束、多表链接查询)

    SQL server基础知识 一.基础知识 (1).存储结构:数据库->表->数据 (2).管理数据库 增加:create database 数据库名称 删除:drop database ...

  5. Linux小技巧总结

    1.fdisk创建磁盘分区不重启系统partprobe 使用fdisk工具只是将分区信息写到磁盘,如果需要mkfs磁盘分区则需要重启系统才能够读取到/dev/sda*,而使用partprobe则可以使 ...

  6. x01.Lab.StoreApp: XP 停服,微软变脸

    变脸,川剧的一种表演形式,除了哄哄小孩,似乎别无用处.而川剧变脸从业者何其多也,存在时间何其长也.以如此多的从业者,如此长的时间,来进行科研,其成果一定是斐然吧.推而广之,试问天下谁能敌! 微软变脸, ...

  7. 用U盘制作启动盘后空间变小的恢复方法,清除U盘启动盘空间

    先把u盘插好,运行cmd, 输入diskpart,回车, (输入list disk,回车,能看到磁盘大致情况,u盘一般是磁盘1) 再输入select disk 1,回车, 再输入clean,回车, 关 ...

  8. Spring AOP 开发中遇到问题:Caused by: java.lang.IllegalArgumentException: warning no match for this type name: com.xxx.collector.service.impl.XxxServiceImpl [Xlint:invalidAbsoluteTypeName]

    在网上找了很多,都不是我想要的,后来发现是我在springaop注解的时候 写错了类名导致的这个问题 @Pointcut("execution(* com.xxx.collector.ser ...

  9. 【转载】阎焱:90后创业是扯淡 大量O2O和P2P公司濒临倒闭

    真正创业成功的大部分是年龄在30岁到38岁之间,很多90后基本什么都不懂.从历史来看,在这样的人口大国,集体性行为,无论是政治的还是经济的,基本都是导致灾难性后果. 10月14日消息,赛富基金创始首席 ...

  10. cuda并行计算的几种模式

    #include "cuda_runtime.h" #include "device_launch_parameters.h" #include <std ...