当前位置:网站首页>关于容器的小案例
关于容器的小案例
2022-07-30 13:50:00 【老鱼37】
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<algorithm>
//创建一个类
class Person
{
public:
Person(string name, int score)
{
m_name = name;
m_score = score;
}
string m_name;
int m_score;
};
//创建选手
void CreatPerson(vector<Person>& v)
{
string name = "ABCDE";
for (int i = 0; i < 5; i++)
{
string name2 = "选手";
name2 += name[i];
int score = 0;
Person p(name2, score);
//然后将创建的对象放入容器中
v.push_back(p);
}
}
//打分 10名评委打分
void MarkScore(vector<Person>& v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
int score = 0;
//创建一个duque容器存放分数
deque<int>d;
for (int i = 0; i < 10; i++)
{
score = rand() % 40 + 60;//控制分数在60-99
d.push_back(score);
}
//169-175为显示代码,可要可不要
//打印deque容器中的分数
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
{
cout << *dit << " ";
}
cout << endl;
//对deque中的分数进行排序 默认从小到大,如果想修改的话直接重载
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 = sum + *dit;
}
int agv = sum / d.size();//原来10个,去掉头尾还剩八个
//将平均数赋值给vector中的score
it->m_score = agv;//此刻还在循环中 所以是it
}
}
//展示
void ShowPerson(vector<Person>& v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << (*it).m_name << " " <<"平均分为:" <<(*it).m_score << endl;
//也可以使用指针的方式 it->m_name it->m_score
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
//创建一个容器 放入选手
vector<Person> v;
//创建选手
CreatPerson(v);
//打分
MarkScore(v);
//展示选手
ShowPerson(v);
return 0;
}
如有错误,多多指教!
边栏推荐
- Flask框架——Sijax
- Interface automation framework, lm-easytest beta version released, use it quickly~
- pytorch学习记录(五):卷积神经网络的实现
- BI-SQL丨WHILE
- CF603E Pastoral Oddities
- The path to uniting the programmer: "titles bucket" to the highest state of pragmatic
- TaskDispatcher source code parsing
- Shell变量与赋值、变量运算、特殊变量、重定向与管渠
- svg波浪动画js特效代码
- libudev manual
猜你喜欢
无代码开发平台全部应用设置入门教程
重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践
Why did I switch from developer to testing, 3 years software testing engineer, tell you the secret of this
Flask Framework - Flask-Mail Mail
js男女身高体重关系图
LeetCode二叉树系列——102.二叉树的层序遍历
jsArray array copy method performance test 2207300040
Self-tuning PID self-tuning control 】 【
Study Notes - Becoming a Data Analyst in Seven Weeks "Week 2: Business": Business Analysis Metrics
“12306” 的架构到底有多牛逼
随机推荐
MQTT网关读取西门子PLC数据传输到阿里云平台案例教程
43.【list链表的定义及初始化】
[C# 循环跳转]-C# 中的 while/do-while/for/foreach 循环结构以及 break/continue 跳转语句
LeetCode二叉树系列——199二叉树的右视图
Flask Framework - Sijax
What is the level of Ali P7?
跳槽前,把自己弄成卷王
(论文翻译]未配对Image-To-Image翻译使用Cycle-Consistent敌对的网络
【ROS进阶篇】第十一讲 基于Gazebo和Rviz的机器人联合仿真(运动控制与传感器)
Why do software testing have to learn automation?Talk about the value of automated testing in my eyes
js背景切换时钟js特效代码
数据中台建设(五):打破企业数据孤岛和提取数据价值
sql中ddl和dml(sql与access的区别)
AT4108 [ARC094D] Normalization
[ARC092D] Two Faced Edges
43.【list的简单属性】
SQL 改写系列七:谓词移动
数字信号处理课程实验报告(数字信号处理需要什么基础)
Huawei's 7-year-experienced software testing director, gives some advice to all friends who want to change careers to learn software testing
无代码开发平台应用可见权限设置入门教程