STL——マップ/マルチマップ

マップコンテナはセットコンテナに似ており、どちらも赤黒木に基づいています

マップコンテナは、キーと値のペアを格納し、キーに基づいてそれらを並べ替えます。重複するキーは許可されません。

グループとして挿入

# include<iostream>
# include<algorithm>
# include<map>
# include<vector>
# include<string>
# include<time.h>
# include<stdlib.h>
# define SALE_DEPARTMENT 1
# define DEVELOP_DEPARTMENT 2
# define FINANCIAL_DEPARTMENT 3

using namespace std;

void test1()
{//初始化、大小操作、赋值 
	map<int,int> mp;
	mp.insert(make_pair(10,10));
	mp.insert(make_pair(20,20));
	mp.insert(make_pair(30,30));
	
	if(!mp.empty())
		cout << "map中元素的数量:" << mp.size() << endl;
	for(map<int,int>::iterator it = mp.begin();it != mp.end();++it)
		cout << "key:" << (*it).first << " " << "value:" << (*it).second << endl;
	
	cout << "---------" << endl;
	cout << "map2:" << endl;
	map<int,int> mp2 = mp;
 	for(map<int,int>::iterator it = mp2.begin();it != mp2.end();++it)
		cout << "key:" << (*it).first << " " << "value:" << (*it).second << endl;
 } 

void test2()
{//四种插入方式 
	map<int,int> mp;
	
	#if 0 
	用pair插入和[]插入的区别:
	pair返回pair对组,键值重复,会插入失败
	[]插入:如果该键不存在,则创建键插入;如果该键已存在,则覆盖对应的值 
	#endif
	pair<map<int,int>::iterator,bool> ret = mp.insert(pair<int,int>(10,10));
	cout << "插入pair(10,10)";
	if(ret.second)
		cout << "成功!";
	else
		cout << "失败!";
	cout << endl; 
	ret = mp.insert(make_pair(10,20));
	cout << "插入pair(10,20)";
	if(ret.second)
		cout << "成功";
	else
		cout << "失败";
	cout << endl;
	ret = mp.insert(make_pair(20,20));
	mp.insert(map<int,int>::value_type(30,30));
	mp[40] = 40;
	
	for(map<int,int>::iterator it = mp.begin();it != mp.end();++it)
		cout << "key:" << (*it).first << " " << "value:" << (*it).second << endl;
 } 

class MyKey{
	public:
		MyKey(int Index,int Id):index(Index),id(Id){}
	public:
		int index;
		int id;
};

class compare{
	public:
		bool operator()(MyKey a,MyKey b){
			return a.index > b.index;
		}
};

void test3()
{//map存储对象 
	map<MyKey,int,compare> mp;
	
	mp.insert(make_pair(MyKey(7,2),3));
	mp.insert(make_pair(MyKey(1,5),6));
	mp.insert(make_pair(MyKey(4,8),9));
	
	for(map<MyKey,int,compare>::iterator it = mp.begin();it != mp.end();++it)
		cout << "index:" << it->first.index << " " << "id:" << it->first.id
		<< " " << "second:" << it->second << endl;
}

void test4()
{//map查找 
	map<int,int> mp;
	
	mp.insert(make_pair(1,4));
	mp.insert(make_pair(2,5));
	mp.insert(make_pair(3,6));
	
	/*
	下界迭代器小于等于elem,优先等于 
	上界迭代器大于 elem
	*/
	pair<map<int,int>::iterator,map<int,int>::iterator> ret = mp.equal_range(2);
	if(ret.first->second)
		cout << "下界:" << ret.first->first << endl;
	else
		cout << "没有找到!" << endl;
	if(ret.second->second)
		cout << "上界:" << ret.second->first << endl;
	else
		cout << "没有找到!" << endl;
} 

/*
案例:
存储五个员工信息,姓名,手机号,年龄,工资 
使用multimap进行员工名分组
分部门显示员工信息 
*/
class Worker{
	public:
		string Name;
		string tel;
		int age;
		int salary;
};

//vector存储员工信息
void CreateWorker(vector<Worker>& v){
	string name = "ABCDE";
	Worker worker;
	srand(time(0));
	for(int i = 0;i < 5;++i)
	{
		worker.Name = "员工";
		worker.Name += name[i];
		worker.tel = "010-88888";
		worker.age = rand()%10 + 20;//10~30之间
		worker.salary = rand()%10000 + 10000;//10000~20000之间 
		
		v.push_back(worker);
	}
}

//为员工分组
void WorkerByGroup(multimap<int,Worker>& WorkerGroup,vector<Worker> v){
	int departID;
	srand(time(0));
	for(vector<Worker>::iterator it = v.begin();it != v.end();++it)
	{
		departID = rand()%3+1;
		WorkerGroup.insert(make_pair(departID,*it));
	}
}

void ShowWorkerByGroup(multimap<int,Worker>& WorkerGroup,int departID){
	
	//输出需要两个参数,第一个是对应部门的起始指针,第二个是该部门的人数 
	multimap<int,Worker>::iterator it = WorkerGroup.find(departID);
	int amount = WorkerGroup.count(departID);
	int num = 0;
	for(;it != WorkerGroup.end() && num < amount;++it,++num){
		
		cout << "姓名:" << it->second.Name << " " << "年龄:" << it->second.age << " "
		 << "工资:" << it->second.salary << " " << "联系方式:" << it->second.tel << endl;
	 } 
}

//按部门显示员工信息
void PrintWorkerByGroup(multimap<int,Worker>& WorkerGroup){
	
	cout << "销售部门:" << endl;
	ShowWorkerByGroup(WorkerGroup,SALE_DEPARTMENT);
	cout << "发展部门:" << endl;
	ShowWorkerByGroup(WorkerGroup,DEVELOP_DEPARTMENT);
	cout << "经济部门:" << endl; 
	ShowWorkerByGroup(WorkerGroup,FINANCIAL_DEPARTMENT);
}

void test(){
	//案例
	vector<Worker> v;
	multimap<int,Worker> WorkerGroup;
	//vector存储员工信息
	//创建员工 
	CreateWorker(v);
	//为员工分组
	WorkerByGroup(WorkerGroup,v);
	//按部门显示员工信息
	PrintWorkerByGroup(WorkerGroup); 
}

int main()
{
	//test1();//初始化、大小操作、赋值 
	//test2();//四种插入方式 
	//test3();//map存储对象
	test4();//map查找 
	//test();//案例 
	
 return 0;
}

演算結果:

test1:

test2:

test3:

test4: 

ケース実行結果:

おすすめ

転載: blog.csdn.net/qq_40479037/article/details/87256554