当前位置:网站首页>[qt] qmap usage details
[qt] qmap usage details
2022-06-30 03:13:00 【my_ angle2016】
One . Catalog
1. Instantiation QMap object
2. insert data
3. Remove data
4. Traversal data
5. Find the corresponding key value by the key
6. Find key by key value
7. Modify key values
8. Find out if it contains a key
9. Get all keys and key values
10. A key corresponds to multiple values
11. Customize QMap class
1. Instantiation QMap object
/* establish QMap example , The first parameter is zero QString Type key , The second parameter is int Type value */
QMap<QString, int> map;
2. insert data
/* insert data Two ways */
map["math"] = 100;
map.insert("English", 99);
Printout : QMap((“English”, 99)(“math”, 100))
3. Remove data
/* Remove data */
map.remove("math");
Printout :QMap((“English”, 99))
4. Traversal data
/* Traversal data ( Just insert a few )*/
map.insert("Math", 100);
map.insert("Chinese", 98);
map.insert("physical", 97);
map.insert("chemical", 96);
map.insert("biological", 95);
/* Iterators are used to traverse the data ,QT Provides two ways of iterating */
/* The first is Java Iterations of types */
QMapIterator<QString, int> iterator(map);
while (iterator.hasNext()) {
iterator.next();
qDebug() << iterator.key() << ":" << iterator.value();
}
/* The second is STL Iterations of types */
QMap<QString, int>::const_iterator iterator_1 = map.constBegin();
while (iterator_1 != map.constEnd()) {
qDebug() << iterator_1.key() << ":" << iterator_1.value();
++iterator_1;
}
Printout : Both methods output the same
“Chinese” : 98
“English” : 99
“Math” : 100
“biological” : 95
“chemical” : 96
“physical” : 97
5. Find the corresponding key value by the key
map.value("Math");
Printout :100
6. Find key by key value
map.key(100);
Printout :“Math”
7. Modify key values
/* Usually a key corresponds to only one value , If called again insert() Method , The previous value will be overwritten */
map.insert("Math", 120);
qDebug() << map.value("Math");
Printout :120
8. Find out if it contains a key
bool isok = map.contains("Math");
qDebug() << isok;
Printout :true
9. Get all keys and key values
QList<QString> allKeys = map.keys();
qDebug() << allKeys;
QList<int> allValues = map.values();
qDebug() << allValues;
Printout :
(“Chinese”, “English”, “Math”, “biological”, “chemical”, “physical”)
(98, 99, 120, 95, 96, 97)
10. A key corresponds to multiple values
/* Use QMultiMap Class to instantiate a QMap object */
QMultiMap<QString, QString> multiMap;
multiMap.insert("People", "Name");
multiMap.insert("People", "Gender");
multiMap.insert("People", "Age");
multiMap.insert("People", "Height");
multiMap.insert("People", "Weight");
qDebug() << multiMap;
/* It can be seen from the printed results that multiMap Still one QMap object */
Printout :
5
“Weight”
(“Weight”, “Height”, “Age”, “Gender”, “Name”)
“Name”
From the above output, we can see , Use it directly value() Method is the last inserted item ; And by values() Method to get all the key values ; If you want to get a certain key value, you can use .at() Method .
11. Customize QMap class
QMap Only keys and key values , As a container , It can only produce one-to-one correspondence between the two data , But at present, I have three data that need to be related , At first I did this
QMap<QString, int> mapOfId;
QMap<QString, QDateTime>mapOfTime;
Use two Qmap You can meet the requirements , Later, I found that there was still a little trouble , Simply use QList Customized a container that can store three values
Famous for its name CMAP
- newly build ->Library–>C++ Library–> Custom library name
- cmap.h Function declaration
- cmap.cpp Function definition
- Click on the run , Generate static link library
cmap.h File implementation
#ifndef CMAP_H
#define CMAP_H
#include "CMAP_global.h"
#include <QStringList>
#include <QDebug>
#include <QList>
class CMAP_EXPORT CMAP
{
public:
CMAP();
void insert(int key, QString value1, int value2); /* Insert a row of data */
void insert(QList<int> keys, QStringList value1s, QList<int> value2s); /* Insert multiple rows of data */
QList<int> keys() const; /* Get all keys */
QStringList value1s() const; /* Get all values 1 */
QList<int> value2s() const; /* Get all values 2 */
QString value1(int key) const; /* From key value to corresponding value 1 */
int value2(int key) const; /* From key value to corresponding value 2 */
int key(QString value1) const; /* By value 1 Get key value */
bool contains(int key) const; /* Determine whether the key is included */
bool contains(QString value1) const; /* Determine if the value... Is included 1 */
bool remove(int key); /* Delete a row of data with the key */
bool remove(QString value1); /* Pass value 1 Delete a row of data */
void clear(); /* eliminate map */
int size() const; /* return map length */
void print() const; /* Print all < key , value 1, value 2> */
private:
QList<int> key_list;
QStringList value1_list;
QList<int> value2_list;
};
#endif // CMAP_H
cmap.cpp File implementation
#include "cmap.h"
CMAP::CMAP()
{
clear();
}
void CMAP::insert(int key, QString value1, int value2)
{
key_list << key;
value1_list << value1;
value2_list << value2;
}
void CMAP::insert(QList<int> keys, QStringList value1s, QList<int> value2s)
{
Q_ASSERT(keys.size() == value1s.size());
Q_ASSERT(keys.size() == value2s.size());
key_list << keys;
value1_list << value1s;
value2_list << value2s;
}
QList<int> CMAP::keys() const
{
return key_list;
}
QStringList CMAP::value1s() const
{
return value1_list;
}
QList<int> CMAP::value2s() const
{
return value2_list;
}
bool CMAP::contains(int key) const
{
if(key_list.contains(key)) return true;
else return false;
}
bool CMAP::contains(QString value1) const
{
if(value1_list.contains(value1)) return true;
else return false;
}
bool CMAP::remove(int key)
{
if(!this->contains(key)) return false;
int i = key_list.indexOf(key);
key_list.removeAt(i);
value1_list.removeAt(i);
value2_list.removeAt(i);
return true;
}
bool CMAP::remove(QString value1)
{
if(!this->contains(value1)) return false;
int i = value1_list.indexOf(value1);
key_list.removeAt(i);
value1_list.removeAt(i);
value2_list.removeAt(i);
return true;
}
void CMAP::clear()
{
key_list.clear();
value1_list.clear();
value2_list.clear();
}
int CMAP::size() const
{
return key_list.size();
}
void CMAP::print() const
{
for(int i = 0; i < size(); i++) {
qDebug() << QString("key:%1 value1:%2 value2:%3").
arg(key_list.at(i)).
arg(value1_list.at(i)).
arg(value2_list.at(i));
}
}
QString CMAP::value1(int key) const
{
if(!this->contains(key)) return "";
int i = key_list.indexOf(key);
return value1_list.at(i);
}
int CMAP::value2(int key) const
{
if(!this->contains(key)) return -1;
int i = key_list.indexOf(key);
return value2_list.at(i);
}
int CMAP::key(QString value1) const
{
if(!this->contains(value1)) return -1;
int i = value1_list.indexOf(value1);
return key_list.at(i);
}
CMAP_global.h Just keep the file unchanged
The generated file can be used directly

