双端数组
区别于 vector (单端数组),
构造函数
注意:读取数据时,const修饰保证函数内只能读取,不能修改数据
void print(const deque<int>& deq)
{
for (deque<int>::const iterator it = deq.begin(); it != deq.end(); it++)
{
cout << *it <<" ";
}
cout << endl;
}
赋值操作
对容器大小操作
* deque没有容量的概念
* 判断是否为空 --- empty
* 返回元素个数 --- size
* 重新指定个数 --- resize
deque 插入和删除
* 插入和删除提供的位置是迭代器!
* 尾插 --- push_back
* 尾删 --- pop_back
* 头插 --- push_front
* 头删 --- pop_front
deque数据存取
deque排序
#include <algorithm>
案例-评委打分
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。
1. 创建五名选手,放到vector中
2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
3. sort算法对deque容器中分数排序,去除最高和最低分
4. deque容器遍历一遍,累加总分
5. 获取平均分
#include<iostream>
using namespace std;
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
class Person
{
public:
int score;
string name;
Person(string name,int score){
this->name = name;
this->score = score;
}
};
void setScore(vector <Person>&vec)
{
for (vector<Person>::iterator it = vec.begin(); it != vec.end(); it++)
{
deque <int>d;
for (int i = 0; i < 10; i++)
{
int socre = rand() % 41 + 60; //数字范围60-100
d.push_back(socre);
}
sort(d.begin(), d.end());
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).score = avg;
}
}
void print_Person(const vector <Person>&vec)
{
for(vector<Person>::const_iterator it=vec.begin();it!=vec.end();it++)
{
//cout << "选手测试" << endl;
cout <<"选手名字:"<< (*it).name << endl;
cout << "选手平均分" << it->score << endl;
}
}
void creat_Person(vector <Person>&vec)
{
string str= "ABCDE";
for (int i = 0; i < str.size(); i++)
{
int score = 0;
Person p("选手", score);
p.name += str[i];
vec.push_back(p);
}
}
int main()
{
vector<Person> vec;
creat_Person(vec);
setScore(vec);
print_Person(vec);
system("pause");
return 0;
}