QList
#include <QList>
【Demo1】foreach
QList<int> list;
for(int i = 0 ; i < 10; i++)
{
list.append(i);
}
list.removeOne(5);
foreach (int num, list) {
qDebug()<<num;
}
【Demo2】QListIterator
QList<int> list;
for(int i = 0 ; i < 10; i++)
{
list.append(i);
}
QListIterator<int> iter(list);
while(iter.hasNext()){
qDebug()<<iter.next();
if(iter.hasNext()){
qDebug()<<"next..."<<iter.peekNext();
}
}
iter.toBack();
while(iter.hasPrevious()){
qDebug()<<iter.previous();
if(iter.hasPrevious()){
qDebug()<<"next..."<<iter.peekPrevious();
}
}
【Demo3】QMutableListIterator
QList<int> list;
for(int i = 0 ; i < 10; i++)
{
list.append(i);
}
QMutableListIterator<int> iter(list);
while(iter.hasNext()){
int i=iter.next();
if(i==5){//此时iter已经是下一个
iter.remove();
}
}
iter.toFront();
while(iter.hasNext()){
qDebug()<<iter.next();
}
QLinkedList
#include <QLinkedList>
QLinkedList<int>list;
list<<1<<2<<3;
foreach (int i, list) {
qDebug()<<i;
}
QMap
#include <QMap>
QMap<int,QString> map;
map.insert(1,"Bob");
map.insert(2,"Chad");
map.insert(3,"Mary");
foreach(int i,map.keys()){
qDebug()<<i<<map[i];
}
QMapIterator<int,QString> iter(map);
while(iter.hasNext()){
iter.next();
qDebug()<<iter.key()<<iter.value();
}
QHash
#include <QHash>
QHash<int,QString> hash;
hash.insert(1,"Bob");
hash.insert(2,"Chad");
hash.insert(3,"Mary");
qDebug()<<"3="<<hash.value(3);
foreach(int i,hash.keys()){
qDebug()<<i<<hash[i];
}
QHashIterator<int,QString> iter(hash);
while(iter.hasNext()){
iter.next();
qDebug()<<iter.key()<<iter.value();
}
QStringList
#include <QStringList>
QStringList list;
QString line="a,b,c,d,e";
list.append("Hello");
list=line.split(",");
list.replaceInStrings("b","batman");
QString after=list.join(";");
qDebug()<<after;
foreach(QString item,list){
qDebug() <<item;
}
QSet
#include <QSet>
QSet<QString> set;
set.insert("one");
set.insert("one");
set.insert("one");
set.insert("two");
set.insert("three");
QSetIterator<QString> i(set);
while (i.hasNext())
qDebug() << i.next();
qSort
QList<int> list;
list<<2<<3<<1<<0;
//qSort(list);
qSort(list.begin()+1,list.end()-1);
foreach(int i ,list){
qDebug()<<i;
}
qCopy
QStringList list;
list<<"a"<<"b"<<"c"<<"d"<<"e";
QVector<QString> vect(3);
qCopy(list.begin()+1,list.end()-2,vect.begin()+1);
foreach(QString item ,vect){
qDebug()<<item;
}
qFill
QVector<QString> vect(5);
// qFill(vect,"hello world");
qFill(vect.begin()+1,vect.end()-1,"hello");
foreach(QString item ,vect){
qDebug()<<item;
}
网友评论