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 #include 
 4 
 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 #include 
 5 #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     QMapint> 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     QList skeys = 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     QMapIteratorint> 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     QMapint>::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     QMapIteratorint> 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     QMapint>::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     QMapint>::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   "geometry">
  6    
  7     0
  8     0
  9     710
 10     313
 11    
 12   
 13   "windowTitle">
 14    <string>MainWindowstring>
 15   
 16   class="QWidget" name="centralwidget">
 17    class="QHBoxLayout" name="horizontalLayout">
 18     
 19      class="QVBoxLayout" name="verticalLayout">
 20       
 21        class="QTextEdit" name="textEdit"/>
 22       
 23       
 24        class="QHBoxLayout" name="horizontalLayout_4">
 25         
 26          class="QPushButton" name="pushButton_5">
 27           "text">
 28            <string>增加元素方式1string>
 29           
 30          
 31         
 32         
 33          class="QPushButton" name="pushButton_4">
 34           "text">
 35            <string>增加元素方式2string>
 36           
 37          
 38         
 39         
 40          class="QPushButton" name="pushButton_10">
 41           "text">
 42            <string>增加元素方式3string>
 43           
 44          
 45         
 46        
 47       
 48       
 49        class="QHBoxLayout" name="horizontalLayout_2">
 50         
 51          class="QPushButton" name="pushButton_3">
 52           "text">
 53            <string>是否已经包含string>
 54           
 55          
 56         
 57         
 58          class="QPushButton" name="pushButton">
 59           "text">
 60            <string>最小键对应的数据string>
 61           
 62          
 63         
 64         
 65          class="QPushButton" name="pushButton_19">
 66           "text">
 67            <string>最大键对应的数据string>
 68           
 69          
 70         
 71         
 72          class="QPushButton" name="pushButton_20">
 73           "text">
 74            <string>最小键string>
 75           
 76          
 77         
 78         
 79          class="QPushButton" name="pushButton_21">
 80           "text">
 81            <string>最大键string>
 82           
 83          
 84         
 85        
 86       
 87       
 88        class="QHBoxLayout" name="horizontalLayout_5">
 89         
 90          class="QPushButton" name="pushButton_7">
 91           "text">
 92            <string>取值1string>
 93           
 94          
 95         
 96         
 97          class="QPushButton" name="pushButton_6">
 98           "text">
 99            <string>取Keysstring>
100           
101          
102         
103         
104          class="QPushButton" name="pushButton_11">
105           "text">
106            <string>取Keys2string>
107           
108          
109         
110         
111          class="QPushButton" name="pushButton_12">
112           "text">
113            <string>取valuesstring>
114           
115          
116         
117         
118          class="QPushButton" name="pushButton_13">
119           "text">
120            <string>取values2string>
121           
122          
123         
124         
125          class="QPushButton" name="pushButton_14">
126           "text">
127            <string>QMapIteratorstring>
128           
129          
130         
131         
132          class="QPushButton" name="pushButton_18">
133           "text">
134            <string>查找findstring>
135           
136          
137         
138         
139          class="QPushButton" name="pushButton_2">
140           "text">
141            <string>uniqueKeysstring>
142           
143          
144         
145        
146       
147       
148        class="QHBoxLayout" name="horizontalLayout_6">
149         
150          class="QPushButton" name="pushButton_9">
151           "text">
152            <string>总元素sizestring>
153           
154          
155         
156         
157          class="QPushButton" name="pushButton_8">
158           "text">
159            <string>总元素countstring>
160           
161          
162         
163         
164          class="QPushButton" name="pushButton_17">
165           "text">
166            <string>判空isEmptystring>
167           
168          
169         
170        
171       
172       
173        class="QHBoxLayout" name="horizontalLayout_7">
174         
175          class="QPushButton" name="pushButton_16">
176           "text">
177            <string>Java风格迭代器string>
178           
179          
180         
181         
182          class="QPushButton" name="pushButton_15">
183           "text">
184            <string>STL风格迭代器string>
185           
186          
187         
188         
189          class="QPushButton" name="pushButton_22">
190           "text">
191            <string>foreachstring>
192           
193          
194         
195        
196       
197       
198        class="QHBoxLayout" name="horizontalLayout_8">
199         
200          class="QPushButton" name="pushButton_25">
201           "text">
202            <string>取走takestring>
203           
204          
205         
206         
207          class="QPushButton" name="pushButton_24">
208           "text">
209            <string>移除removestring>
210           
211          
212         
213         
214          class="QPushButton" name="pushButton_23">
215           "text">
216            <string>清空clearstring>
217           
218          
219         
220        
221       
222      
223     
224    
225   
226  
227  
228  
229 

搜索

复制

相关