日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT学习笔记(九):遍历容器-迭代器(iterators)

發(fā)布時(shí)間:2024/7/23 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT学习笔记(九):遍历容器-迭代器(iterators) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

QT學(xué)習(xí)筆記(九):遍歷容器-迭代器(iterators)

      • 遍歷容器 :
        • 1、Jave風(fēng)格:
        • 2、STL風(fēng)格:
        • 3、foreach 關(guān)鍵字:

遍歷容器 :

遍歷一個(gè)容器可以使用迭代器(iterators)完成,迭代器提供一個(gè)統(tǒng)一的方法來訪問容器中的項(xiàng)目。
迭代器:Jave風(fēng)格、STL(標(biāo)準(zhǔn)模板庫(Standard Template Library))風(fēng)格;當(dāng)容器中的數(shù)據(jù)被修改后或由于調(diào)用了non-const成員函數(shù)導(dǎo)致其脫離了隱式共享,那么這兩種迭代器都會(huì)失效。

兩者比較:
Jave較STL使用方便,但性能上較弱與后者。
Jave風(fēng)格迭代器:只讀訪問、讀寫訪問;

1、Jave風(fēng)格:


QList 迭代器示例:

#include <QCoreApplication> #include <QList> #include <QListIterator> #include <QMutableListIterator> #include <QDebug> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QList<QString> list;list << "A" << "B" << "C" << "D";// 創(chuàng)建列表的只讀迭代器,將list作為參數(shù)QListIterator<QString> i(list); qDebug() << "the forward is :";while (i.hasNext()) // 正向遍歷列表,結(jié)果為A,B,C,DqDebug() << i.next();qDebug() << "the backward is :";while (i.hasPrevious()) // 反向遍歷列表,結(jié)果為D,C,B,AqDebug() << i.previous();// 創(chuàng)建列表的讀寫迭代器,將list作為參數(shù)QMutableListIterator<QString> j(list);j.toBack(); // 返回列表尾部while (j.hasPrevious()) {QString str = j.previous();if(str == "B") j.remove(); // 刪除項(xiàng)目“B”}j.insert("Q"); // 在列表最前面添加項(xiàng)目“Q”j.toBack();if(j.hasPrevious()){j.previous() = "N"; // 直接賦值}j.previous(); // 返回前一個(gè)項(xiàng)目,并回移一格j.setValue("M"); // 使用setValue()進(jìn)行賦值j.toFront();qDebug()<< "the forward is :";while (j.hasNext()) // 正向遍歷列表,結(jié)果為Q,A,M,NqDebug() << j.next();return a.exec(); }

運(yùn)行結(jié)果:


QMap 迭代器示例:

#include <QCoreApplication> #include <QMapIterator> #include <QMutableMapIterator> #include <QDebug>int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QMap<QString, QString> map;map.insert("Paris", "France");map.insert("Guatemala City", "Guatemala");map.insert("Mexico City", "Mexico");map.insert("Moscow", "Russia");// 創(chuàng)建Map的只讀迭代器,將map作為參數(shù)QMapIterator<QString,QString> i(map);while(i.hasNext()) // 正向遍歷 map{i.next();qDebug() << i.key() << " : " << i.value();}if(i.findPrevious("Mexico")) {qDebug() << "find 'Mexico'"; // 向前查找鍵的值}// 創(chuàng)建Map的讀/寫迭代器,將map作為參數(shù)QMutableMapIterator<QString, QString> j(map);while (j.hasNext()) {if (j.next().key().endsWith("City")) // endsWith()是QString類的函數(shù)j.remove(); // 刪除含有“City”結(jié)尾的鍵的項(xiàng)目}while(j.hasPrevious()) {j.previous(); // 現(xiàn)在的鍵值對(duì)為 (paris,France),(Moscow,Russia)qDebug() << j.key() << " : " << j.value();}return a.exec(); }

運(yùn)行結(jié)果:

2、STL風(fēng)格:

STL 風(fēng)格迭代器兼容Qt和STL的通用算法,在速度上進(jìn)行了優(yōu)化:

QList 和QMap 綜合STL 風(fēng)格迭代器示例:

#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()函數(shù)轉(zhuǎn)換為小寫qDebug() << *i; // 結(jié)果為a,b,c,d}qDebug() << "the backward is :";while (i != list.begin()){--i;qDebug() << *i; // 結(jié)果為d,c,b,a}// 使用只讀迭代器QList<QString>::const_iterator j; qDebug() << "the forward is :";for (j = list.constBegin(); j != list.constEnd(); ++j)qDebug() << *j; // 結(jié)果為a,b,c,d// QMap STL 風(fēng)格迭代器使用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(); // 結(jié)果為(one,1),(three,3),(two,2)return a.exec(); }

運(yùn)行結(jié)果:

3、foreach 關(guān)鍵字:

如果你只是想順序的變量容器中的所以元素,可以使用Qt的foreach關(guān)鍵字。這個(gè)關(guān)鍵字是Qt特定的,是使用預(yù)處理器實(shí)現(xiàn)的。
它的語法是:

foreach(variable, container) statement;

例如,下面的代碼說明了怎么使用foreach來迭代QLinkedList:

QLinkedList<QString> list;... QString str; foreach (str, list)qDebug() << str;

QList 和QMap 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中獲取每一項(xiàng){ qDebug() << str; // 結(jié)果為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中獲取每一個(gè)鍵 qDebug() << str << " : " << map.value(str); // 輸出鍵和對(duì)應(yīng)的值,結(jié)果為(first,1),(second,2),(third,3)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;}// 結(jié)果為(first,3),(first,2),(first,1),(second,2)return a.exec(); }

運(yùn)行結(jié)果:

總結(jié)

以上是生活随笔為你收集整理的QT学习笔记(九):遍历容器-迭代器(iterators)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。