5.1 双向开口连续线性空间,可以在头尾两端分别做元素的插入(push_pront)与删除(pop
5.2 deque与vector最大的差异:1、deque允许使用常数项时间(以固定的步骤)对头端进行元素的插入与删除操作2、deque没有容器的概念,内部用中控器控制连续分段空间,可以随时增加一段新的空间进行拼接(vector需要进行一系列的扩容、复制、释放操作)
5.3 作业
5.4 所有程序
#include "pch.h"
#include <iostream>
#include<deque>
#include<vector>
#include<algorithm>
#include<string>
#include<ctime>
using namespace std;
void printDeque(const deque<int>&d)
{
//iterator 普通迭代器
//reverse_iterator 反转迭代器
//const_iterator 只读迭代器
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
cout << *it << " ";
cout << endl;
}
void test01()
{
deque<int>d;
d.push_back(10);
d.push_back(30);
d.push_back(20);
d.push_back(40);
printDeque(d);
deque<int>d2(10, 10);
d.swap(d2);
printDeque(d);
if (d.empty())
{
cout << "d 为空 " << endl;
}
else
{
cout << "d 不为空——size= " << d.size() << endl;
}
}
void test02()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_back(40);
d.push_front(100);
d.push_front(200);
printDeque(d);//200 100 10 20 30 40
d.pop_back();//删除40
d.pop_front();//删除200
printDeque(d);//100 10 20 30
cout << "第一个元素:" << d.front() << endl;
cout << "最后一个元素:" << d.back() << endl;
//插入
d.insert(++d.begin(), 10000);
printDeque(d);//100 10000 10 20 30
//删除
d.erase(++d.begin());
printDeque(d);//100 10 20 30
//删除区间
d.erase(++d.begin(), --d.end());
printDeque(d);//100 30
}
//作业 byself
class player
{
private:
string name;
deque<double>score;
double average;
public:
player();
player(string name,deque<double>sco);
double aver() { return average; };
};
player::player()
{
cout << "enter name: ";
cin >> name;
cout << "enter " << name << "'s score" << endl;
double s_temple;
for (int i = 0; i < 10; i++)
{
cout << "enter score" << i + 1 << ": ";
cin >> s_temple;
score.push_back(s_temple);
}
sort(score.begin(), score.end());
score.pop_back();
score.pop_front();
double sum=0;
for (deque<double>::iterator it = score.begin(); it != score.end(); it++)
sum = sum + *it;
average = sum / score.size();
}
player::player(string na,deque<double>sco)
{
name = na;
score = sco;
sort(sco.begin(), sco.end());
score.pop_back();//去除最低分
score.pop_front();//去除最高分
double sum=0;
for (unsigned int i = 0; i < score.size(); i++)
sum = sum + sco[i];
average = sum / score.size();
}
void test03()
{
vector<player>v;//创建5名选手
for (int i = 0; i < 5; i++)
{
player temple;
v.push_back(temple);
}
for (unsigned int i = 0; i < v.size(); i++)
{
cout << "average score of player No." << i + 1 << ": "
<< v[i].aver()<<endl;
}
}
//作业 讲解
class Person
{
public:
string m_name;
int m_score;
Person(string name, int score)
{
m_name = name;
m_score = score;
}
};
void creatPerson(vector<Person>&v)//一定要有引用传递
{
string nameSeed = "ABCDE";
for (int i = 0; i < 5; i++)
{
string name = "选手";
name += nameSeed[i];//name=选手A/B....
int score = 0;
Person p(name, score);
v.push_back(p);
}
}
void setScore(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
//有10个评委给每个人打分
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;
}
}
void showScore(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名: " << it->m_name << " 分数: " << it->m_score
<< endl;
}
}
void test04()
{
//设置随机数种子
srand((unsigned int)time(NULL));
//1.存放选手容器
vector<Person>v;
//2.创建5名选手
creatPerson(v);
//3.打分
setScore(v);
//4.显示得分
showScore(v);
}
int main()
{
//test01();
//test02();
//test03();
test04();
}