C++编程 关联容器Map的简单使用

目录

一:什么是关联容器

二:关联容器 Map

三:Map的构造函数

四:Map的使用示例

五:Set 关联容器


一:什么是关联容器

通过保存在数据项中的索引项,尽可能快速检索数据项

STL标准库中只包含有序关联容器set、multiset、map、multimap

set, multiset:数据项就是索引项; multiset允许出现重复的索引项

map, multimap:数据项是由索引项和其他某种类型的数据组成的一对数据; multimap允许出现重复的索引项

二:关联容器 Map

增加和删除节点对迭代器的影响很小;对于迭代器来说,可以修改实值,而不能修改key

自动建立Key - value的对应;key 和 value可以是任意你需要的类型

根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,

1,000,000个记录,最多查找20次

三:Map的构造函数

使用map得包含map类所在的头文件

#include <map>

map对象是模板类,需要关键字和存储对象两个模板参数:
map<int, string> personnel;
//用int作为索引,存储string对象

四:Map的使用示例

1. 添加元素:

staffMap.insert(make_pair(1001,Staff(1001,"admin","123",ADMIN)));//添加元素

CStaff.h: 

#ifndef CSTAFF_H
#define CSTAFF_H
#define ADMIN 1
#define MANAGER 2
#define WAITER 3
#include<string>
#include<iostream>
using namespace std;

class Staff
{
public:
	Staff();
	Staff(int id,string name,string pwd,int prole);
	~Staff();
	int getId();
	string getName();
	string getPwd();
	int getRole();
private:
	int ID;
	string name;
	string pwd;
	int role;
};

#endif

CStaff.cpp: 

#include"CStaff.h"
#include<iostream>
using namespace std;

Staff::Staff()
{
}

Staff::Staff(int id,string name,string pwd,int prole)
{
	this->ID = id;
	this->name = name;
	this->pwd = pwd;
	this->role = prole;
}

int Staff::getId()
{
	return this->ID;
}

string Staff::getName()
{
	return this->name;
}

string Staff::getPwd()
{
	return this->pwd;
}

int Staff::getRole()
{
	return this->role;
}

Staff::~Staff()
{
}

main.cpp: 

#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include"CStaff.h"

void demo_map();

int main()
{
	demo_map();
	return 0;
}

void demo_map()
{
  //map key - value  用户map
	map<int, Staff> staffMap;

  //添加元素
	staffMap.insert(make_pair(1001,Staff(1001,"admin","123",ADMIN)));
	staffMap.insert(make_pair(1002,Staff(1002,"lily","123",ADMIN)));

	cout<<staffMap.size()<<endl;//2
}

2. 索引添加方法   map中key是唯一的

    //添加方法2
	staffMap[1002] = Staff(1003,"lei","123",ADMIN);

map中key是唯一的:

#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include"CStaff.h"

void demo_map();

int main()
{
	demo_map();
	return 0;
}

void demo_map()
{
//  map key - value   用户map  key唯一
	map<int ,Staff> staffMap;
//添加方法1
	staffMap.insert(make_pair(1001,Staff(1001,"admin","123",ADMIN)));
	staffMap.insert(make_pair(1002,Staff(1002,"lily","123",ADMIN)));
//添加方法2
	staffMap[1002] = Staff(1003,"lei","123",ADMIN);

	cout<<staffMap.size()<<endl;//2
}

3. it->first对应key值,迭代器访问

#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include"CStaff.h"

void demo_map();

int main()
{
	demo_map();
	return 0;
}

void demo_map()
{
//  map key - value 用户map
	map<int ,Staff> staffMap;
//添加方法1
	staffMap.insert(make_pair(1001,Staff(1001,"admin","123",ADMIN)));
	staffMap.insert(make_pair(1002,Staff(1002,"lily","123",ADMIN)));
//添加方法2
	staffMap[1003] = Staff(1003,"lei","123",ADMIN);
	cout<<staffMap.size()<<endl;

	//访问
	map<int, Staff>::iterator it;
	for(it = staffMap.begin();it!=staffMap.end();it++)
	{
		cout<<it->first<<endl;//key值
//3
//1001
//1002
//1003
	}
}

4. first--key     second--value

cout<<it->second.getId()<<endl;//获取value
#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include"CStaff.h"

void demo_map();

int main()
{
	demo_map();
	return 0;
}

void demo_map()
{
	map<int ,Staff> staffMap;
//添加方法1
	staffMap.insert(make_pair(1001,Staff(1001,"admin","123",ADMIN)));
	staffMap.insert(make_pair(1002,Staff(1002,"lily","123",ADMIN)));
//添加方法2
	staffMap[1003] = Staff(1003,"lei","123",ADMIN);
	cout<<staffMap.size()<<endl;

	//访问
	map<int, Staff>::iterator it;
	for(it = staffMap.begin();it!=staffMap.end();it++)
	{
		cout<<it->first<<endl;          //key值
		cout<<it->second.getId()<<endl; //value值
//3
//1001
//1001
//1002
//1002
//1003
//1003
	}
}

