Qt容器类介绍

Qt容器类介绍

Qt提供了一些顺序容器:QList,QLinkedList,QVector,QStack和QQueue。 因为这些容器中的数据都是一个接一个线性存储的,所以称为顺序容器。

Qt还提供了一些关联容器:QMap,QMultiMap,QHash,QMultiHash和QSet。 因为这些容器存储的是<键,值>对,比如QMap<Key,T>,所以称为关联容器。

  1. Qlist

//main.cpp

#include <QCoreApplication>
#include<QList>
#include<QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QList<QString>list;
    list<<"aa"<<"bb"<<"cc";                //插入项目
if(list[1]=="bb") list[1]="ab";
list.replace(2,"bc");                      //将"cc"换为"bc"
qDebug()<<"this list is: ";                //输出整个列表
for(int i=0;i<list.size();++i)
{
    qDebug()<<list.at(i);                  //现在列表为aa ab bc
}

list.append("dd");                         //在列表尾部添加
list.prepend("mm");                         //在列表头部添加
QString str=list.takeAt(2);                //从列表中删除第3个项目,并获取它
qDebug()<<"at(2) item is: "<<str;
qDebug()<<"the list is: ";
for(int i=0;i<list.size();++i)
{
    qDebug()<<list.at(i);                   //现在列表为mm aa bc dd

}
list.insert(2,"mm");                        //在位置2插入项目
list.swap(1,3);                             //交换项目1和项目3
qDebug()<<"the list is:";
for(int i=0;i<list.size();++i)
{
    qDebug()<<list.at(i);                   //现在列表为mm bc mm aa dd
}
qDebug()<<"contains 'mm'?"<<list.contains("mm");   //列表中是否包含"mm"
qDebug()<<"the 'mm' count:"<<list.count("mm");     //包含"mm"的个数
//第一个"mm"的位置,默认从位置0开始往前查找,返回第一个匹配的项目的位置
qDebug()<<"the first 'mm' index: "<<list.indexOf("mm");
//第二个"mm"的位置,我们指定从位置1开始往前查找
qDebug()<<"the second 'mm' index: "<<list.indexOf("mm",1);
    return a.exec();
}

程序运行结果:

this list is:

"aa"

"ab"

"bc"

at(2) item is:  "ab"

the list is:

"mm"

"aa"

"bc"

"dd"

the list is:

"mm"

"bc"

"mm"

"aa"

"dd"

contains 'mm'? true

the 'mm' count: 2

the first 'mm' index:  0

the second 'mm' index:  2

 

  1. QMap

//main.cpp

#include <QCoreApplication>
#include<QMap>
#include<QMultiMap>
#include<QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QMap<QString,int>map;
    map["one"]=1;                 //向map中插入("one",1)
    map["three"]=3;
    map.insert("seven",7);        //使用insert()函数进行插入
    //获取键的值,使用"[]"操作符时,如果map中没有该键,那么会自动插入
    int value1=map["six"];
    qDebug()<<"value1:"<<value1;
    qDebug()<<"contains 'six'?"<<map.contains("six");
    //使用value函数获取键的值,这样当键不存在时不会自动插入
    int value2=map.value("five");
    qDebug()<<"value2"<<value2;
    qDebug()<<"contains 'five'?"<<map.contains("five");
    //当键不存在时,value()默认返回0,这里可以设定该值,比如这里设置为9
    int value3=map.value("nine",9);
    qDebug()<<"value3:"<<value3;
    //map是一个键对应一个值,如果重新给该键设置了值,那么以前的会被擦除
    map.insert("ten",10);
    map.insert("ten",100);
    qDebug()<<"ten:"<<map.value("ten");
    //可以使用insertMulti()函数来实现一键多值,然后使用values()函数来获取值的列表
    map.insertMulti("two",2);
    map.insertMulti("two",4);
    QList<int>values=map.values("two");
    qDebug()<<"two:"<<values;
    //也可以使用QMultiMap类来实现一键多值
    QMultiMap<QString,int>map1,map2,map3;
    map1.insert("values",1);
    map1.insert("values",2);
    map2.insert("values",3);
    //可以进行相加,这样map3的“values”键将包含2,1,3三个值
    map3=map2+map1;
    QList<int>myValues=map3.values("values");
    qDebug()<<"the values are:";
    for(int i=0;i<myValues.size();++i)
    {
        qDebug()<<myValues.at(i);
    }

    return a.exec();
}

