1.deque容器
此处不再对函数具体介绍,可进入Cpp参考
#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> d1;
deque<int> d2(10, 5);
deque<int> d3(d2.begin(),d2.end());
deque<int> d4(d3);
//打印d4
PrintDeque(d4);
}
//赋值 大小操作
void test02(){
deque<int> d1;
deque<int> d2;
deque<int> d3;
d1.assign(10, 5);
d2.assign(d1.begin(), d1.end()); //迭代器指定区间赋值
d3 = d2; //等号赋值
d1.swap(d2); //交换两个空间元素
if (d1.empty()){
cout << "空!" << endl;
}
else{
cout << "size:" << d1.size() << endl;
}
d1.resize(5); //10个元素,后五个元素扔掉
}
//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);
//500 200 100 300 400
PrintDeque(d1);
int val = d1.front(); //拿到被删除的数据
d1.pop_front(); //删除
val = d1.back();
d1.pop_back(); //删除最后一个元素 pop_back()没有返回值
}
int main(void){
//test01();
test03();
return 0;
}
deque容器案例
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;
//评委打分案例(sort 算法排序)
//创建 5 个选手(姓名,得分) , 10 个评委对 5 个选手进行打分
//得分规则:去除最高分,去除最低分,取出平均分
//按得分对 5 名选手进行排名
//选手类
class Player{
public:
Player(){}
Player(string name,int score):mName(name),mScore(score){}
public:
string mName;
int mScore;
};
//创建选手
void Create_Player(vector<Player>& v){
string nameSeed = "ABCDE";
for (int i = 0; i < 5;i ++){
Player p;
p.mName = "选手";
p.mName += nameSeed[i];
p.mScore = 0;
v.push_back(p);
}
}
void PrintScore(int val){
cout << val << " ";
}
//打分
void Set_Score(vector<Player>& v){
srand(time(NULL));
for (vector<Player>::iterator it = v.begin(); it != v.end();it ++){
//当前学生进行打分
deque<int> dScore;
for (int i = 0; i < 10;i++){
int score = rand() % 41 + 60; // 60-100的随机数
cout << score << " ";
dScore.push_back(score);
}
cout << endl;
//对分数排序 默认从小到大
sort(dScore.begin(),dScore.end());
//for_each(dScore.begin(), dScore.end(), PrintScore);
//cout << endl;
//去除最高分 去除最低分
dScore.pop_front();
dScore.pop_back();
//求平均分
int totalScore = 0;
for (deque<int>::iterator dit = dScore.begin(); dit != dScore.end(); dit++){
totalScore += (*dit);
}
int avgScore = totalScore / dScore.size();
//保存分数
(*it).mScore = avgScore;
}
}
//排序规则
bool mycompare(Player& p1,Player& p2){
return p1.mScore > p2.mScore;
}
//根据选手分数排名 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).mName << "得分:" << (*it).mScore << endl;
}
}
int main(void){
//定义vector容器,保存选手信息
vector<Player> vPlist;
Create_Player(vPlist);
Set_Score(vPlist);
Print_Rank(vPlist);
return 0;
}
2.stack容器
此处不再对函数具体介绍,可进入Cpp参考
#include <iostream>
#include <stack>
using namespace std;
void test01(){
//初始化
stack<int> s1;
stack<int> s2(s1);
//stack操作
s1.push(10);
s1.push(20);
s1.push(30);
s1.push(100);
cout << "栈顶元素:" << s1.top() << endl;
s1.pop(); //删除栈顶元素
//打印栈容器的数据
while (!s1.empty()){
cout << s1.top() << " ";
s1.pop();
}
cout << "size:" << s1.size() << endl;
}
int main(void){
test01();
return 0;
}
3.queue容器
此处不再对函数具体介绍,可进入Cpp参考
#include <iostream>
#include<queue>
using namespace std;
void test01(){
queue<int> q; //创建队列
q.push(10);
q.push(20);
q.push(30);
q.push(40);
cout << "队尾:" << q.back() << endl;
//输出顺序? 10 20 30 40
while (q.size() > 0){
cout << q.front() << " "; //输出队头元素
q.pop(); //删除队头
}
}
int main(void){
test01();
return 0;
}
4.list容器
此处不再对函数具体介绍,可进入Cpp参考
#include <iostream>
#include <list>
using namespace std;
//初始化
void test01(){
list<int> mlist1;
list<int> mlist2(10,10); //有参构造
list<int> mlist3(mlist2);//拷贝构造
list<int> mlist4(mlist2.begin(), mlist2.end());
for (list<int>::iterator it = mlist4.begin(); it != mlist4.end();it ++){
cout << *it << " ";
}
cout << endl;
}
//list容器插入删除
void test02(){
list<int> mlist;
//插入操作
mlist.push_back(100);
mlist.push_front(200);
mlist.insert(mlist.begin(),300);
mlist.insert(mlist.end(),400);
mlist.insert(mlist.end(), 200);
list<int>::iterator it = mlist.begin();
it++;
it++;
mlist.insert(it, 500);
mlist.insert(mlist.end(), 200);
//删除
//mlist.pop_back();
//mlist.pop_front();
//mlist.erase(mlist.begin(), mlist.end()); //mlist.clear();
mlist.remove(200); //删除匹配所有值
list<int>::iterator testit = mlist.begin();
for (int i = 0; i < mlist.size() - 1;i++){
testit++;
}
(*(mlist.end()));
cout << "------------" << endl;
cout << (*testit) << endl;
cout << mlist.back() << endl;
cout << "------------" << endl;
//删除所有200 还是删除第一次出现的200
/*
for (list<int>::iterator lit = mlist.begin(); lit != mlist.end(); lit++){
cout << *lit << " ";
}
cout << endl;
*/
}
//赋值操作
void test03(){
list<int> mlist;
mlist.assign(10, 10);
list<int> mlist2;
mlist2 = mlist;
mlist2.swap(mlist);
}
//排序 翻转
void test04(){
list<int> mlist;
for (int i = 0; i < 10;i++){
mlist.push_back(i);
}
for (list<int>::iterator it = mlist.begin(); it != mlist.end(); it++){
cout << *it << " ";
}
cout << endl;
//容器元素反转
mlist.reverse();
for (list<int>::iterator it = mlist.begin(); it != mlist.end(); it++){
cout << *it << " ";
}
cout << endl;
}
bool mycompare05(int v1,int v2){
return v1 > v2;
}
//排序
void test05(){
list<int> mlist;
mlist.push_back(2);
mlist.push_back(1);
mlist.push_back(7);
mlist.push_back(5);
for (list<int>::iterator it = mlist.begin(); it != mlist.end(); it++){
cout << *it << " ";
}
cout << endl;
//排序 对象怎么排序? 默认从小到大
mlist.sort();
for (list<int>::iterator it = mlist.begin(); it != mlist.end(); it++){
cout << *it << " ";
}
cout << endl;
//从大到小
mlist.sort(mycompare05);
for (list<int>::iterator it = mlist.begin(); it != mlist.end(); it++){
cout << *it << " ";
}
cout << endl;
}
int main(void){
//test01();
//test02();
//test04();
test05();
return 0;
}
5.set容器
此处不再对函数具体介绍,可进入Cpp参考
注意仿函数mycompare以及mycompare02的使用。
#include <iostream>
#include <set>
#include <list>
using namespace std;
//仿函数
class mycompare{
public:
bool operator()(int v1,int v2){
return v1 > v2;
}
};
//set容器初始化
void test01(){
set<int, mycompare> s1; //自动进行排序 默认从小到大
s1.insert(7);
s1.insert(2);
s1.insert(4);
s1.insert(5);
s1.insert(1);
for (set<int>::iterator it = s1.begin(); it != s1.end();it ++){
cout << *it << " ";
}
cout << endl;
#if 0
//赋值操作
set<int> s2;
s2 = s1;
//删除操作
s1.erase(s1.begin());
s1.erase(7);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++){
cout << *it << " ";
}
cout << endl;
//先序遍历 中序遍历 后序遍历
//如何改变默认排序?
#endif
}
//set查找
void test02(){
set<int> s1;
s1.insert(7);
s1.insert(2);
s1.insert(4);
s1.insert(5);
s1.insert(1);
set<int>::iterator ret = s1.find(14);
if (ret == s1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "ret:" << *ret << endl;
}
//找第一个大于等于key的元素
ret = s1.lower_bound(2);
if (ret == s1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "ret:" << *ret << endl;
}
//找第一个大于key的值
ret = s1.upper_bound(2);
if (ret == s1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "ret:" << *ret << endl;
}
//equal_range 返回Lower_bound 和 upper_bound值
pair<set<int>::iterator,set<int>::iterator> myret = s1.equal_range(2);
if (myret.first == s1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "myret:" << *(myret.first) << endl;
}
if (myret.second == s1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "myret:" << *(myret.second) << endl;
}
}
class Person{
public:
Person(int age,int id):id(id),age(age){}
public:
int id;
int age;
};
class mycompare2{
public:
bool operator()(const Person p1,const Person p2)const{
if (p1.id == p2.id){
return p1.age > p2.age;
}
else{
return p1.id > p2.id;
}
}
};
void test03(){
set<Person, mycompare2> sp; //set需要排序,当你放对象,set知道怎么排吗?
Person p1(10, 20), p2(20, 20), p3(50, 60);
sp.insert(p1);
sp.insert(p2);
sp.insert(p3);
Person p4(10, 30);
sp.insert(p4);
for (set<Person, mycompare2>::iterator it = sp.begin(); it != sp.end();it++){
cout << (*it).age << " " << (*it).id << endl;
}
//查找
set<Person, mycompare2>::iterator ret = sp.find(p4);
if (ret == sp.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到:" << (*ret).id << " " << (*ret).age << endl;
}
}
int main(void){
//test01();
//test02();
test03();
return 0;
}
6.对组
此处不再对函数具体介绍,可进入Cpp参考
pair的用法借鉴下面代码:
#include <iostream>
#include <string>
using namespace std;
void test01(){
//构造方法
pair<int, int> pair1(10,20);
cout << pair1.first << " " << pair1.second << endl;
pair<int, string> pair2 = make_pair(10, "aaa");
cout << pair2.first << " " << pair2.second << endl;
pair<int, string> pair3 = pair2;
}
int main(void){
test01();
return 0;
}