边栏推荐
- Distributed file system fastdfs
- Regular full match: the password consists of more than 8 digits, upper and lower case letters, and special characters
- JS cross reference
- Global and Chinese market for sensor screwdrivers 2022-2028: Research Report on technology, participants, trends, market size and share
- Simulate activity startup mode in compose
- 正则全匹配:密码由8位以上数字,大小写字母,特殊字符组成
- 2022 tool fitter (Advanced) and tool fitter (Advanced) certificate examination
- 2022 new test questions for safety management personnel of metal and nonmetal mines (small open pit quarries) and certificate examination for safety management personnel of metal and nonmetal mines (s
- Some technology sharing
- Distributed file storage system fastdfs hands on how to do it
猜你喜欢

uniapp 地址转换经纬度

MySQL extracts strings from table fields

Principle, advantages and disadvantages of three operating modes of dc/dc converter under light load

Distributed file system fastdfs

How to use vant to realize data paging and drop-down loading

Intel hex, Motorola S-Record format detailed analysis

What are outer chain and inner chain?

What is the concept of string in PHP

Use compose to realize the effect of selecting movie seats by panning tickets

How to use redis to realize the like function
随机推荐
Servlet interview questions
发现mariadb数据库时间晚了12个小时
Redis高并发分布式锁(学习总结)
GTK interface programming (II): key components
How do I enable assembly binding logging- How can I enable Assembly binding logging?
【直播笔记0629】 并发编程二:锁
golang bilibili直播彈幕姬
Distributed file system fastdfs
【微信小程序】条件渲染 列表渲染 原来这样用?
Huawei interview question: divide candy
编译一个无导入表的DLL
PHP two-dimensional array randomly fetches a random or fixed number of one-dimensional arrays
Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)
Servlet面试题
Golang BiliBili live broadcast bullet screen
X书6.97版本shield-unidbg调用方式
hudi记录
Functions in C language
OP diode limit swing
Data set preparation and arrangement