Qt学习笔记常用容器
主要说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学习笔记常用容器的更多相关文章
- Java学习笔记--常用容器
容器 1. 出现原因 解决程序运行时需要创建新对象,在程序运行前不知道运行的所需的对象数量甚至是类型的问题. Java中提供了一套集合类来解决这些问题包括:List.Set.Queue.Map 2. ...
- springmvc学习笔记(常用注解)
springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...
- Qt学习笔记-Widget布局管理
Qt学习笔记4-Widget布局管理 以<C++ GUI Programming with Qt 4, Second Edition>为参考 实例:查找对话框 包含三个文件,f ...
- qt学习笔记(五) QGraphicsPixmapItem与QGraphicsScene的编程实例 图标拖动渐变效果
应大家的要求,还是把完整的project文件贴出来,大家省点事:http://www.kuaipan.cn/file/id_48923272389086450.htm 先看看执行效果,我用的群创7寸屏 ...
- QT学习笔记(一)——Helloworld
QT学习笔记(一)--Helloworld 一.调试的基本方法: Log调试法 --在代码中加入一定的打印语句 --打印程序状态和关键变量的值 断点调试法: --在开发环境中的对应代码行加上断点 -- ...
- qt学习笔记(七)之数据库简介(所有支持数据库类型的列表)
笔者最近用Qt写公司的考勤机.本来要求是要基于frameBuffer下用自己开发的easyGUI来进行上层应用开发,但是考虑到easyGUI提供的接口不是很多,就考虑用Qt来开发,顺带练练手. 废话不 ...
- Docker学习笔记 - Docker容器内部署redis
Docker学习笔记(2-4)Docker应用实验-redist server 和client的安装使用 一.获取redis容器(含客户端和服务端) 二.创建服务端容器 1.在终端A中运行redis- ...
- Qt学习笔记(2)-利用StackWidget实现选项卡式页面
学习笔记第二篇,利用Qt实现选项卡式的页面,效果如图1.1-图1.3所示.程序实现的功能是通过点击状态栏实现不同页面的切换,实际上Qt中自带有Tab选项卡式的控件,本文利用StackWidge实现类似 ...
- Docker学习笔记 - Docker容器之间的连接
学习目标: 容器之间可以相互连接访问:: --link redis:redisAlias 准备工作 FROM ubuntu:14.04 RUN apt-get install -y ping RUN ...
随机推荐
- mac版 android破解软件下载安装
1 apktool下载安装 下载地址https://code.google.com/p/android-apktool/ [1].下载apktool.jar — 解压 [2].下载Mac上的辅助工具a ...
- Oracle BIEE 环境迁移所导致的账号登陆问题的解决
系统版本 系统版本:11G(11.1.1.9) 问题描述 将系统数据(RPD.catalog等数据)迁移到另一环境(版本同样为11G)后,老系统weblogic控制台中添加的账户在新系统(仪表盘)中无 ...
- 【CSharp】C#中equals与==小记
序: 昨天技术群中的一个小伙伴发了几个字符串以及值类型比较的面试题,没想到我们的答案不尽人意...下面是截图以及答案,看看与各位看官的答案是否相同. ...
- oracle和postgresql 递归查询父子关系记录语法区别
oracle: 一.数据 db数据字段如下: task_id task_name t.parent_task_id *** *** ...
- C#调用自定义表类型参数
-SQL SERVER生成测试环境: --创建测试DB CREATE database Sales; go USE Sales GO --创建表类型 IF TYPE_ID('LocalDT') IS ...
- ARM体系结构
工作模式_ufisaus USR(User) :正常程序的执行状态 FIQ(Fast interrupt) :用于高速数据传输和通道处理 IRQ(Interrupt) :通常的中断处理 SVC(Sup ...
- yum或apt基本源设置指南
关于: 管理Linux服务器的运维或开发人员经常需要安装软件,最常用方式应该是通过Linux系统提供的包管理工具来在线安装,比如centos的yum,ubuntu或debian的apt-get.当然这 ...
- POJ1386Play on Words[有向图欧拉路]
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11846 Accepted: 4050 De ...
- 使用mxmlc在命令行编译.as代码
在cmd命令行环境下,敲mxmlc出现 提示Error: could not find JRE和"Error: could not find Java 2 Runtime Envi 解决办法 ...
- [No000049]狗日的中年——姜文
文件名 大小 [No000049]狗日的中年——姜文.7z 228KB