Qt-Qt之QMap的使用分析
相关资料:
https://blog.csdn.net/markely/article/details/8055961 原文
https://blog.csdn.net/asd1147170607/article/details/105616292 原文
https://download.csdn.net/download/zhujianqiangqq/85798434 CSDN代码包下载
代码实现:
.pro
1 QT += core gui 2 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 5 CONFIG += c++11 6 7 # The following define makes your compiler emit warnings if you use 8 # any Qt feature that has been marked deprecated (the exact warnings 9 # depend on your compiler). Please consult the documentation of the 10 # deprecated API in order to know how to port your code away from it. 11 DEFINES += QT_DEPRECATED_WARNINGS 12 13 # You can also make your code fail to compile if it uses deprecated APIs. 14 # In order to do so, uncomment the following line. 15 # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 18 SOURCES += \ 19 main.cpp \ 20 mainwindow.cpp 21 22 HEADERS += \ 23 mainwindow.h 24 25 FORMS += \ 26 mainwindow.ui 27 28 # Default rules for deployment. 29 qnx: target.path = /tmp/$${TARGET}/bin 30 else: unix:!android: target.path = /opt/$${TARGET}/bin 31 !isEmpty(target.path): INSTALLS += target
main.cpp
1 #include "mainwindow.h" 2 3 #include4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include5 #include 6 7 QT_BEGIN_NAMESPACE 8 namespace Ui { class MainWindow; } 9 QT_END_NAMESPACE 10 11 class MainWindow : public QMainWindow 12 { 13 Q_OBJECT 14 15 public: 16 MainWindow(QWidget *parent = nullptr); 17 ~MainWindow(); 18 19 private slots: 20 void on_pushButton_5_clicked(); 21 22 void on_pushButton_4_clicked(); 23 24 void on_pushButton_10_clicked(); 25 26 void on_pushButton_3_clicked(); 27 28 void on_pushButton_7_clicked(); 29 30 void on_pushButton_6_clicked(); 31 32 void on_pushButton_11_clicked(); 33 34 void on_pushButton_12_clicked(); 35 36 void on_pushButton_13_clicked(); 37 38 void on_pushButton_14_clicked(); 39 40 void on_pushButton_9_clicked(); 41 42 void on_pushButton_8_clicked(); 43 44 void on_pushButton_17_clicked(); 45 46 void on_pushButton_18_clicked(); 47 48 void on_pushButton_2_clicked(); 49 50 void on_pushButton_clicked(); 51 52 void on_pushButton_19_clicked(); 53 54 void on_pushButton_20_clicked(); 55 56 void on_pushButton_21_clicked(); 57 58 void on_pushButton_16_clicked(); 59 60 void on_pushButton_15_clicked(); 61 62 void on_pushButton_22_clicked(); 63 64 void on_pushButton_25_clicked(); 65 66 void on_pushButton_24_clicked(); 67 68 void on_pushButton_23_clicked(); 69 70 private: 71 Ui::MainWindow *ui; 72 QMap int> m_pMap; 73 }; 74 #endif // MAINWINDOW_H
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 10 setWindowTitle(QStringLiteral("Qt之QMap的使用分析")); 11 } 12 13 MainWindow::~MainWindow() 14 { 15 delete ui; 16 } 17 18 void MainWindow::on_pushButton_5_clicked() 19 { 20 m_pMap.insert("key 2", 2); 21 m_pMap.insert("key 0", 0); 22 m_pMap.insert("key 1", 1); 23 } 24 25 void MainWindow::on_pushButton_4_clicked() 26 { 27 m_pMap["333"] = 333; 28 } 29 30 void MainWindow::on_pushButton_10_clicked() 31 { 32 m_pMap.insertMulti("three",4); 33 m_pMap.insertMulti("three",5); 34 m_pMap.insertMulti("three",6); 35 } 36 37 void MainWindow::on_pushButton_3_clicked() 38 { 39 if(!m_pMap.contains("333")) 40 ui->textEdit->append("contains:true"); 41 else 42 ui->textEdit->append("contains:false"); 43 } 44 45 void MainWindow::on_pushButton_7_clicked() 46 { 47 ui->textEdit->append("map[333]:" + QString::number(m_pMap["333"])); 48 } 49 50 void MainWindow::on_pushButton_6_clicked() 51 { 52 QListskeys = m_pMap.keys(); 53 for(int i = 0; i< skeys.count(); i++) 54 { 55 ui->textEdit->append("skeys:" + skeys[i]); 56 } 57 } 58 59 void MainWindow::on_pushButton_11_clicked() 60 { 61 QList skeys2 = m_pMap.keys(4); 62 for(int i=0; i < skeys2.count(); i++) 63 { 64 ui->textEdit->append("skeys2:" + skeys2[i]); 65 } 66 } 67 68 void MainWindow::on_pushButton_12_clicked() 69 { 70 QList<int> svalues = m_pMap.values(); 71 for(int i = 0; i < svalues.count(); i++) 72 { 73 ui->textEdit->append("svalues:" + QString::number(svalues[i])); 74 } 75 } 76 77 void MainWindow::on_pushButton_13_clicked() 78 { 79 QList<int> svalues2 = m_pMap.values("three"); 80 for(int i=0; i ) 81 { 82 ui->textEdit->append("svalues2:" + QString::number(svalues2[i])); 83 } 84 } 85 86 void MainWindow::on_pushButton_14_clicked() 87 { 88 QMapIterator int> it(m_pMap); 89 while(it.hasNext()) 90 { 91 it.next(); 92 ui->textEdit->append("QMapIterator:" + it.key() + " : " + QString::number(it.value())); 93 } 94 } 95 96 void MainWindow::on_pushButton_9_clicked() 97 { 98 ui->textEdit->append("size:" + QString::number(m_pMap.size())); 99 } 100 101 void MainWindow::on_pushButton_8_clicked() 102 { 103 ui->textEdit->append("count:" + QString::number(m_pMap.count())); 104 } 105 106 void MainWindow::on_pushButton_17_clicked() 107 { 108 ui->textEdit->append("isEmpty:" + QString::number(m_pMap.isEmpty())); 109 } 110 111 void MainWindow::on_pushButton_18_clicked() 112 { 113 QMap int>::iterator it = m_pMap.find("333"); 114 ui->textEdit->append("find 333:" + QString::number(it.value())); 115 } 116 117 void MainWindow::on_pushButton_2_clicked() 118 { 119 QList suniqueKeys = m_pMap.uniqueKeys(); 120 for(int i = 0; i < suniqueKeys.count(); i++) 121 { 122 ui->textEdit->append("uniqueKeys:" + suniqueKeys[i]); 123 } 124 } 125 126 void MainWindow::on_pushButton_clicked() 127 { 128 int nfirst = m_pMap.first(); 129 ui->textEdit->append("first:" + QString::number(nfirst)); 130 } 131 132 void MainWindow::on_pushButton_19_clicked() 133 { 134 int nlast = m_pMap.last(); 135 ui->textEdit->append("last:" + QString::number(nlast)); 136 } 137 138 void MainWindow::on_pushButton_20_clicked() 139 { 140 QString firstKey = m_pMap.firstKey(); 141 ui->textEdit->append("firstKey:" + firstKey); 142 } 143 144 void MainWindow::on_pushButton_21_clicked() 145 { 146 QString lastKey = m_pMap.lastKey(); 147 ui->textEdit->append("lastKey:" + lastKey); 148 } 149 // 用Java风格迭代器进行遍历 150 void MainWindow::on_pushButton_16_clicked() 151 { 152 QMapIterator int> i(m_pMap); 153 i.toFront(); 154 while(i.hasNext()) 155 { 156 ui->textEdit->append("hasNext:" + i.key() + " : " + QString::number(i.next().value())); 157 } 158 i.toBack(); 159 while(i.hasPrevious()) 160 { 161 ui->textEdit->append("hasPrevious:" + i.key() + " : " + QString::number(i.previous().value())); 162 } 163 164 // bool jjj = i.findNext(2); 165 // if (jjj) 166 // qDebug() << "jjj: " << i.key() << i.value(); 167 168 // bool kkk = i.findPrevious(2); 169 // if (kkk ) 170 // qDebug() << "kkk : " << i.key() << i.value(); 171 } 172 // 用STL风格迭代器进行遍历 173 void MainWindow::on_pushButton_15_clicked() 174 { 175 QMap int>::const_iterator constI; 176 for(constI = m_pMap.cbegin(); constI != m_pMap.cend(); constI++) 177 ui->textEdit->append("constI:" + constI.key() + " : " + QString::number(constI.value())); 178 179 QMap int>::iterator itI; 180 for(itI = m_pMap.begin();itI != m_pMap.end();itI++) 181 ui->textEdit->append("itI:" + itI.key() + " : " + QString::number(itI.value())); 182 } 183 // 用foreach进行遍历 184 void MainWindow::on_pushButton_22_clicked() 185 { 186 foreach(const QString &str, m_pMap.keys()) 187 ui->textEdit->append("foreach: " + str + " : " + QString::number(m_pMap.value(str))); 188 // 对于多值映射,可以使用两重foreach语句 189 foreach(const QString &str, m_pMap.uniqueKeys()) 190 { 191 foreach(int i, m_pMap.values(str)) 192 ui->textEdit->append("uniqueKeys: " + str + " : " + QString::number(i)); 193 } 194 } 195 196 void MainWindow::on_pushButton_25_clicked() 197 { 198 m_pMap.take("333"); 199 ui->textEdit->append("take: " + QString::number(m_pMap.count())); 200 } 201 202 void MainWindow::on_pushButton_24_clicked() 203 { 204 m_pMap.remove("key 2"); 205 ui->textEdit->append("remove: " + QString::number(m_pMap.count())); 206 } 207 208 void MainWindow::on_pushButton_23_clicked() 209 { 210 m_pMap.clear(); 211 ui->textEdit->append("clear: " + QString::number(m_pMap.count())); 212 }
mainwindow.ui
1 <?xml version="1.0" encoding="UTF-8"?> 2"4.0"> 3 <class>MainWindowclass> 4 class="QMainWindow" name="MainWindow"> 5 227"geometry"> 6 137 120 80 9710 10313 11"windowTitle"> 14 <string>MainWindowstring> 15 16class="QWidget" name="centralwidget"> 17 226class="QHBoxLayout" name="horizontalLayout"> 18 225- 19
224class="QVBoxLayout" name="verticalLayout"> 20 223- 21
23class="QTextEdit" name="textEdit"/> 22 - 24
48class="QHBoxLayout" name="horizontalLayout_4"> 25 47- 26
32class="QPushButton" name="pushButton_5"> 27 31"text"> 28 <string>增加元素方式1string> 29 30- 33
39class="QPushButton" name="pushButton_4"> 34 38"text"> 35 <string>增加元素方式2string> 36 37- 40
46class="QPushButton" name="pushButton_10"> 41 45"text"> 42 <string>增加元素方式3string> 43 44- 49
87class="QHBoxLayout" name="horizontalLayout_2"> 50 86- 51
57class="QPushButton" name="pushButton_3"> 52 56"text"> 53 <string>是否已经包含string> 54 55- 58
64class="QPushButton" name="pushButton"> 59 63"text"> 60 <string>最小键对应的数据string> 61 62- 65
71class="QPushButton" name="pushButton_19"> 66 70"text"> 67 <string>最大键对应的数据string> 68 69- 72
78class="QPushButton" name="pushButton_20"> 73 77"text"> 74 <string>最小键string> 75 76- 79
85class="QPushButton" name="pushButton_21"> 80 84"text"> 81 <string>最大键string> 82 83- 88
147class="QHBoxLayout" name="horizontalLayout_5"> 89 146- 90
96class="QPushButton" name="pushButton_7"> 91 95"text"> 92 <string>取值1string> 93 94- 97
103class="QPushButton" name="pushButton_6"> 98 102"text"> 99 <string>取Keysstring> 100 101- 104
110class="QPushButton" name="pushButton_11"> 105 109"text"> 106 <string>取Keys2string> 107 108- 111
117class="QPushButton" name="pushButton_12"> 112 116"text"> 113 <string>取valuesstring> 114 115- 118
124class="QPushButton" name="pushButton_13"> 119 123"text"> 120 <string>取values2string> 121 122- 125
131class="QPushButton" name="pushButton_14"> 126 130"text"> 127 <string>QMapIteratorstring> 128 129- 132
138class="QPushButton" name="pushButton_18"> 133 137"text"> 134 <string>查找findstring> 135 136- 139
145class="QPushButton" name="pushButton_2"> 140 144"text"> 141 <string>uniqueKeysstring> 142 143- 148
172class="QHBoxLayout" name="horizontalLayout_6"> 149 171- 150
156class="QPushButton" name="pushButton_9"> 151 155"text"> 152 <string>总元素sizestring> 153 154- 157
163class="QPushButton" name="pushButton_8"> 158 162"text"> 159 <string>总元素countstring> 160 161- 164
170class="QPushButton" name="pushButton_17"> 165 169"text"> 166 <string>判空isEmptystring> 167 168- 173
197class="QHBoxLayout" name="horizontalLayout_7"> 174 196- 175
181class="QPushButton" name="pushButton_16"> 176 180"text"> 177 <string>Java风格迭代器string> 178 179- 182
188class="QPushButton" name="pushButton_15"> 183 187"text"> 184 <string>STL风格迭代器string> 185 186- 189
195class="QPushButton" name="pushButton_22"> 190 194"text"> 191 <string>foreachstring> 192 193- 198
222class="QHBoxLayout" name="horizontalLayout_8"> 199 221- 200
206class="QPushButton" name="pushButton_25"> 201 205"text"> 202 <string>取走takestring> 203 204- 207
213class="QPushButton" name="pushButton_24"> 208 212"text"> 209 <string>移除removestring> 210 211- 214
220class="QPushButton" name="pushButton_23"> 215 219"text"> 216 <string>清空clearstring> 217 218228 229
搜索
复制