deque 双端队列
deque 删除操作
deque案例:
#if 1
#include<iostream>
#include<deque>
using namespace std;
void PrintDeque(deque<int>& d) {for (deque<int>::iterator it = d.begin(); it != d.end(); it++) {cout << *it << " ";}cout << endl;
}
//deque初始化
void test01() {deque<int> d;deque<int> d2(10, 5);deque<int> d3(d2.begin(), d2.end());deque<int> d4(d3);//打印PrintDeque(d4);
}
/*
输出:
5 5 5 5 5 5 5 5 5 5
*///赋值 大小操作
void test02() {deque<int> d1;deque<int> d2,d3,d4;d1.assign(10, 5);d4.assign(10, 4);d2.assign(d1.begin(), d1.end()); //迭代器指定区间赋值d3 = d2; //等号赋值cout << "未交换前的d1和d4:" << endl;PrintDeque(d1);PrintDeque(d4);swap(d1, d4);cout << "交换后的d1和d4:" << endl;PrintDeque(d1);PrintDeque(d4);if (d1.empty())cout << "d1 is empty!" << endl;elsecout << "d1 is not empty!" << endl;d1.resize(5); //后5个元素扔掉PrintDeque(d1);d1.resize(8); //以默认值填充新位置PrintDeque(d1);d1.resize(10, 88); //容器变长,以88进行填充PrintDeque(d1);}
/*
输出:
未交换前的d1和d4:
5 5 5 5 5 5 5 5 5 5
4 4 4 4 4 4 4 4 4 4
交换后的d1和d4:
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
d1 is not empty!
4 4 4 4 4
4 4 4 4 4 0 0 0
4 4 4 4 4 0 0 0 88 88
*/
//deque插入和删除
void test03() {deque<int> d1;d1.push_back(100);d1.push_front(200);d1.push_back(300);d1.push_back(400);d1.push_front(500);PrintDeque(d1);d1.pop_back();d1.pop_front();PrintDeque(d1);
}
/*
输出:
500 200 100 300 400
200 100 300
*/
int main()
{cout<<"test01" << endl;test01();cout << "test02" << endl;test02();cout << "test03" << endl;test03();
}
#endif
打分案例:
#include<iostream>
#include<vector>
#include<deque>
#include<algorithm>
#include<string>
using namespace std;
//评委打分案例(sort)
//创建5个选手,姓名,得分 10个评委对5个选手打分
//得分规则:去除最低分,去除最高分 去除平均分
//按得分对5名选手排名
#if 0
//我的思路
typedef struct Person {
public:Person(double Score,char* Name):score(Score),name(Name){}
public:double score;char* name;
};
int main() {Person p1(0, "wang");Person p2(0, "yu");Person p3(0, "da");Person p4(0, "zhao");Person p5(0, "liu");vector<Person> v;v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {cout << "请输入对" << (*it).name << "的打分:" << endl;deque<double> d;for (int i = 0; i < 10; i++){while (cin.get() != EOF) //错误{d.push_back(cin.get());}}//排序 去除左右两端 然后去平均数 然后赋值给此选手的score 用map<Person,score>进行排序}return 0;
}
#endif#if 1
class Player {
public:Player(){}Player(double Score, string Name) :score(Score), name(Name) {}
public:double score;string name;
};
//创建选手
void Create_Player(vector<Player>& v) {string nameSpeed = "ABCDE";for (int i = 0; i < 5; i++){Player p;p.name = "选手";p.name += nameSpeed[i];p.score = 0;v.push_back(p);}}
void PrintdScore(int val) {cout << val << " ";
}
//打分
void set_Score(vector<Player>& v){for (vector<Player>::iterator it = v.begin(); it != v.end(); it++) {string name=(*it).name;//当前学生进行打分deque<int> dScore;for (int i = 0; i < 10; i++){int score = rand() % 41 + 60;dScore.push_back(score);}sort(dScore.begin(), dScore.end()); //默认从小到大for_each(dScore.begin(), dScore.end(), PrintdScore);cout << endl;//去除最高分 最低分dScore.pop_front();dScore.pop_back();//求平均分int totalScore = 0;for (deque<int>::iterator itt = dScore.begin(); itt != dScore.end(); itt++) {totalScore += (*itt);}int averageScore = totalScore / dScore.size();//保存分数(*it).score = averageScore;}}
//排序规则
bool mycompare(Player p1, Player p2) {return p1.score > p2.score;
}
//排名 sort默认从小到大 希望从打到小
void Print_Rank(vector<Player>& v) {//排序sort(v.begin(), v.end(), mycompare);//打印for (vector<Player>::iterator it = v.begin(); it != v.end(); it++) {cout << "姓名:"<<(*it).name <<"得分:"<<(*it).score<< endl;}
}
int main() {vector<Player> vPlist;Create_Player(vPlist);set_Score(vPlist);Print_Rank(vPlist);return 0;
}
#endif