c++-基础知识-STL案例

c+±基础知识-STL案例

1.vector deque应用

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

/*
需求:有5名选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分
*/
//选手
class Person
{
public:
	//构造函数
	Person(string name,int score)
	{
		this->m_Name = name;
		this->m_Score = score;
	}

	string m_Name;//姓名
	int m_Score;//成绩
};

void createPerson(vector<Person> &v)
{
	string name[] = {"A","B","C","D","E"};
	int score = 0;
	for (int i = 0; i < 5;i++)
	{
		Person p(name[i],score);
		v.push_back(p);
	}
}

void setScore(vector<Person> &v)
{
	for(vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		//将评委的分数放入到deque容器中
		deque<int>d;
		for (int i = 0;i < 10; i++)
		{
			int score = rand()%41 + 60; //60 ~ 100
			d.push_back(score);
		}
		//排序
		sort(d.begin(),d.end());

		//去除最高分,去除最低分
		d.pop_back();
		d.pop_front();

		//求平均
		int sum = 0;
		int avg ;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			sum += *dit;
		}
		avg = sum/d.size();
		//赋值
		(*it).m_Score = avg;
	}
}

void showInfo(vector<Person> v)
{
	for(vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout<<"姓名: "<<(*it).m_Name<<" 成绩: "<<(*it).m_Score<<endl;
	}
}
void test()
{
	//随机数字种子
	srand((unsigned int)time(NULL));

	//1.创建
	vector<Person> v;
	createPerson(v);

	//测试
	//for(vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	//{
	//	cout<<"姓名: "<<(*it).m_Name<<" 成绩: "<<(*it).m_Score<<endl;
	//}
	
	//2.打分
	setScore(v);

	//3.展示
	showInfo(v);
	
}

int main()
{
	test();
	return 0;
}

2.vector map应用

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

//define关键字
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
/*
步骤:
1.创建10名员工,放到vector中
2.遍历vector数组,取出每个员工,随机分组
3.分组后,将员工部门编号作为key,具体员工作为value,放到multimap容器中
4.分部门显示员工信息
*/
//选手
class Worker
{
public:
	//构造函数
	Worker(string name,int salary)
	{
		this->m_Name = name;
		this->m_Salary = salary;
	}

	string m_Name;//姓名
	int m_Salary;//成绩
};

//创建worker
void createWorker(vector<Worker> &v)
{
	string nameSeed[] = {"A","B","C","D","E","F","G","H","I","J"};
	
	for (int i = 0; i < 10;i++)
	{
		int Salary = rand()%10000 + 10000;
		Worker p(nameSeed[i],Salary);
		v.push_back(p);
	}
}

//分组
void setGroup(vector<Worker> &v, multimap<int,Worker> &mWorker)
{
	for(vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
	{
		//产生随机部门编号
		int depthID	= rand() % 3;

		//将员工编号插入到分组中
		//key部门编号,value具体分工
		mWorker.insert(make_pair(depthID,*it));
	}
}

//void showInfo(vector<Worker> v)
//{
//	for(vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
//	{
//		cout<<"姓名: "<<(*it).m_Name<<" 薪水: "<<(*it).m_Salary<<endl;
//	}
//}

void showWorkerByGroup(multimap<int,Worker>&m)
{
	cout<<"策划部门: "<<endl;
	//查找
	multimap<int,Worker>::iterator pos = m.find(CEHUA);
	int count = m.count(CEHUA);//统计具体人数
	int index = 0;
	for (;pos != m.end()&&index <count;pos++,index++)
	{
		cout<<"姓名: "<<pos->second.m_Name<<" 薪水: "<<pos->second.m_Salary<<endl;
	}

	cout<<"美术部门: "<<endl;
	//查找
	multimap<int,Worker>::iterator pos1 = m.find(MEISHU);
	int count1 = m.count(MEISHU);//统计具体人数
	int index1 = 0;
	for (;pos1 != m.end()&&index <count1;pos1++,index1++)
	{
		cout<<"姓名: "<<pos1->second.m_Name<<" 薪水: "<<pos1->second.m_Salary<<endl;
	}

	cout<<"研发部门: "<<endl;
	//查找
	multimap<int,Worker>::iterator pos2 = m.find(YANFA);
	int count2 = m.count(YANFA);//统计具体人数
	int index2 = 0;
	for (;pos2 != m.end()&&index <count2;pos2++,index2++)
	{
		cout<<"姓名: "<<pos2->second.m_Name<<" 薪水: "<<pos2->second.m_Salary<<endl;
	}	

}

void test()
{
	//随机数字种子
	srand((unsigned int)time(NULL));

	//1.创建员工
	vector<Worker> vWorker;
	createWorker(vWorker);

	//测试
	//for(vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
	//{
	//	cout<<"姓名: "<<(*it).m_Name<<" 薪水: "<<(*it).m_Salary<<endl;
	//}
	
	//2.分组
    multimap<int,Worker>mWorker;
	setGroup(vWorker,mWorker);
	//3.展示
	//showInfo(v);
	showWorkerByGroup(mWorker);
}

int main()
{
	test();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值