2、stack、queue、list、set、map(STL基础)

1 栈容器 Stack

1.1 符合先进后出的数据结构
1.2 对外接口
	1.2.1 入栈 push
	1.2.2 出栈 pop 
	1.2.3 栈顶 top
	1.2.4 是否为空 empty
	1.2.5 栈大小   size
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <stack>

/*
3.4.3.1 stack构造函数
stack<T> stkT;//stack采用模板类实现, stack对象的默认构造形式:
stack(const stack &stk);//拷贝构造函数
3.4.3.2 stack赋值操作
stack& operator=(const stack &stk);//重载等号操作符
3.4.3.3 stack数据存取操作
push(elem);//向栈顶添加元素
pop();//从栈顶移除第一个元素
top();//返回栈顶元素
3.4.3.4 stack大小操作
empty();//判断堆栈是否为空
size();//返回堆栈的大小
*/

void test01()
{
	stack<int>S;
	//入栈
	S.push(10);
	S.push(20);
	S.push(30);
	S.push(40);

	cout << "size  = " << S.size() << endl;

	while (!S.empty())
	{
		//访问栈顶元素
		cout << S.top() << endl;
		//出栈
		S.pop();
	}
	cout << "size  = " << S.size() << endl;

}


int main(){

	test01();

	system("pause");
	return EXIT_SUCCESS;
}	

2 队列容器 Queue

2.1 符合先进先出的数据结构
2.2 对外接口
	2.2.1 入队 push
	2.2.2 出队 pop
	2.2.3 队头元素  front
	2.2.4 队尾元素  back
	2.2.5 是否为空  empty
	2.2.6 队列大小  size
	#define _CRT_SECURE_NO_WARNINGS
	#include<iostream>
	using namespace std;
	#include <queue>
	#include <string>
	class Person
	{
	public:
		Person(string name, int age)
		{
			this->m_Name = name;
			this->m_Age = age;
		}
		string m_Name;
		int m_Age;
	};

	void test01()
	{
		queue<Person> Q; //队列容器

		Person p1("aaa", 10);
		Person p2("bbb", 20);
		Person p3("ccc", 30);
		Person p4("ddd", 40);


		//入队
		Q.push(p1);
		Q.push(p2);
		Q.push(p3);
		Q.push(p4);

		cout << "size = " << Q.size() << endl;

		while ( !Q.empty())
		{
			cout << "队头元素--- 姓名:  " << Q.front().m_Name << " 年龄: " << Q.front().m_Age << endl;
			cout << "队尾元素--- 姓名:  " << Q.back().m_Name << " 年龄: " << Q.back().m_Age << endl;

			//出队
			Q.pop();
		}

		cout << "size = " << Q.size() << endl;

	}

	int main(){

		test01();

		system("pause");
		return EXIT_SUCCESS;
	}

3 list容器

3.1 双向循环链表
3.2 对外接口
	3.2.1 构造、赋值、大小、重置大小、是否为空
	3.2.2 反转 reverse
	3.2.3 排序 sort
		3.2.3.1 //如果容器的迭代器支持随机访问,可以使用系统提供的标志算法
		3.2.3.2 //不支持随机访问的迭代器的容器,内部会提供对应的算法接口
		3.2.3.3 对于自定义数据类型,必须要指定排序规则
	3.2.4 对自定义数据类型做了高级排序
	3.2.5 如果利用remove删除自定义数据类型,需要重载 == 