5. 若key值相同,取后面添加的为最新信息

#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include"CStaff.h"

void demo_map();

int main()
{
	demo_map();
	return 0;
}

void demo_map()
{
	map<int ,Staff> staffMap;
//添加方法1
	staffMap.insert(make_pair(1001,Staff(1001,"admin","123",ADMIN)));
	staffMap.insert(make_pair(1002,Staff(1002,"lily","123",ADMIN)));
//添加方法2
	staffMap[1003] = Staff(1003,"lei","123",ADMIN);
	staffMap[1003] = Staff(1004,"mei","123",ADMIN);
	cout<<staffMap.size()<<endl;

	//访问
	map<int, Staff>::iterator it;
	for(it = staffMap.begin();it!=staffMap.end();it++)
	{
		cout<<it->first<<endl;            //key
		cout<<it->second.getId()<<endl;   //数据
//3
//1001
//1001
//1002
//1002
//1003
//1004
	}
}

6. 查询find

	//查询
	it = staffMap.find(1002);
#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include"CStaff.h"

void demo_map();

int main()
{
	demo_map();
	return 0;
}

void demo_map()
{
	map<int ,Staff> staffMap;
//添加方法1
	staffMap.insert(make_pair(1001,Staff(1001,"admin","123",ADMIN)));
	staffMap.insert(make_pair(1002,Staff(1002,"lily","123",ADMIN)));
//添加方法2
	staffMap[1003] = Staff(1003,"lei","123",ADMIN);
	staffMap[1004] = Staff(1004,"mei","123",ADMIN);
//	cout<<staffMap.size()<<endl;

	//访问
	map<int, Staff>::iterator it;
	//查询
	it = staffMap.find(1002);
	cout<<it->first<<"--"<<it->second.getName()<<endl;
	cout<<"-----------"<<endl;
	for(it = staffMap.begin();it!=staffMap.end();it++)
	{
		cout<<it->first<<endl;
		cout<<it->second.getId()<<endl;
//1002--lily
//-----------
//1001
//1001
//1002
//1002
//1003
//1003
//1004
//1004
	}
}

7. erase删除

	//删除
	staffMap.erase(it);
#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include"CStaff.h"

void demo_map();

int main()
{
	demo_map();
	return 0;
}

void demo_map()
{
	//map key-value 用户map
	map<int ,Staff> staffMap;
//添加方法1
	staffMap.insert(make_pair(1001,Staff(1001,"admin","123",ADMIN)));
	staffMap.insert(make_pair(1002,Staff(1002,"lily","123",ADMIN)));
//添加方法2
	staffMap[1003] = Staff(1003,"lei","123",ADMIN);
	staffMap[1004] = Staff(1004,"mei","123",ADMIN);
//	cout<<staffMap.size()<<endl;

	//访问
	map<int, Staff>::iterator it;
	//查询
	it = staffMap.find(1002);
	cout<<it->first<<"--"<<it->second.getName()<<endl;
	//删除
	staffMap.erase(it);
	cout<<"-----------"<<endl;
    cout<<staffMap.size()<<endl;
	for(it = staffMap.begin();it!=staffMap.end();it++)
	{
		cout<<it->first<<endl;
		cout<<it->second.getId()<<endl;
//1002--lily
//-----------
//3
//1001
//1001
//1003
//1003
//1004
//1004
	}
}

8. clear清空

#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include"CStaff.h"

void demo_map();

int main()
{
	demo_map();
	return 0;
}

void demo_map()
{
	map<int ,Staff> staffMap;
//添加方法1
	staffMap.insert(make_pair(1001,Staff(1001,"admin","123",ADMIN)));
	staffMap.insert(make_pair(1002,Staff(1002,"lily","123",ADMIN)));
//添加方法2
	staffMap[1003] = Staff(1003,"lei","123",ADMIN);
	staffMap[1004] = Staff(1004,"mei","123",ADMIN);
	cout<<staffMap.size()<<endl;

	//访问
	map<int, Staff>::iterator it;
	//查询
	it = staffMap.find(1002);
	cout<<it->first<<"--"<<it->second.getName()<<endl;
	//删除
	staffMap.erase(it);
	cout<<"-----------"<<endl;
    cout<<staffMap.size()<<endl;
	for(it = staffMap.begin();it!=staffMap.end();it++)
	{
		//       key               数据
		cout<<it->first<<"\t"<<it->second.getId()<<endl;
	
	}
	staffMap.clear();
	cout<<staffMap.size()<<endl;
//4
//1002--lily
//-----------
//3
//1001    1001
//1003    1003
//1004    1004
//0
}

五:Set 关联容器

Set可以被视为只有关键字而没有相关的元素值的map

头文件:#include <set>

Set示例如下

#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<set>
#include"CStaff.h"

void demo_set();

int main()
{
	demo_set();
	return 0;
}

void demo_set()
{
	set<int> intSet;
	intSet.insert(1);
	intSet.insert(2);
	intSet.insert(3);
	cout<<"count::"<<intSet.size()<<endl;
//count::3
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenruhan_QAQ_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值