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 ...
随机推荐
- jquery.serialize() 函数详解
jQuery - serialize() 方法 W3School给出的定义与用法: serialize() 方法通过序列化表单值,创建 URL 编码文本字符串. 您可以选择一个或多个表单元素(比如 i ...
- yii2集成富文本编辑器redactor
作者:白狼 出处:http://www.manks.top/article/yii2_redactor本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保 ...
- DATETIME类型和BIGINT 类型互相转换
项目中使用BIGINT来存放时间,以下代码用来转换时间类型和BIGINT类型 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========= ...
- Java并发之BlockingQueue 阻塞队列(ArrayBlockingQueue、LinkedBlockingQueue、DelayQueue、PriorityBlockingQueue、SynchronousQueue)
package com.thread.test.thread; import java.util.Random; import java.util.concurrent.*; /** * Create ...
- my_atoi()
void my_atoi(const char* s){ int i=0,res=0; if(*s<='9' && *s>='0'){ //如果输入的一个字符是数字 for ...
- Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'解决
安装上mysql后,报 Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock',试了网上的方法 ...
- Eclipse常用的十个方便的快捷键
Ctrl+F:在当前代在cg中查找关键字 Ctrl+H:打开查找窗口 Ctrl+/: 屏蔽代码(注释): (以下转自:http://wenku.baidu.com/view/d291ade3172de ...
- 虚拟机centos6.5 --hadoop2.6集群环境搭建
一.环境说明 虚拟机:virtualBox 系统:centos6.5,64位 集群:3个节点 master 192.168.12.232 slave01 192.168.12.233 slave02 ...
- 证明你是你——快速开启Windows Azure多重身份验证
中国版Windows Azure的多重身份验证(Multi-Factor Authentication)功能已经开放.这个功能说白了就是要“证明你是你”.目前可以支持以下几种验证方式: 手机,短信验证 ...
- .Net程序员之Python基础教程学习----字符串的使用 [Second Day]
在The FirstDay 里面学习了列表的元组的使用,今天开始学习字符串的使用.字符串的使用主要要掌握,字符串的格式化(C语言中我们应该都知道,Python和C语言差别不大),字符串的基本 ...