STL代码练习

#include<iostream>
using namespace std;
#include <vector>
#include <string>
#include <map>
#include <ctime>

//宏定义
#define cehua  1
#define meishu 2
#define yanfa  3

class Worker
{
public:

	//Worker(string name,int  salary)
	//{
	//	this->m_Name = name;
	//	this->m_Salary = salary;
	//}

public:
	string m_Name;
	int m_Salary;

};

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

		worker.m_Salary = 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++ )
	{
		int departmentid = rand()%3 + 1; // 部门 1 2 3;

		m.insert(make_pair(departmentid,*it)); //插入随机部门和员工

	}
}

//void showWorkerByFroup( multimap<int,Worker> & m)
//{
//	for( multimap<int,Worker>:: iterator it = m.begin(); it != m.end(); it++ )
//	{
//		cout << "职位 " <<it->first << "  " 
//			<< "姓名 " << it->second.m_Name << " " 
//			<< "工资 " << it->second.m_Salary << endl;
//	}
//
//}

void showWorkerByFroup( multimap<int,Worker> & m)
{
	//按部门输出

	cout << "策划部门:  " << endl;
	//cout << m.find(1) << endl;

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

	int num = m.count(cehua);
	int index = 0;

	for( ; pos != m.end() && index < num; pos++,index++ )
	{
		cout << pos->second.m_Name << "  " << pos->second.m_Salary << endl;
	}

	cout << "--------------------------" << endl;

	cout << "美术部门:  " << endl;

	pos = m.find(meishu);

	num = m.count(meishu);
	index = 0;

	for( ; pos != m.end() && index < num; pos++,index++ )
	{
		cout << pos->second.m_Name << "  " << pos->second.m_Salary << endl;
	}

	cout << "--------------------------" << 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_Salary << endl;
	}

	//cout << "美术部门: " << endl;

	//multimap<int,Worker>::iterator pos1 = m.find(meishu);

	//int num1 = m.count(meishu);

	//for(multimap<int,Worker>::iterator pos1 = m.find(meishu); pos1 != m.end() && index < num1; pos1++,index++  )
	//{
	//	cout << pos1->second.m_Name << "  " << pos1->second.m_Salary << endl;
	//}

	//cout << "研发部门: " << endl;

	//multimap<int,Worker>::iterator pos2 = m.find(yanfa);

	//int num2 = m.count(yanfa);

	//for( multimap<int,Worker>::iterator pos2 = m.find(yanfa); pos2 != m.end() && index < num2; pos2++,index++  )
	//{
	//	cout << pos2->second.m_Name << "  " << pos2->second.m_Salary << endl;
	//}

}

int main()
{
	//随机
	srand((unsigned)time(NULL));


	vector<Worker> vWorker;
	createWorker(vWorker);


	//分组操作
	multimap<int,Worker> mWorker;
	setGroup(vWorker,mWorker);


	//测试
	//for( vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++ )
	//{
	//	cout <<  it->m_Name << ""<< it->m_Salary <<  endl;
	//}

	//测试
	//for( multimap<int,Worker>:: iterator it = mWorker.begin(); it != mWorker.end(); it++ )
	//{
	//	cout << it->first << "  " << it->second.m_Name << " " << it->second.m_Salary << endl;
	//}
	showWorkerByFroup(mWorker);

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值