程序运行结果:

value1: 0

contains 'six'? true

value2 0

contains 'five'? false

value3: 9

ten: 100

two: (4, 2)

the values are:

2

1

3

 

STL风格迭代器

  1. STL风格迭代器兼容Qt和STL的通用算法,而且在速度上进行了优化。

容器

只读迭代器

读/写迭代器

QList<T>,QQueue<T>

QList<T>::const_iterator

QList<T>::iterator

QLinkedList<T>

QLinkedList<T>::const_ iterator

QLinkedList<T>::iterator

QVector<T>,QStack<T>

QVector<T>:: const_ iterator

QVector<T>::iterator

QSet<T>

QSet<T>::const_iterator

QSet<T>::iterator

QMap<Key,T>,

QMultiMap<Key,T>

QMap<Key,T>::const_iterator

QMap<Key,T>::iterator

QHash<Key,T>,

QMultiHash<Key,T>

QHash<Key,T>:: const_iterator

QHash<Key,T>::iterator

//main.cpp

#include <QCoreApplication>
#include<QList>
#include<QDebug>
#include<QMap>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QList<QString>list;
    list<<"A"<<"B"<<"c"<<"D";
    QList<QString>::iterator i;   //使用读写迭代器
    qDebug()<<"the forward is:";
    for(i=list.begin();i!=list.end();++i)
    {
        *i=(*i).toLower();         //使用QString的toLower()函数转换为小写
        qDebug()<<*i;              //结果为a,b,c,d
    }
    qDebug()<<"the backward is:";
    while(i!=list.begin()){
        --i;
        qDebug()<<*i;                 //结果为d,c,b,a
    }
    QList<QString>::const_iterator j;         //使用只读迭代器
    qDebug()<<"the forward is:";
    for(j=list.constBegin();j!=list.constEnd();++j)
        qDebug()<<*j;                 //结果为a,b,c,d
    QMap<QString,int>map;
    map.insert("one",1);
    map.insert("two",2);
    map.insert("three",3);
    QMap<QString,int>::const_iterator p;
    qDebug()<<"the forward is: ";
    for(p=map.constBegin();p!=map.constEnd();++p)
        qDebug()<<p.key()<<":"<<p.value();         //结果为(one,1),(three,3),(two,2)
    return a.exec();}

程序运行结果:

the forward is:

"a"

"b"

"c"

"d"

the backward is:

"d"

"c"

"b"

"a"

the forward is:

"a"

"b"

"c"

"d"

the forward is:

"one" : 1

"three" : 3

"two" : 2

 

  1. foreach关键字
#include <QCoreApplication>
#include<QList>
#include<QMap>
#include<QMultiMap>
#include<QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QList<QString>list;
    list.insert(0,"A");
    list.insert(1,"B");
    list.insert(2,"C");
    qDebug()<<"the list is:";
    foreach(QString str,list){           //从list中获取每一项
        qDebug()<<str;                   //结果为A,B,C
    }
    QMap<QString,int>map;
    map.insert("first",1);
    map.insert("second",2);
    map.insert("third",3);
    qDebug()<<endl<<"the map is:";
    foreach(QString str,map.keys())           //从map中获取每一个键
        //输出键和对应的值,结果为(first,1),(second,2),(third,3)
        qDebug()<<str<<":"<<map.value(str);
QMultiMap<QString,int>map2;
map2.insert("first",1);
map2.insert("first",2);
map2.insert("first",3);
map2.insert("second",2);
qDebug()<<endl<<"the map2 is:";
QList<QString>keys=map2.uniqueKeys();         //返回所以键的列表
foreach(QString str,keys){                    //遍历所有的键
    foreach(int i,map2.values(str))           //遍历键中所有的值
        qDebug()<<str<<":"<<i;
}
    return a.exec();
}

程序运行结果:

