目录
一:什么是关联容器
通过保存在数据项中的索引项,尽可能快速检索数据项
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
}