STL案例2-成员分组
发布日期:2022-03-04 12:48:33 浏览次数:29 分类:技术文章

本文共 1961 字,大约阅读时间需要 6 分钟。

黑马236

#include
#include
#include
#include
#include
using namespace std;
#define chehua 0
#define yanfa 2
#define meishu 1
class person
{
public:
string m_name;
int m_salary;
};
void creatPerson(vector &v)
{
string nameSeed = “ABCDEFGHIJ”;
for (int i = 0; i < 10; i++)
{
/string work_name = “员工”;/
person p;
p.m_name= “员工”;
p.m_name+= nameSeed[i];
p.m_salary = rand() % 4001 + 6000;
v.push_back§;
}
}
void setGroup(vector& v, multimap<int, person>& m)
{
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
//产生随机部门编号
int deptId = rand() % 3;
//将员工插入到分组中
//key部门编号,value具体员工
m.insert(make_pair(deptId, *it));

}

}

void showPerosn(multimap<int, person>& m)
{
cout << “策划部门” << endl;
multimap<int, person>::iterator pos=m.find(chehua);
int count = m.count(chehua);
int index = 0;
for (; pos != m.end() && index < count; pos++)
{
cout << “name” << pos->second.m_name << " salary:" << pos->second.m_salary << endl;
index++;
}
cout << “美术部门” << endl;
multimap<int, person>::iterator pos1 = m.find(meishu);
int count1 = m.count(meishu);
int index1 = 0;
for (; pos1 != m.end() && index1 < count1; pos1++)
{
cout << “name” << pos1->second.m_name << " salary:" << pos1->second.m_salary << endl;
index1++;
}
cout << “研发部门” << endl;
multimap<int, person>::iterator pos2 = m.find(yanfa);
int count2 = m.count(yanfa);
int index2 = 0;
for (; pos2 != m.end() && index2 < count2; pos2++)
{
cout << “name” << pos2->second.m_name << " salary:" << pos2->second.m_salary << endl;
index2++;
}
}
int main()
{
//创建员工
srand((unsigned int)time(NULL));
vector v;
creatPerson(v);
/测试/
//for (vector::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << “name:” << (*it).m_name << " salary:" << (*it).m_salary << endl;
//}

//员工分组multimap
m;setGroup(v, m);//测试for (multimap
::iterator it = m.begin(); it != m.end(); it++)

{

cout << “key=”<first << “name:” << (*it).second.m_name << " salary:" << (*it).second.m_salary << endl;
}

//分组显示员工showPerosn(m);

}

转载地址:https://blog.csdn.net/qq_43752920/article/details/120532657 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:查找相邻元素算法
下一篇:STL函数对象基本使用

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月11日 00时41分57秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章