the list is:

"A"

"B"

"C"

 

the map is:

"first" : 1

"second" : 2

"third" : 3

 

the map2 is:

"first" : 3

"first" : 2

"first" : 1

"second" : 2

  1. 通用算法
#include <QCoreApplication>
#include<QVector>
#include<QStringList>
#include<QDebug>
#include<algorithm>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QStringList list;
    list<<"one"<<"two"<<"three";
    qDebug()<<QObject::tr("std::copy算法:");
    QVector<QString>vect(3);
    //将list中所有项目复制到vect中
    std::copy(list.begin(),list.end(),vect.begin());
    qDebug()<<vect;         //结果为one,two,three
    qDebug()<<endl<<QObject::tr("std::equal算法:");
    //从list的开始到结束的所有项目与vect的开始及其后面的等数量的项目进行比较
    //全部相同则返回true
    bool ret1=std::equal(list.begin(),list.end(),vect.begin());
    qDebug()<<"equal:"<<ret1;         //结果为true
    qDebug()<<endl<<QObject::tr("std::find算法");
    //从list中查找"two",返回第一个对应的值的迭代器,如果没有找到则返回end()
    QList<QString>::iterator i=std::find(list.begin(),list.end(),"two");
    qDebug()<<*i;                    //结果为"two"
    qDebug()<<endl<<QObject::tr("std::fill算法");
    //将list中的所有项目填充为"eleven"
    std::fill(list.begin(),list.end(),"eleven");
    qDebug()<<list;                  //结果eleven,eleven,eleven
    QList<int>list1;
    list1<<3<<3<<6<<6<<6<<8;
    qDebug()<<endl<<QObject::tr("std::count算法");
    int countOf6=std::count(list1.begin(),list1.end(),6);     //查找6的个数
    qDebug()<<"countOf6:"<<countOf6;                //结果为3
    qDebug()<<endl<<QObject::tr("std::lower_bound算法:");
    //返回第一个出现5的位置,如果没有5,则返回5应该在的位置
    //list1被查找的范围中的项目必须是升序
    QList<int>::iterator j=std::lower_bound(list1.begin(),list1.end(),5);
    list1.insert(j,5);
    qDebug()<<list1;                       //结果3,3,5,6,6,6,8
    QList<int>list2;
    list2<<33<<12<<68<<6<<12;
    qDebug()<<endl<<QObject::tr("std::sort算法");
    //使用快速排序算法对list2进行升序排序,排序后两个12的位置不确定
    std::sort(list2.begin(),list2.end());
    qDebug()<<list2;                      //结果6,12,12,33,68
    qDebug()<<endl<<QObject::tr("std::stable_sort算法");
    //使用一种稳定排序算法对list2进行升序排序
    //排序前在前面的12,排序后依然在前面
    std::stable_sort(list2.begin(),list2.end());
    qDebug()<<list2;              //结果6,12,12,33,68
    qDebug()<<endl<<QObject::tr("std::greater算法");
    //可以在qSort()算法中使其反向排序
    qSort(list2.begin(),list2.end(),std::greater<int>());
    qDebug()<<list2;         //结果68,33,12,12,6
    qDebug()<<endl<<QObject::tr("std::swap算法");
    double pi=3.14;
    double e=2.71;
    std::swap(pi,e);                //交换pi和e的值
    qDebug()<<"pi:"<<pi<<"e:"<<e;
    return a.exec();
}

程序运行结果:

"std::copy算法:"

QVector("one", "two", "three")

 

"std::equal算法:"

equal: true

 

"std::find算法"

"two"

 

"std::fill算法"

("eleven", "eleven", "eleven")

 

"std::count算法"

countOf6: 3

 

"std::lower_bound算法:"

(3, 3, 5, 6, 6, 6, 8)

 

"std::sort算法"

(6, 12, 12, 33, 68)

 

"std::stable_sort算法"

(6, 12, 12, 33, 68)

 

"std::greater算法"

(68, 33, 12, 12, 6)

 

"std::swap算法"

pi: 2.71 e: 3.14

猜你喜欢

转载自blog.csdn.net/Ezra1991/article/details/81781024