前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >qmap的书写格式linux,QMap 键值存储「建议收藏」

qmap的书写格式linux,QMap 键值存储「建议收藏」

作者头像
全栈程序员站长
发布2022-08-23 19:01:10
1.1K0
发布2022-08-23 19:01:10
举报

大家好,又见面了,我是你们的朋友全栈君。

Qt中的QMap介绍与使用,在坛子里逛了一圈,发现在使用QMap中,出现过很多的问题,Map是一个很有用的数据结构。它以“键-值”的形式保存数据。在使用的时候,通过提供字符标示(键)即可得到想要的数据。这个“数据”即可以是一个字符串,也可以是任意对象,当然也包括自己定义的类对象。说明:map是以值传递的形式保存数据的。

1. 基本应用

下面以“键-值”都是QString的例子说明QMap的基本使用方法。更详细的说明,请查看《Qt帮助手册》或其他资源。

#include

#include

usingnamespacestd;

classMapTest

{

public:

voidshowMap()

{

if(!m_map.isEmpty())return;//判断map是否为空

m_map.insert(“111″,”aaa”);//向map里添加一对“键-值”

if(!m_map.contains(“222”))//判断map里是否已经包含某“键-值”

m_map.insert(“222″,”bbb”);

m_map[“333″] =”ccc”;//另一种添加的方式

qDebug(“map[333] , value is : “+ m_map[“333”]);//这种方式既可以用于添加,也可以用于获取,但是你必须知道它确实存在

if(m_map.contains(“111”)){

QMap::iterator it = m_map.find(“111”);//找到特定的“键-值”对

qDebug(“find 111 , value is : “+ it.data());//获取map里对应的值

}

cout<

qDebug(“size of this map is : %d”, m_map.count());//获取map包含的总数

cout<

QMap::iterator it; //遍历map

for( it = m_map.begin(); it != m_map.end(); ++it ) {

qDebug( “%s: %s”, it.key().ascii(), it.data().ascii());//用key()和data()分别获取“键”和“值”

}

m_map.clear(); //清空map

}

private:

QMap m_map; //定义一个QMap对象

};

#include

#include

using namespace std;

class MapTest

{

public:

void showMap()

{

if(!m_map.isEmpty()) return; //判断map是否为空

m_map.insert(“111”, “aaa”); //向map里添加一对“键-值”

if(!m_map.contains(“222”)) //判断map里是否已经包含某“键-值”

m_map.insert(“222”, “bbb”);

m_map[“333”] = “ccc”; //另一种添加的方式

qDebug(“map[333] , value is : ” + m_map[“333”]); //这种方式既可以用于添加,也可以用于获取,但是你必须知道它确实存在

if(m_map.contains(“111”)){

QMap::iterator it = m_map.find(“111”); //找到特定的“键-值”对

qDebug(“find 111 , value is : ” + it.data()); //获取map里对应的值

}

cout<< endl;

qDebug(“size of this map is : %d”, m_map.count()); //获取map包含的总数

cout<< endl;

QMap::iterator it; //遍历map

for ( it = m_map.begin(); it != m_map.end(); ++it ) {

qDebug( “%s: %s”, it.key().ascii(), it.data().ascii()); //用key()和data()分别获取“键”和“值”

}

m_map.clear(); //清空map

}

private:

QMap m_map; //定义一个QMap对象

};

调用类函数showMap(),显示结果:

map[333] , value is : ccc

find 111 , value is : aaa

size of thismap is : 3

111: aaa

222: bbb

333: ccc

map[333] , value is : ccc

find 111 , value is : aaa

size of this map is : 3

111: aaa

222: bbb

333: ccc

2. 对象的使用

map当中还可以保存类对象、自己定义类对象,例子如下(摘自QT帮助文档《Qt Assistant》,更详细的说明参考之):

以注释形式说明

#include

#include

#include

//自定义一个Employee类,包含fn、sn、sal属性

classEmployee

{

public:

Employee(): sn(0) {}

Employee( constQString& forename,constQString& surname,intsalary )

: fn(forename), sn(surname), sal(salary)

{ }

QString forename() const{returnfn; }

QString surname() const{returnsn; }

intsalary()const{returnsal; }

voidsetSalary(intsalary ) { sal = salary; }

private:

QString fn;

QString sn;

intsal;

};

intmain(intargc,char**argv)

{

QApplication app( argc, argv );

typedefQMap EmployeeMap;//自定义一个map类型,值为EmployeeMap对象

EmployeeMap map;

map[“JD001”] = Employee(“John”,”Doe”, 50000);//向map里插入键-值

map[“JW002”] = Employee(“Jane”,”Williams”, 80000);

map[“TJ001”] = Employee(“Tom”,”Jones”, 60000);

Employee sasha( “Sasha”,”Hind”, 50000 );

map[“SH001”] = sasha;

sasha.setSalary( 40000 ); //修改map值的内容,因为map采用值传递,所以无效

//批量打印

EmployeeMap::Iterator it;

for( it = map.begin(); it != map.end(); ++it ) {

printf( “%s: %s, %s earns %d\n”,

it.key().latin1(),

it.data().surname().latin1(),

it.data().forename().latin1(),

it.data().salary() );

}

return0;

}

#include

#include

#include

//自定义一个Employee类,包含fn、sn、sal属性

class Employee

{

public:

Employee(): sn(0) {}

Employee( const QString& forename, const QString& surname, int salary )

: fn(forename), sn(surname), sal(salary)

{ }

QString forename() const { return fn; }

QString surname() const { return sn; }

int salary() const { return sal; }

void setSalary( int salary ) { sal = salary; }

private:

QString fn;

QString sn;

int sal;

};

int main(int argc, char **argv)

{

QApplication app( argc, argv );

typedef QMap EmployeeMap; //自定义一个map类型,值为EmployeeMap对象

EmployeeMap map;

map[“JD001”] = Employee(“John”, “Doe”, 50000); //向map里插入键-值

map[“JW002”] = Employee(“Jane”, “Williams”, 80000);

map[“TJ001”] = Employee(“Tom”, “Jones”, 60000);

Employee sasha( “Sasha”, “Hind”, 50000 );

map[“SH001”] = sasha;

sasha.setSalary( 40000 ); //修改map值的内容,因为map采用值传递,所以无效

//批量打印

EmployeeMap::Iterator it;

for ( it = map.begin(); it != map.end(); ++it ) {

printf( “%s: %s, %s earns %d\n”,

it.key().latin1(),

it.data().surname().latin1(),

it.data().forename().latin1(),

it.data().salary() );

}

return 0;

}

Program output:

JD001: Doe, John earns 50000

JW002: Williams, Jane earns 80000

SH001: Hind, Sasha earns 50000

TJ001: Jones, Tom earns 60000

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/139280.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月7,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档