QT开发STL

容器类
Qt的容器类比STL(标准模板库)种的容器类更轻巧、安全和易于使用。

1
2
3
4
5
QList<QString> aList;
aList.append("Monday");
aList.append("uesday");
aList.append("Wednesday");
QString str=aList[0];qDebug()<<str; //显示"Monday'

顺序容器通过元素在容器中的位置顺序存储和访问,没有key值概念的
关联容器通过键(key)存储和读取元素(有key值和value是同一个值,set的时候,只能看到一个值,这种就不存在关联;其他都有key值,找到value值)
Qt的容器类分为顺序容器和关联容器
顺序容器:QList(指针),QLinkedList(链表),QVector(),QStack(栈)和QQueue(队列)
关联容器QMap,QMultiMap,QHash,QMultiHash和QSet

QLis:数组列表

1
2
3
4
QList<QString> list;
list<<"one"<<"two"<<"three";
QString str1=list[1]; //str1 == "two"
QString str0=list.at(0); //str0 == "one"

QLinkedList: 除了不提供下标索引的数据访问,和QList其他函数接口基本相同
QVector : 函数接口与QList几乎完全相同,访问性能更高,因为是连续存储.但是插入和删除不方便

QStack:LIFO

1
2
3
4
5
6
QStack<int> stack;
stack.push(10);
stack.push(20);
stack.push(30);
while(!stack.isEmpty())
qDebug()<<stack.pop();

Queue:FIFO 先进先出

1
2
3
4
5
6
QQueue<int> queue;
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
while(!queue.isEmpty())
qDebug()<<queue.dequeue();

QSet:基于散列表

1
2
3
4
QSet<QString> set;
set<<"dog"<<"cat"<<"tiger"
if(set.contains("cat"))
qDebug()<<"the set has a cat";

QMap:按键值的顺序存储(如果插入重复的值,就忽略,键值是唯一的)

1
2
3
4
5
6
7
8
9
10
11
QMap<QString,int> map;
map["one"]=1; //如果不存在就会创建
map["two"]=2;
map["three"]=3;
map.insert("four",4);
map.remove("two"); //删除"two"
int num1=map["one"];
int num2=map.value["two"]; //没有"two",就给默认值 0
int num3=map["five"]; //没有"five",就给默认值 0
int timeout=map.value("TIMEOUT"30);//没有TIMEOUT直接给个30
qDebug()<<num1<<num2<<num3<<timeout;//1 0 0 30

key: one two three four
value: 1 2->0 3 4

QMultiMap: QMap的子类,键值可以重复

1
2
3
4
5
6
7
8
9
QMultiMap<QStringint> map1,map2,map3;
map1.insert("plenty",100);
map1.insert("plenty",2000);
map2.insert("plenty",5000);
map3=map1+map2; //键值可以重复,所以这里map1和map2加在一起给map3
qDebug()<<map3.size();//3
QList<int> values=map3.values("plenty");//将plenty的值存储到values数组表中
foreach(int i,values) //values 一个一个放入i中,然后打印出来
qDebug()<<i; //5000 2000 100

QHash必须提供”==”于QMap用法类似,QMultiHash与QMultiMap用法类似

3.4容器类的迭代
迭代器就是为访问容器里面的数据,提供的一种方法。QT有两种迭代器:java类型的迭代器(易于使用)和STL类型的迭代器(效率高)
Java类型迭代器


<![endif]>

容器类 只读迭代器 读写迭代器
QList<T>QQueue<T> QListlterator<T> QMutableListlterator<T>
QLinkedList<T> QLinkedListlterator<T> QMutableLinkedListlterator<T>
QVector<T>QStack<T> QVectorlterator<T> QMutableVectorlterator<T>
Qset<T> QSetiterator<T> QMutableSetiterator<T>
QMap<Key,T>QMultiMap<Key,T> QMaplterator<Key,T> QMutableMaplterator<Key,T>
Qhash<Key,T>QMultiHash<Key,T> QHashlterator<Key,T> QMutableHashlterator<Key,T>
1
2
3
4
5
6
7
QList<QString> list;
......
QListIterator<QString> i(list);
while(i.hasNext()) //判断在迭代器指针后面是否还有数据项 必须配合使用
{
qDebug()<<i.next(); //跳过一个数据项,并返回其值
}

反向遍历

1
2
3
4
5
QListIterator<QString> i(list);
while(i.hasPrevious()) //判断前面是否有元素
{
qDebug<< i.previous();//跳过当前元素,并返回值
}

<![endif]>

常用函数 功能
void toFront() 迭代器移动到列表的最前面(第一个数据项之前)
void toBack() 迭代器移动到列表的最后面(最后一个数据项之后)
bool hasNext() 如果迭代器不是位于列表最后位置,返回true
const T& next() 返回下一个数据项,并且迭代器后移一个位置
const T&peekNext() 返回下一个数据项,但是不移动迭代器位置
bool hasPrevious() 如果迭代器不是位于列表最前面,返回true
const T& previous() 返回前一个数据项,并且迭代器前移一个位置
const T& peekPrevious() 返回前一个数据项,但是不移动迭代器指针

//删除奇数项

1
2
3
4
5
6
7
8
9
10
11
QList<int> list;
list<<1<<2<<3<<4<<5;
QMutableListIterator<int> i(list);

whie(i.hasNext())
{
if(i.next() % 2 != 0)
{
i.remove();//remove()函数移除next()函数刚刚跳过的一个数据项,不会是迭代器失效。setValue()函数可以修改刚刚跳过去的数据项的值
}
}

容器内数据: 2 4;

关联容器类的迭代器的使用
具有上表所示的所有函数,主要是增加了key()和value()函数用于获取刚刚跳过的数据项的键和值

1