#define _CRT_SECURE_NO_WARNINGS
	#include<iostream>
	using namespace std;
	#include <list>
	#include <algorithm>
	#include <string>
	void test01()
	{

		list<int> myList;
		for (int i = 0; i < 10; i++){
			myList.push_back(i);
		}

		list<int>::_Nodeptr node = myList._Myhead->_Next;

		for (int i = 0; i < myList._Mysize * 2; i++){
			cout << "Node:" << node->_Myval << endl;
			node = node->_Next;
			if (node == myList._Myhead){
				node = node->_Next;
				//node = node->_Prev
			}
		}


	}

	/*
	3.6.4.1 list构造函数
	list<T> lstT;//list采用采用模板类实现,对象的默认构造形式:
	list(beg,end);//构造函数将[beg, end)区间中的元素拷贝给本身。
	list(n,elem);//构造函数将n个elem拷贝给本身。
	list(const list &lst);//拷贝构造函数。
	3.6.4.2 list数据元素插入和删除操作
	push_back(elem);//在容器尾部加入一个元素
	pop_back();//删除容器中最后一个元素
	push_front(elem);//在容器开头插入一个元素
	pop_front();//从容器开头移除第一个元素
	insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
	insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
	insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
	clear();//移除容器的所有数据
	erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
	erase(pos);//删除pos位置的数据,返回下一个数据的位置。
	remove(elem);//删除容器中所有与elem值匹配的元素。
	*/

	//遍历链表
	void printList(const list<int> & L)
	{
		for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}

	void test02()
	{
		list<int> L1;
		L1.push_back(10);
		L1.push_back(20);
		L1.push_back(30);

		//正序遍历
		//for (list<int>::iterator it = L1.begin(); it != L1.end();it++)
		//{
		//	cout << *it << endl;
		//}
		//逆序遍历
		for (list<int>::reverse_iterator it = L1.rbegin(); it != L1.rend();it++)
		{
			cout << *it << endl;
		}

		//list迭代器是不是支持随机访问 ,答案:不支持,是一个双向迭代器
		list<int>::iterator itBegin = L1.begin();
		//itBegin = itBegin + 1;
	}

	void test03()
	{
		list<int>L;
		L.push_back(10);
		L.push_back(20);
		L.push_back(30);
		L.push_front(100);
		L.push_front(200);
		L.push_front(300);

		printList(L);

		L.pop_back(); //尾删
		L.pop_front(); //头删
		// 200 100 10 20
		printList(L);


		//插入
		L.insert(L.begin(), 10000);
		// 10000 200 100 10 20
		printList(L);

		L.erase(L.begin());
		// 200 100 10 20 
		printList(L);

		//remove 删除容器中所有与elem值匹配的元素。
		L.push_back(100);
		L.push_back(100);
		L.remove(100);
		printList(L);

	}

/*
3.6.4.3 list大小操作
size();//返回容器中元素的个数
empty();//判断容器是否为空
resize(num);//重新指定容器的长度为num,
若容器变长,则以默认值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。
resize(num, elem);//重新指定容器的长度为num,
若容器变长,则以elem值填充新位置。
如果容器变短,则末尾超出容器长度的元素被删除。

3.6.4.4 list赋值操作
assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。
list& operator=(const list &lst);//重载等号操作符
swap(lst);//将lst与本身的元素互换。
3.6.4.5 list数据的存取
front();//返回第一个元素。
back();//返回最后一个元素。
*/
void test04()
{
	list<int>L;
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);

	list<int>L2;
	L2.assign(10, 100);

	L.swap(L2);

	printList(L);

}


/*
3.6.4.6 list反转排序
reverse();//反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。
sort(); //list排序
*/
bool myCompare(int v1 ,int v2)
{
	return v1 > v2;
}

void test05()
{
	list<int>L;
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);

	//反转
	L.reverse();

	printList(L);

	//排序  
	//如果容器的迭代器支持随机访问,可以使用系统提供的标志算法
	//不支持随机访问的迭代器的容器,内部会提供对应的算法接口
	//sort(L.begin(), L.end());
	/*L.sort(myCompare);
	printList(L);*/
}


class Person
{
public:
	Person(string name, int age , int height)
	{
		this->m_Name = name;
		this->m_Age = age;
		this->m_Height = height;
	}

	bool operator==( const Person & p )
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age && this->m_Height == p.m_Height)
		{
			return true;
		}
		return false;
	}

	string m_Name;
	int m_Age;
	int m_Height;
};

bool myComparePerson( Person &p1, Person &p2)
{
	if (p1.m_Age == p2.m_Age )
	{
		return p1.m_Height < p2.m_Height;
	}

	return  p1.m_Age > p2.m_Age;
}

void test06()
{
	list<Person> L;

	Person p1("亚瑟", 40 ,  180);
	Person p2("赵云", 20 , 160);
	Person p3("妲己", 30 , 120);
	Person p4("孙悟空", 50 , 200);
	Person p5("关羽", 10 ,  170 );
	Person p6("刘备", 10  , 165);
	Person p7("张飞", 10 , 185);

	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);
	L.push_back(p6);
	L.push_back(p7);

	//按照年龄进行降序   从大到下 , 如果年龄相等,按照身高进行升序 
	//对于自定义数据类型,必须要指定排序规则
	L.sort(myComparePerson);

	for (list<Person>::iterator it = L.begin(); it != L.end();it++)
	{
		cout << "姓名: " << (*it).m_Name << " 年龄: " << it->m_Age << " 身高: "<< it->m_Height <<endl;
	}

	//删除孙悟空
	L.remove(p4);

	cout << "删除孙悟空后:" << endl;
	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名: " << (*it).m_Name << " 年龄: " << it->m_Age << " 身高: " << it->m_Height << endl;
	}

}

