QT开发应用程序(4)-- 容器类(QList, QLinkedList, QVector, QMap, QHash)

QList、QLinkedList、QVector

QList、QLinkedList和QVector提供的操作相似,用QList就可以代替其它两个,QList比QVector更快。元素的储存位置彼此相邻时使用QVector。

QList list; 
list <<1<<2<<3; // list: [“1”, “2”, “3”]
for ( int i=0; i!=list.size(); ++i ) 
{ 
	if ( list.at(i) ==1) 
	{
	 cout << “Found 1 at position:<< i<< endl;
	} 
} 
遍历容器有两种方式,一种是java方式,一种是stl方式。
QList<int> list1 ;
list1 <<1<<2<<3<<4<<5;
// Java方式遍历        
 QListIterator<int> it(list1 );
 for(;it.hasNext();)
     qDebug()<<it.next();
QMutableListIterator<int> mi(list1 );
for(int j=0;j<10;++j)
        mi.insert(j);
  
QListIterator<int> it(list1 );
for(it.toFront();it.hasNext();)
        qDebug()<<it.next();
for(it.toBack();it.hasPrevious();)
{
    if(it.previous()%2 == 0)
         it.remove();
      else
         it.setValue(it.peekNext()*10);
}
for(it.toFront();it.hasNext();)
        qDebug()<<it.next();
// STL方式遍历代码:
QList<int>::iterator it;
for(it= list.begin();it!=list.end();i++)
    {
        qDebug()<<(*it);
        *i =(*it)*10;
    }
QList<int>::ConstIterator ci;
for(ci=list.constBegin();ci!=list.constEnd();ci++)
  qDebug()<<*ci;

QVector

 #include<QVector> 
 // 添加元素:      
QVector<QString> strArray;         
strArray.append("Hello"); //可以这样       
strArray<<"World!"; //也可以这样        
strArray<<"MyName"<<"is"<<"LEO";//也可以这样加上个元素        
//现在strArray总共有5个字符串元素,strArray.count()==5     
// 遍历:
QVector<QString>::iterator iter;  
for (iter=strArray.begin();iter!=strArray.end();iter++)  
{  
   qDebug() <<  *iter << "\0"}  
for (int i=0;i<strArray.size();i++)  
{  
   qDebug() <<  strArray[i] << "\0"} 
//插入:
    strArray.insert(1,"这就是在hello和world之间添加");
// 删除:
    strArray.remove(1); //删除第一个元素,从0开始
    strArray.remove(13); //从1开始,删除3个元素
//复制(取代):
    strArray.replace(1,"LEO"); //删除第一个元素,从0开始   

QMap

注意:
1)通过Key获取Value时:
当Key存在:返回对应的Value
当Key不存在:返回值类型所对应的“零”值
2)插入键值对时:
当Key存在:更新Value的值
当Key不存在:插入新的键值对

QMap<QString,QColor> map;
map.insert("AA",RGB(255,0,0));//插入数据
map.insert("BB",RGB(0,255,0));//插入数据
map["CC"] = RGB(0,0,255); //另一种添加的方式 
QColor color = QColor(map["AA"]);
QMap<QString,QColor>::Iterator it = map.begin();
int i=0;
while(it!=map.end())
{
	qDebug()<<it.key();//"AA"
	qDebug()<<((QColor)it.value).name();//#FF0000...,注意类型转换
	it++;
}
auto find_index = map.find("as");//搜索
if(find_index!=m_map.end()) {//返回为end表明未搜索到
	qDebug()<<find_index.key()<<find_index.value();
}
// JAVA 方式遍历
QMapIterator<QString,QColor> it(map);
for(;it.hasNext(); )
{
	qDebug()<< it.key() << it.next().value();
}

QMutableMapIterator<QString,QColor> mit(map);
if(mit.findNext("111"))
{
	mit.setValue(QColor(RGB(255,0,0)));
}

QHash

QHash以任意顺序方式存放数据,如果数据的顺序无关紧要时,使用QHash更好。
QHash的查找速度明显快于QMap
QHash占用的存储空间明显多于QMap
QHash以任意的方式存储元素
QMap以Key顺序存储元素
QHash的键类型必须提供operator==()和qHash(key)函数
QMap的键类型必须提供operator<()函数

QHash<QString, int> hash;
hash.insert("2", 2);
hash.insert("0", 0);
hash.insert("1", 1);
hash.insert("3", 3);
QList<QString> kList = hash.keys();
for(int i=0; i<kList.count(); i++)
{
   qDebug() << kList[i];
}

QList<int> vList = hash.values();
for(int i=0; i<vList.count(); i++)
{
   qDebug() << vList[i];
}
QHash<QString, int>::const_iterator i;
for(i=hash.constBegin(); i!=hash.constEnd(); ++i)
{
   qDebug() << i.key() << ":" << i.value();
}
//只读遍历器
QHash<QString, int>::const_iterator rIt1;
for (rIt1 = hash.begin(); rIt1 != hash.end(); ++rIt1)
{
        if(QString(2) == rIt1.key())
        {
            qDebug() << rIt1.value();
        }
}

//读写遍历器
QHash<QString, int>::iterator wIt1;
for (wIt1 = hash.begin(); wIt1 != hash.end(); ++wIt1)
{
   if(QString(2)== wIt1.key())
    {
        //修改内容
       *wIt1 = 2*(*wIt1);// 使用 * 运算符获取遍历器所指的元素
            qDebug() << wIt1.value();
     }
     else if(QString(3)== wIt1.key())
     {
            //修改内容
            hash.insert(QString(3),5);
            qDebug() << wIt1.value();
     }
}

发布了30 篇原创文章 · 获赞 9 · 访问量 937

猜你喜欢

转载自blog.csdn.net/x879014419/article/details/105117318