Triumph Day13——C++ STL容器案例、随机数、<algorithm>头文件下常用的函数

目录

1、STL容器案例-评分打分

2、关于随机数

3、头文件下常用的函数:

4、STL容器案例-员工分组

5.1 代码呈现

5.2 均匀分组

5、关于如何实现不重复随机数


*

1、STL容器案例-评分打分

案例描述

有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。

实现步骤

  1. 创建五名选手,放到vector中

  2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中

  3. sort算法对deque容器中分数排序,去除最高和最低分

  4. deque容器遍历一遍,累加总分

  5. 获取平均分

头文件及声明:

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

注意:

使用STL容器要加上头文件声明

选手类:

将选手的属性私有化,写set、get方法来进行访问

class Person
{
public:
    Person(string name,int score) 
    {
        this->name = name;
        this->score = score;
    }
    void setName(string name)
    {
        this->name = name;
    }
    string getName()
    {
        return this->name;
    }
    void setScore(int score)
    {
        this->score = score;
    }
    int getScore()
    {
        return this->score;
    }
    
private:
    string name;
    int score;
};

创建人对象将其添加入vector容器中:

vector容器中存储的是对象,对象可以通过引用的方式对属性进行访问

void creatPerson(vector<Person>& v)
{
    string nameString = "ABCDE";
    for (int i = 0; i < 5; i++)
    {
        string name = "选手";
        name += nameString[i];
        int score = 0;
        Person p(name, score);
        v.push_back(p);
    }
}


 

评委打分函数:

小细节:

        由于题目中得分规则是去掉最高和最低分之后的平均分,所以这里可以使用deque双端数组进行存储评委的打分情况,存储后通过排序将头尾两个元素删除,得到剩下的打分即为有效打分

        得到平均分后一定不能忘记将其赋值给选手的属性中

//评委打分
void setScore(vector<Person>& v)
{
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		deque<int>d;
		for (int i = 0; i < 10; i++)
		{
			int score = rand()%41+60;
			d.push_back(score);
		}
		//打分进行排序
		sort(d.begin(), d.end());
		//排序后正好最高最低分处于头尾,pop进行删除
		d.pop_back();
		d.pop_front();
		int sum = 0;
		//遍历评委的有效分数进行相加
		for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
		{
			sum += *it;
		}
		//得到平均数
		int avg = sum / d.size();
		//将得到的平均分给选手
		it->setScore(avg);
	}
}

展示最终分数的函数:

通过遍历vector容器来得到选手的最终分数

void showScore(vector<Person>& v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout  << it->getName() << "" << "得分" << it->getScore() << endl;
    }
}

主函数:

int main() {
    //随机数种子
    srand(time(NULL));
    
    //创建五位选手
    vector<Person>v;//存放选手的容器
    creatPerson(v);
    
    //打分函数
    setScore(v);

    //展示分函数
    showScore(v);

    system("pause");

    return 0;
}


2、关于随机数

rand函数。

int rand(void);

rand()函数不需要引入任何头文件,可直接使用,返回0~RAND_MAX(32767)的整数,不需要参数,它是根据种子生成的,根据不同的种子产生不同的随机序列。

这个程序无论何时何地,运行的结果都是固定的。系统默认的种子是1,随机序列又是根据种子产生的,所以说每次使用的随机序列都是固定的,随机数不随机。要想使它随机,就必须使它的种子随机。

srand函数。

void srand(unsigned int seed);

srand()函数就是用来设置rand()函数的种子的。
同样srand()函数也不需要头文件,可以直接使用,根据不同的参数产生不同的种子。

同样,这个程序如同上个程序,何时何地运行结果都一样,因为srand()的参数一样,生成的种子一样,rand()函数返回的值也一样。那么就只有在srand()的参数上做文章。

time函数

time(NULL);

函数会返回1970年1月1日至今所经历的时间(以秒为单位),需要引入头文件<time.h>

用时间值做种子,就可以产生随机数了,因为时间总是在变的嘛。

将time(NULL)作为srand()的参数,更新种子,再用rand()函数产生随机数。

综上所述:

        通过time函数作为参数传入srand函数,进而设置rand函数的种子,再使用rand函数根据srand函数设置的种子产生随机序列,就可以到我们想要的随机数

        srand(time(NULL));

        int num = rand()%41+60;//60~100

srand((unsigned int)time(NULL));

for (int i = 0; i < 10; i++)
{
    int score = rand() % 41 + 60;  // 60 ~ 100

    cout << score << endl;
}

