当前位置:网站首页>[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

边栏推荐
- Link garbled escape character
- Global and Chinese markets for active transdermal drug delivery devices 2022-2028: Research Report on technology, participants, trends, market size and share
- F1C100S自制开发板调试过程
- How to set password complexity and timeout exit function in Oracle
- Wechat applet +php to realize authorized login operation
- 问题记录:fel_lib.c:26:10: fatal error: libusb.h: 没有那个文件或目录
- &nbsp; Difference from spaces
- Use of custom MVC
- MySQL extracts strings from table fields
- Mysql提取表字段中的字符串
猜你喜欢

Reasons for MySQL master-slave database synchronization failure

Auto.js学习笔记16:按项目保存到手机上,不用每次都保存单个js文件,方便调试和打包

2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...

发现mariadb数据库时间晚了12个小时

自定义JvxeTable的按钮及备注下$set的用法

An article to get you started VIM

How to modify and add fields when MySQL table data is large

通用分页(2)

golang bilibili直播彈幕姬

What is the concept of string in PHP
随机推荐
What is the metauniverse: where are we, where are we going
Prompt learning a blood case caused by a space
Add a custom button to jvxetable
Huawei interview question: divide candy
Global and Chinese market for nasal drug delivery devices 2022-2028: Research Report on technology, participants, trends, market size and share
JS 字母和数字的相互转换
产品思维 | 无人机快递的未来值得期待吗?
[QT] QMap使用详解
OP diode limit swing
Uniapp address translation latitude and longitude
链接乱码转义符
Golang BiliBili live broadcast bullet screen
Azure developer news flash list of events of developers in June
[untitled]
Global and Chinese market for sensor screwdrivers 2022-2028: Research Report on technology, participants, trends, market size and share
数据库的下一个变革方向——云原生数据库
Data set preparation and arrangement
Principle of device driver
图的邻接矩阵存储 C语言实现BFS
Simple custom MVC