int main(){

	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
	test06();
	system("pause");
	return EXIT_SUCCESS;
}

4 set 容器

4.1 关联式容器 key就是value
4.2 默认排好序 从小到大
4.3 插入 insert 大小 size 是否为空 empty
4.4 查找 find 返回值 迭代器
4.5 统计 count 对于set的结果 要么是0 要么是1
4.6 lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
4.7 upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
4.8 equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
4.9 pair对组
4.9.1 创建方式
4.9.2 pair<string, int> p(“Tom”, 10);
4.9.3 pair<string, int> p2 = make_pair(“Jerry”, 18);
4.10 set.insert的返回值是个对组 pair<iterator, bool> bool代表插入是否成功
4.11 multiset可以插入重复的key值
4.12 可以指定set容器的排序规则,但是必须在插入前指定,利用仿函数的技术
4.13 对于自定义数据类型,set通常都会指定出排序规则

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <set>
#include <string>

/*
3.7.2.1 set构造函数
set<T> st;//set默认构造函数:
mulitset<T> mst; //multiset默认构造函数:
set(const set &st);//拷贝构造函数
3.7.2.2 set赋值操作
set& operator=(const set &st);//重载等号操作符
swap(st);//交换两个集合容器
3.7.2.3 set大小操作
size();//返回容器中元素的数目
empty();//判断容器是否为空

3.7.2.4 set插入和删除操作
insert(elem);//在容器中插入元素。
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem);//删除容器中值为elem的元素。
*/

void printSet(set<int>&s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	set<int>s;

	s.insert(10);
	s.insert(50);
	s.insert(30);
	s.insert(20);
	s.insert(40);

	printSet(s);


	if (s.empty())
	{
		cout << "set容器为空" << endl;
	}
	else
	{
		cout << "set容器不为空  大小为: " << s.size() << endl;
	}

	s.erase(30);

	printSet(s);
}