注意:

        rand()%41代表的意思是生成0~41之间的数字,左闭右开;

        加上60后,生成的随机数就是60~100;

        需要注意的是,srand(time(NULL));只需要执行一次即可。如果每次使用rand()前都执行一遍,就会产生相同的随机数,这是因为time()是以秒为单位的,在同一秒内返回的值都是相同的,种子是相同的,产生的随机序列自然也相同。每次都从同一个序列的开头取数,可不就一样了嘛。


3、<algorithm>头文件下常用的函数:

1)max(),min(),abs()
max(x,y)和min(x,y)分别返回x和y中的最大值和最小值,且参数必须是两个。
abs(x) 返回x的绝对值。x必须为整数,浮点型的绝对值要用math头文件下的fabs

2)swap()
swap(x,y)用来交换x和y的值

3)reverse()
reverse(it,it2) 可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内的元素进行反转。

4)fill()

fill() 可以把数组或容器中的某一段区间赋为某个相同的值。和memset不同,这里的赋值可以使数组类型对应范围中的任意值。

5)sort()

默认为递增排序
* 若要递减排序,需要增加比较函数


4、STL容器案例-员工分组

案例描述

  • 公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作

  • 员工信息有: 姓名 工资组成;部门分为:策划、美术、研发

  • 随机给10名员工分配部门和工资

  • 通过multimap进行信息的插入 key(部门编号) value(员工)

  • 分部门显示员工信息

实现步骤

  1. 创建10名员工,放到vector中

  2. 遍历vector容器,取出每个员工,进行随机分组

  3. 分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中

  4. 分部门显示员工信息

5.1 代码呈现

#include <iostream>
#include <vector>
#include <map>
#include <string>

#define CEHUA  0
#define MEISHU 1
#define YANFA  2
using namespace std;

class Person
{
public:
	string name;
	int pay;
	int post;

public:

	Person(string name, int pay, int post)
	{
		this->name = name;
		this->pay = pay;
		this->post = post;
	}
};
void creatPerson(vector<Person>&v)
{
	string personName = "ABCDEFGHIJ";
	for (int i=0; i<10; i++)
	{
		string name="员工";
		name += personName[i];
		int pay = rand()%5000+10000;
		int post = 0;
		Person p(name,pay,post);
		v.push_back(p);
	}
}
void setGroup(vector<Person>& v, multimap<int, Person>& m)
{
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		it->post = rand() % 3;
		m.insert(pair<int,Person>(it->post, *it));
	}
}
void show(vector<Person>& v, multimap<int, Person>& m)
{
	int num_0 = 0;
	int num_1 = 0;
	int num_2 = 0;
	
	cout << "策划部门:" << endl;
	multimap<int, Person>::iterator pos = m.find(CEHUA);
	int count = m.count(CEHUA); // 统计具体人数
	int index = 0;
	for (;pos!=m.end() && index<count; pos++,index++)
	{
		cout << "姓名:  " << pos->second.name << "  " << "工资:  " << pos->second.pay << endl;
	}
	
	cout << "----------------------" << endl;
	cout << "美术部门: " << endl;
	pos = m.find(MEISHU);
	count = m.count(MEISHU); // 统计具体人数
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名: " << pos->second.name << "  " << "工资: " << pos->second.pay << endl;
	}
	
	cout << "----------------------" << endl;
	cout << "研发部门: " << endl;
	pos = m.find(YANFA);
	count = m.count(YANFA);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名: " << pos->second.name << "  " << "工资: " << pos->second.pay << endl;
	}
}
int main()
{
	srand(time(NULL));

	vector<Person> v;
	creatPerson(v);

	multimap<int, Person> m;
	setGroup(v, m);

	show(v, m);
	system("pause");

	return 0;
}

5.2 均匀分组

如果需要均匀分组,则需要运用到不重复随机数,12个人就是0~11 12个随机数然后除余3个部门,得到三组数据,将其赋值到岗位属性中即可

void setGroup(vector<Person>& v, multimap<int, Person>& m)
{
	int a[12];
	for (int i = 0; i <= 11; ++i)
		a[i] = i;
	for (int i = 11; i >= 1; --i)
	{
		swap(a[i], a[rand() % i]);
	}
	int i = 0;
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{

		it->post = a[i++] % 3;
		m.insert(pair<int, Person>(it->post, *it));
	}
}

5、关于如何实现不重复随机数

一种简单的方式:

现在一个数组中赋值然后,通过遍历数组每一个下标,与一个随机下标对应的元素值进行交换

int a[12];
	for (int i = 0; i <= 11; ++i)
		a[i] = i;
	for (int i = 11; i >= 1; --i)
	{
		swap(a[i], a[rand() % i]);
	}

当然还有很多方法,这个方法只是对于这个程序来说较为好用

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖虎不秃头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值