前言
QT的容器比起STL的容器更輕巧、安全和易於使用。
用容器存放數據,命令,對象或是別的什麼東西。。。不用數組,也不自己建立鏈表那就使用容器來做這些事——到底實際上用容器還是自己寫數據結構哪種更好這個我真不清楚,學了很久還是感覺什麼都不會。。。對了,想到需要使用諸如查找,刪改,添加,排序等操作還是使用容器吧,自己實現多無聊。
建一個Qt console application項目,來測試QT容器類。QT的容器類有兩種,順序容器sequential containers和關聯容器associative containers。
QList,QLinkedList,QVector,QStack,QQueue都是順序容器。
QMap,QMultiMap,QHash,QMultiHash,QSet都是關聯容器。
1.順序容器
QList
#include <QCoreApplication>
#include <QList>
#include <QLinkedList>
#include <iostream>
#include <iomanip>
#include <QtDebug>
using namespace std;
#define out qDebug()
void test01_QList(){
QList<QString> list;
//QStringList list;
//1.用<<添加
list<<"one"<<"two"<<"three";
QString str0=list[0];
QString str1=list.at(1);
QString str2=list[2];
//2.用push_back添加元素
for(int i=0;i<10;i++)
list.push_back(QString("%1").arg(i));
//3.用append添加元素
list.append(QString("10"));
//4.用insert添加元素
list.insert(list.length()-1,QString("[12]"));
//1.用qDebug()來進行輸出
//qDebug()輸出後面會被自動添加一個換行
//並且輸出的字符串有雙引號
qDebug()<<str0<<endl<<str1; //"one" "two"
out<<str2; //"three"
//2.用cout和toStdString()來進行輸出
cout<<str0.toStdString()<<endl; //one
cout<<str1.toStdString().data()<<endl;//two
cout<<"--------------------------------"<<endl;
//1.用iterator遍歷
for(QList<QString>::iterator it=list.begin();it!=list.end();it++)
cout<<setw(6)<<it->toStdString();
cout<<endl<<"--------------------------------"<<endl;
list.replace(list.length()-1,QString("[67]"));
list.sort(Qt::CaseInsensitive);//或者Qt::CaseSensitive
//2.用auto 遍歷
for(auto it : list)
cout<<setw(6)<<it.toStdString();
cout<<endl<<"--------------------------------"<<endl;
list.clear();
cout<<"list's length is "<<list.length()<<endl;
}
int main(int argc, char *argv[])
{
//QCoreApplication a(argc, argv);
cout<<"-----------------hello world----------------"<<endl;
//測試容器QList
test01_QList();
cout<<"----------------program end-----------------"<<endl;
return 1;//a.exec();如果寫了exec那麼按了什麼都一樣卡在這裏
}
控制檯結果:
-----------------hello world----------------
"one"
"two"
"three"
one
two
--------------------------------
one two three 0 1 2 3 4 5 6 7 8 9 [12] 10
--------------------------------
0 1 2 3 4 5 6 7 8 9 [12] [67] one three two
--------------------------------
list's length is 0
----------------program end-----------------
QLinkedList
void test02_QLinkedList(){
QLinkedList<qint32> list;
for(int i=0;i<10;i++)
list.push_front(i);
for(auto i : list)
cout<<i;
cout<<endl;
QLinkedList<qint32>::iterator it=list.begin();
list.erase(++it);//刪除第二個元素
for(auto i : list)
cout<<i;
cout<<endl;
cout<<list.first()<<endl;//第一個元素
list.prepend(4);//前面加一個元素4
list.push_front(4);//前面加一個元素4
for(auto i : list)
cout<<i;
cout<<endl;
list.pop_back();//後面pop一個元素
for(auto i : list)
cout<<i;
cout<<endl;
cout<<list.removeOne(3)<<endl;//return true
for(auto i : list)
cout<<i;
cout<<endl;
list.clear();
cout<<"length = "<<list.size()<<"Are it empty ? "<<list.empty()<<endl;
}
控制檯結果:
-----------------hello world----------------
9876543210
976543210
9
44976543210
4497654321
1
449765421
length = 0Are it empty ? 1
----------------program end-----------------
QVector
void test03_QVector(){
QVector<char> v;
for(int i=0;i<20;i++)
v.prepend(char(i+'a'));
for(auto i :v)
cout<<i;
cout<<endl;
v.move(1,v.length()-1);//移動元素
for(auto i :v)
cout<<i;
cout<<endl;
QVector<char> a;
v.swap(a);//清空container
cout<<v.empty()<<endl;
}
控制檯結果:
-----------------hello world----------------
tsrqponmlkjihgfedcba
trqponmlkjihgfedcbas
1
----------------program end-----------------
QStack
void test04_QStack(){
QStack<float> s;
for(int i=0;i<20;i++)
s.push(i);
for(auto i:s)
cout<<i<<" "; //0 1 2 3...17 18 19
cout<<endl;
s.pop_back();//pop後面的
s.pop();//pop後面的
s.pop_front();//pop前面的
for(auto i:s)
cout<<i<<" "; //1 2 3...17
cout<<endl;
cout<<s.top()<<endl;//top在後面
cout<<"capacity:"<<s.capacity()<<",size:"<<s.size()<<",length:"<<s.length()<<endl;
}
控制檯結果:
-----------------hello world----------------
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
17
capacity:28,size:17,length:17
----------------program end-----------------
QQueue
void test05_QQueue(){
QQueue<float> q;
for(int i=0;i<20;i++)
q.push_front(i);//9 8 ... 1
for(auto i : q)
cout<<i<<" ";
cout<<endl;
q.dequeue();//從前面pop一個
q.pop_front();//從前面pop一個
q.enqueue(float(23.3));//後面添加一個
for(auto i : q)
cout<<i<<" ";
cout<<endl;
cout<<q[1]<<q.at(2)<<endl;
}
控制檯結果:
-----------------hello world----------------
19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 23.3
1615
----------------program end-----------------
2.關聯容器
QMap
void test06_QMap(){
QMap<int,QString> m;
for (int i=0;i<20;i++)
m.insert(i,QString("student%1").arg(i));
for(auto i : m)
cout<<i.toStdString()<<endl;
cout<<">>>"<<m.find(4)->toStdString()<<endl;
//添加元素
m[m.size()]="Demllie";
m[m.count()]="Suatin";
cout<<">>>"<<m.value(0).toStdString()<<endl;
for(QMap<int,QString>::iterator it=m.begin();it!=m.end();it++)
cout<<"key:"<<setw(5)<<it.key()<<",value:"<<setw(10)<<it.value().toStdString()<<endl;
}
控制檯結果:
-----------------hello world----------------
student0
student1
student2
student3
student4
student5
student6
student7
student8
student9
student10
student11
student12
student13
student14
student15
student16
student17
student18
student19
>>>student4
>>>student0
key: 0,value: student0
key: 1,value: student1
key: 2,value: student2
key: 3,value: student3
key: 4,value: student4
key: 5,value: student5
key: 6,value: student6
key: 7,value: student7
key: 8,value: student8
key: 9,value: student9
key: 10,value: student10
key: 11,value: student11
key: 12,value: student12
key: 13,value: student13
key: 14,value: student14
key: 15,value: student15
key: 16,value: student16
key: 17,value: student17
key: 18,value: student18
key: 19,value: student19
key: 20,value: Demllie
key: 21,value: Suatin
----------------program end-----------------
QMultiMap
void test07_QMultiMap(){
QMultiMap<int,int> m;
for(int i=0;i<10;i++)
m.insertMulti(i,i+1);
cout<<"Did the multi map have contain 12 ?"<<m.contains(12)<<endl;
for(QMultiMap<int,int>::iterator it=m.begin();it!=m.end();it++)
cout<<"key:"<<setw(3)<<it.key()<<" ,value:"<<setw(4)<<it.value()<<endl;
cout<<">>>"<<m.take(2)<<endl;
}
控制檯結果:
-----------------hello world----------------
Did the multi map have contain 12 ?0
key: 0 ,value: 1
key: 1 ,value: 2
key: 2 ,value: 3
key: 3 ,value: 4
key: 4 ,value: 5
key: 5 ,value: 6
key: 6 ,value: 7
key: 7 ,value: 8
key: 8 ,value: 9
key: 9 ,value: 10
>>>3
----------------program end-----------------
QHash
void test08_QHash(){
QHash<int,float> h;
for(int i=0;i<10;i++)
h.insert(i,2*i);
for(auto i:h)
cout<<setw(3)<<i;
cout<<endl;
for(QHash<int,float>::iterator it=h.begin();it!=h.end();it++)
cout<<"key:"<<setw(3)<<it.key()<<" ,value:"<<setw(4)<<it.value()<<endl;
}
QMultiHash
void test09_QMultiHash(){
QMultiHash<int,float> h;
for(int i=0;i<10;i++)
h.insertMulti(i,2*i);
for(auto i:h)
cout<<setw(3)<<i;
cout<<endl;
for(QHash<int,float>::iterator it=h.begin();it!=h.end();it++)
cout<<"key:"<<setw(3)<<it.key()<<" ,value:"<<setw(4)<<it.value()<<endl;
}
QSet
void test10_QSet(){
QSet<QString> s;
s<<"dog"<<"cat"<<"tiger"<<"lion"<<" ";
if(s.contains(" ")){
cout<<"capacity:"<<s.capacity()<<",size:"<<s.size()<<endl;
s.insert("apple");
s.insert("banana");
s<<"peach1"<<"peach2"<<"peach3";//重複的元素只留下一個
for(int i=0;i<10;i++)
s<<QString("peach%1").arg(i);
cout<<"capacity:"<<s.capacity()<<",size:"<<s.size()<<endl;
for(auto i:s)
cout<<setw(10)<<i.toStdString()<<endl;
cout<<"capacity:"<<s.capacity()<<",size:"<<s.size()<<endl;
s<<"qiyala";
for(auto i:s)
cout<<setw(10)<<i.toStdString()<<endl;
cout<<"capacity:"<<s.capacity()<<",size:"<<s.size()<<endl;
}
}
控制檯結果:
-----------------hello world----------------
capacity:17,size:5
capacity:17,size:17
peach7
cat
dog
peach9
peach1
peach4
banana
tiger
peach6
peach5
apple
peach3
peach0
peach8
lion
peach2
capacity:17,size:17
dog
peach2
banana
peach9
peach4
peach8
peach7
peach1
peach3
apple
tiger
peach6
peach5
cat
lion
qiyala
peach0
capacity:37,size:18
----------------program end-----------------