/*
3.7.2.5 set查找操作
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//查找键key的元素个数
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/

void test02()
{
	set<int>s;

	s.insert(10);
	s.insert(50);
	s.insert(30);
	s.insert(20);
	s.insert(40);

	set<int>::iterator pos = s.find(30);
	if (pos != s.end())
	{
		cout << "找到了元素:" << *pos << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//对于set而言,count的结果  要么是0  要么是1
	int num = s.count(40);

	cout << "key为40的个数为:" << num << endl;

	//lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
	set<int>::iterator pos2 = s.lower_bound(30);

	if (pos2 != s.end())
	{
		cout << "lower_bound的值为:" << *pos2 << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


	//upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
	pos2 = s.upper_bound(30);
	if (pos2 != s.end())
	{
		cout << "upper_bound的值为:" << *pos2 << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


	//equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。

	pair< set<int>::iterator, set<int>::iterator>  ret = s.equal_range(30);

	if (ret.first != s.end())
	{
		cout << "equal_range中的lower_bound的值为:" << *ret.first << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


	if (ret.second != s.end())
	{
		cout << "equal_range中的upper_bound的值为:" << *ret.second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

}

//pair创建
void test03()
{
	pair<string, int> p("Tom", 10);

	cout << "姓名: " << p.first <<  " 年龄: "<< p.second << endl;


	pair<string, int> p2 = make_pair("Jerry", 18);
	cout << "姓名: " << p2.first << " 年龄: " << p.second << endl;
}


void test04()
{
	set<int>s;
	pair<set<int>::iterator, bool> ret = s.insert(10);
	
	if (ret.second)
	{
		cout << "第一次插入成功" << endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}
	
	
	ret = s.insert(10);
	if (ret.second)
	{
		cout << "第二次插入成功" << endl;
	}
	else
	{
		cout << "第二次插入失败" << endl;
	}


	printSet(s);

	//允许插入重复的key值
	multiset<int> ms;
	ms.insert(10);
	ms.insert(10);
	cout << "---------" << endl;
	for (multiset<int>::iterator it = ms.begin(); it != ms.end();it++)
	{
		cout << (*it) << endl;
	}

}


class myCompareInt
{
public:
	bool operator()(int v1 , int v2)
	{
		return v1 > v2;
	}
};

void test05()
{
	//set容器的排序规则要在插入之前指定
	set<int, myCompareInt >s;

	s.insert(10);
	s.insert(50);
	s.insert(30);
	s.insert(20);
	s.insert(40);

	//printSet(s);

	for (set<int, myCompareInt>::iterator it = s.begin(); it != s.end();it++)
	{
		cout << *it << endl;
	}
}

//对于自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	
	string m_Name;
	int m_Age;
};

class myComparePerson
{
public:
	bool operator()( const Person & p1 , const Person & p2)
	{
		//按照年龄 升序  从小到大
		return p1.m_Age < p2.m_Age;
	}
};

void test06()
{
	//利用仿函数  指定出自定义数据类型的排序规则
	set<Person,myComparePerson> s;

	Person p1("aaa", 10);
	Person p2("bbb", 40);
	Person p3("ccc", 20);
	Person p4("ddd", 30);
	Person p5("eee", 50);

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	s.insert(p5);

	for (set<Person, myComparePerson>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << "姓名: " << (*it).m_Name << " 年龄: " << (*it).m_Age << endl;
	}

}


int main(){
	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
	test06();
	
	system("pause");
	return EXIT_SUCCESS;
}

5 map容器

5.1 关联式容器(底层红黑树)
5.2 默认按照key从小到大排序
5.3 插入 
5.3.1	m.insert(pair<int, int>(1, 10));
5.3.2	m.insert(make_pair(2, 20));
5.3.3	m.insert(map<int, int>::value_type(3, 30));
5.3.4	m[4] = 40;
5.4 查找 find  返回值 是迭代器
5.5 统计 count  
5.6 lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
5.7 upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
5.8 equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
5.9 利用仿函数  实现指定排序规则
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <map>
/*
3.8.2.1 map构造函数
map<T1, T2> mapTT;//map默认构造函数:
map(const map &mp);//拷贝构造函数
3.8.2.2 map赋值操作
map& operator=(const map &mp);//重载等号操作符
swap(mp);//交换两个集合容器

3.8.2.3 map大小操作
size();//返回容器中元素的数目
empty();//判断容器是否为空
3.8.2.4 map插入数据元素操作
map.insert(...); //往容器插入元素,返回pair<iterator,bool>
map<int, string> mapStu;
// 第一种 通过pair的方式插入对象
mapStu.insert(pair<int, string>(3, "小张"));
// 第二种 通过pair的方式插入对象
mapStu.inset(make_pair(-1, "校长"));
// 第三种 通过value_type的方式插入对象
mapStu.insert(map<int, string>::value_type(1, "小李"));
// 第四种 通过数组的方式插入值
mapStu[3] = "小刘";
mapStu[5] = "小王";
*/

void test01()
{
	map<int, int> m;

	//第一种插入方式
	m.insert(pair<int, int>(1,10));

	//第二种插入方式  推荐
	m.insert(make_pair(2, 20));

	//第三种插入方式
	m.insert(map<int, int>::value_type(3, 30));

	//第四种
	m[4] = 40;

	//cout << m[10] << endl; //如果利用第四种 使用未指定的key,生成一个key为为指定的值,value为0的数据插入到容器中

	for (map<int, int>::iterator it = m.begin(); it != m.end();it++)
	{
		cout << "key = " << it->first << " value = " << it->second << endl;
	}

}

/*
3.8.2.5 map删除操作
clear();//删除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg,end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(keyElem);//删除容器中key为keyElem的对组。
*/
void test02()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(map<int, int>::value_type(3, 30));
	m[4] = 40;

	m.erase(3); //删除传入key值

	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << it->first << " value = " << it->second << endl;
	}

}

