一:Qt中有很多基于模板的容器类,分为顺序容器类和关联容器类,QList 容器是最常用的顺序容器,它以数组列表的形式实现, 将我们需要存储的各种类型数据一次按顺序存放到堆栈中
二 :QList的使用,直接上代码
QList list;
//插入
list << 1 << 2 << 3 << 4 << 5;
//删除
//将删除2
list.removeAt(1);
//替换 如将1替换成0
list.replace(0,0);
//查找
//bhas1 返回true,bHas2返回false
bool bHas1 = list.contains(5);
bool bhas2 = list.contains(2);
qDebug() << "bHas1:" << bHas1 << "bHas2" << bhas2;
//指向
int nValue = list.at(0);
qDebug() << "nVlaue:" << nValue;
//遍历
for(auto x: list)
qDebug() << "遍历1:" << x;
//迭代
QList::const_iterator iterList1 = list.begin();
for(; iterList1 != list.end(); ++iterList1)
qDebug() << "迭代遍历1:" << *iterList1;
//或者
QListIterator iterList2(list);
while(iterList2.hasNext())
qDebug() << "迭代遍历2:" << iterList2.next();
输出:
bHas1: true bHas2 false
nVlaue: 0
遍历1: 0
遍历1: 3
遍历1: 4
遍历1: 5
迭代遍历1: 0
迭代遍历1: 3
迭代遍历1: 4
迭代遍历1: 5
迭代遍历2: 0
迭代遍历2: 3
迭代遍历2: 4
迭代遍历2: 5