C++ STL case (string container, vector container, deque container, etc. to achieve the judges' scoring)


Preface

Recently, I learned various containers of C++, such as string container, vector container, and deque container. There happened to be a case in the learning video (scoring by the judges). These kinds of containers are used well to complete the task of scoring and averaging by the judges. I think it's pretty good, just record it.


1. Code implementation tasks

Task requirements: Use C++ to write a program. In a competition, there are N contestants (take 5 as an example). Player ABCDEF, ten judges score each contestant, remove the highest score and remove the lowest score among the judges. Take the average score and give each player a score.

2. Implementation steps

1. Write a Person class to store the name and average score

The code is as follows (example):

class Person
{
    
    
public:
	Person(string name,int score)
	{
    
    
		this->m_Name=name;
		this->m_Score=score;
	}
	
	string m_Name; //姓名
	int m_Score;   //平均分

};

2. Create an empty vector container for players

The code is as follows (example):

void createPerson(vector<Person>&v)
{
    
    
	string nameSeed="ABCDE";
	for(int i=0;i<5;i++)
	{
    
    
	string name="选手";
	name+=nameSeed[i];//  +=依此遍历进行nameSeed的输出

	int score=0;

	Person p(name,score);

	//将创建的person对象,放入到容器中
	v.push_back(p); //push_back进行尾插
	}
}

3. Scoring-the deque container is suitable for sorting operations, so use the deque container to perform scoring operations

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);
	}

	/*cout<<"选手:"<<it->m_Name<<"打分:"<<endl;
	for(deque<int>::iterator dit=d.begin();dit!=d.end();dit++)
	{
	cout<<*dit<<" ";
	}
	cout<<endl;*/

	//排序
	sort(d.begin(),d.end());//需要包含标准算法头文件

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

	//取平均分
	int sum=0;
	for(deque<int>::iterator dit =d.begin();dit!=d.end();dit++)
	{
    
    
	   sum+=*dit;//累加每个评委的分数
	}


	int avg =sum /d.size();

	//将平均分赋值到选手身上
	it->m_Score=avg;
  }
}

4. Write a showScore display function

void showScore(vector<Person>&v)
{
    
    
	for(vector<Person>::iterator it =v.begin();it!=v.end();it++)
	{
    
    
		cout<<"姓名:"<<it->m_Name<<"平均分:"<<it->m_Score<<endl;
	}

}

5. The header files included in the program

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

6. Calling the main function of the program

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

	//1、创建5名选手
	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、给5名选手打分
	setScore(v);


	//3、显示最后得分
	showScore(v);

system("pause");
return 0;

}

to sum up

The above is the content of the program. This judges scoring program uses the vector container to provide information such as the names and scores of the players. The deque container is used to more conveniently sort the judges’ scores, better calculate the average score, and use the algorithm to sort the sorts. Sort, use ctime to realize random number seed.
The things learned during the recent period are well reflected in this program. I can make comprehensive use of these and realize the realization of this specific case. I think I have learned a lot. The important thing is to summarize the scattered knowledge. , It’s a wonderful feeling o(  ̄▽ ̄ )ブ

Guess you like

Origin blog.csdn.net/qq_45252077/article/details/108911807