/*
3.8.2.6 map查找操作
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;/若不存在,返回map.end();
count(keyElem);//返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/

void test03()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(map<int, int>::value_type(3, 30));
	m[4] = 40;

	map<int,int>::iterator ret = m.find(3);
	if (ret != m.end())
	{
		cout << "找到了 key为 " << ret->first << " value = " << ret->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//统计  count 
	int num =  m.count(4);
	cout << "key为4的对组个数为: " << num << endl;

	//lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
	map<int,int>::iterator pos = m.lower_bound(3);

	if (pos != m.end())
	{
		cout << "找到了lower_bound key: " << pos->first << " value: " << pos->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
	pos = m.upper_bound(3);
	if (pos != m.end())
	{
		cout << "找到了upper_bound key: " << pos->first << " value: " << pos->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


	//equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
	pair<map<int, int>::iterator, map<int, int>::iterator>  ret2 = m.equal_range(3);

	if ( ret2.first != m.end() )
	{
		cout << "找到了equal_range中的 lower_bound的 key =  " << ret2.first->first << " value = " << ret2.first->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	if (ret2.second != m.end())
	{
		cout << "找到了equal_range中的 upper_bound的 key =  " << ret2.second->first << " value = " << ret2.second->second << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

}


class myCompareInt
{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};
void test04()
{
	map<int, int, myCompareInt> m;

	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(map<int, int>::value_type(3, 30));
	m[4] = 40;

	for (map<int, int, myCompareInt>::iterator it = m.begin(); it != m.end();it++)
	{
		cout << "key = " << it->first << " value = " << it->second << endl;
	}

}


int main(){
	//test01();
	//test02();
	//test03();
	test04();

	system("pause");
	return EXIT_SUCCESS;
}

6 STL容器使用时机

vector:可以随机访问,场景:软件的历史操作记录的存储
deque:可随机存储,排队购票系统
list:不可以随机存储,公交车乘客的存储
set:不可以随机存储,手机游戏玩家得分
multiset:不可以随机存储,
map:对于key而言,不可以随机存储;ID很多,快速查找
multimap:不可以随机存储,

7 作业布置

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <string>
#include <map>
#include <ctime>
/*
//公司今天招聘了5个员工,5名员工进入公司之后,需要指派员工在那个部门工作
//人员信息有: 姓名 年龄 电话 工资等组成
//通过Multimap进行信息的插入 保存 显示
//分部门显示员工信息 显示全部员工信息
*/

enum{ CAIWU,RENLI,YANFA};

class Worker
{
public:
	string m_Name;
	int m_Money;
};

void createWorker(vector<Worker>&v)
{
	string nameSeed = "ABCDE";
	for (int i = 0; i < 5;i++)
	{
		Worker worker;
		worker.m_Name = "员工";
		worker.m_Name += nameSeed[i];

		worker.m_Money = rand() % 10000 + 10000; // 10000 ~ 19999

		v.push_back(worker);
	}
}


void setGroup(vector<Worker>&v, multimap<int,Worker>&m)
{
	for (vector<Worker>::iterator it = v.begin(); it != v.end();it++)
	{
		//随机产生部门编号   0  1  2
		int dId = rand() % 3;
		m.insert(make_pair(dId, *it));
	}
}

void showWorker(multimap<int,Worker>&m)
{
	cout << "财务部门员工信息如下:" << endl;

	//  0  A  0  B  1 C   2 D  2 E

	multimap<int,Worker>::iterator pos = m.find(CAIWU);

	int num = m.count(CAIWU);
	int index = 0;
	for (; pos != m.end(), index<num; pos++,index++)
	{
		cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Money << endl;
	}


	cout << "人力部门员工信息如下:" << endl;
	pos = m.find(RENLI);

	num = m.count(RENLI);
	index = 0;
	for (; pos != m.end(), index < num; pos++, index++)
	{
		cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Money << endl;
	}


	cout << "研发部门员工信息如下:" << endl;
	pos = m.find(YANFA);

	num = m.count(YANFA);
	index = 0;
	for (; pos != m.end(), index < num; pos++, index++)
	{
		cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Money << endl;
	}


}

int main(){

	//随机数种子
	srand((unsigned int)time(NULL));

	vector<Worker>v;
	//1、创建5名员工
	createWorker(v);

	//2、员工分组
	multimap<int, Worker> m;
	setGroup(v, m);

	//3、分部门显示员工
	showWorker(m);


	//测试
	//for (vector<Worker>::iterator it = v.begin(); it != v.end();it++)
	//{
	//	cout << "姓名: " << it->m_Name << " 工资: " << it->m_Money << endl;
	//}

	system("pause");
	return EXIT_SUCCESS;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值