一,deque的基础知识

1.deque的基础

  • deque是“double-ended-queue”的缩写,意思是双端队列,其和vector的区别在于vector是单端的。
  • deque在头部和尾部插入和删除元素非常快,但是在中部插入和删除元素比较耗时。
  • deque支持随机存取元素(即通过数组下标可以直接访问和修改元素),其底层数据结构是数组。
  • deque的操作和vector很相似,在许多操作上可以直接替换。
  • 在使用deque之前需要导入头文件# include<deque>

二,deque的构造函数

1.无参构造函数

/* 无参构造函数 */
deque<int> d1;

2.带参数的构造函数

/* 带参构造函数一:用10个字符'A'初始化容器 */
deque<char> d1(,'A');
/* 用容器d1的前五个元素来初始化容器d2 */
deque<char> d2(d1.begin(),d1.begin()+);

3.拷贝构造函数

/* 定义含有是个字符'A'的容器 */
deque<char> d1(,'A');
/* 用上面的容器初始化下面的容器 */
deque<char> d2 = d1;

4.析构函数

  deque的析构函数用来释放容器中元素所占用的内存。

三,deque的操作符重载

1.赋值操作符重载

// 定义双端队列d1
deque<int> d1(, );
// 定义双端队列d2
deque<int> d2;
// 使用赋值操作
d2 = d1;

2.数组下标操作符重载

// 定义双端队列d
deque<int> d(, );
// 数组下标操作,获取容器第二个元素
int i2 = d[];
cout << "i2 = " << i2 << endl;
// 数组下标操作,修改容器的第三个元素
d2[] = ;
cout << "i3 = " << d[] << endl;

四,deque的成员函数

1.在头部插入和移除元素

// 定义容器
deque<char> d1;
// 头部插入元素
d1.push_front('C');
d1.push_front('B');
d1.push_front('A');
// 头部元素删除
d1.pop_front();

2.在尾部插入和移除元素

// 定义容器
deque<char> d2;
// 尾部插入元素
d2.push_back('X');
d2.push_back('Y');
d2.push_back('Z');
// 删除尾部元素
d2.pop_back();

3.获取头部和尾部元素

// 定义容器
deque<char> d1(,'A');
// 获取首元素
char first = d1.front();
// 获取尾元素
char last = d1.back();

4.获取容器的长度

// 定义容器
deque<char> d1(,'A');
// 获取容器长度
int size = d1.size();

5.清空容器的元素

// 定义容器
deque<char> d1(,'A');
// 清空元素
d1.clear();

6.判断元素是否为空

// 定义容器
deque<char> d1(,'A');
// 判断元素是否为空
bool isEmpty = d1.empty();

7.重置容器长度

// 定义容器
deque<char> d(,'A');
// 重置容器元素为10个,不足的位置用字符'B'替代
d.resize(,'B');
// 重置容器元素为5个,多余的元素删除
d.resize();

8.获取和修改容器元素

// 定义容器
deque<char> d(,'A');
// 获取第3个元素
char c3 = d.at();
// 修改第三个元素
d.at() = 'B';

9.容器插入元素

// 定义容器d1
deque<char> d1(,'A');
// 定义容器d2
deque<char> d2(, 'B');
// 在容器d2的第二个位置插入字符'X'
d2.insert(d2.begin() + , 'X');
// 在容器d2的第四个位置插入5个字符'Z'
d2.insert(d2.begin() + , , 'Z');
// 在容器d2的最后插入容器d1
d2.insert(d2.end(), d1.begin(), d1.end());

10.容器删除元素

// 定义容器d1
deque<char> d1(,'A');
// 删除第2个元素
d1.erase(d1.begin() + );
// 删除第2个到第4个元素(左开右闭)
d1.erase(d1.begin() + , d1.begin() + );

11.容器的遍历

// 定义容器d2
deque<char> d2(,'A');
// 增强for遍历
for (char tmp : d2)
{
cout << tmp << " ";
}
cout << endl;
// 迭代器的正向遍历
for (deque<char>::iterator it = d2.begin(); it != d2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
// 迭代器的反向遍历
for (deque<char>::reverse_iterator it = d2.rbegin(); it != d2.rend(); it++)
{
cout << *it << " ";
}
cout << endl;

12.容器的遍历删除

# include<iostream>
# include<deque> using namespace std; int main()
{
// 定义容器
deque<int> v;
// 添加数据
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
v.push_back();
// 这里注意不需要++it
for (deque<int>::iterator it = v.begin(); it != v.end();)
{
// 删除偶数
if (*it % == )
{
// erase删除该元素后返回下一个元素的迭代器
it = v.erase(it);
}
else {
it++;
}
}
// 遍历
for (int tmp : v)
{
cout << tmp << " ";
}
cout << endl; return ;
}

五,deque存储自定义数据类型

1.定义学生类

#pragma once
# include<iostream>
using namespace std; class Student
{
private:
char * name;
int age;
public:
Student();
Student(char * name, int age);
Student(const Student& student);
~Student();
public:
friend ostream& operator<<(ostream& out, Student& student);
};

2.学生类的实现

# define _CRT_SECURE_NO_WARNINGS
# include<iostream>
# include "Student.h"
using namespace std; /* 无参构造函数 */
Student::Student()
{
this->name = NULL;
this->age = ;
}
/* 有参构造函数 */
Student::Student(char * name, int age)
{
this->name = new char[strlen(name) + ];
strcpy(this->name, name);
this->age = age;
}
/* 拷贝构造函数 */
Student::Student(const Student& student)
{
this->name = new char[strlen(student.name) + ];
strcpy(this->name, student.name);
this->age = student.age;
}
/* 析构函数 */
Student::~Student()
{
if (this->name != NULL)
{
delete[] this->name;
this->name = NULL;
this->age = ;
} }
/* 左移操作符重载 */
ostream& operator<<(ostream& out, Student& student)
{
out << student.name << " = " << student.age;
return out;
}

3.测试类

# include<iostream>
# include<deque>
# include"Student.h"
using namespace std; int main()
{
// 定义自定义对象
Student s1("刘备", );
Student s2("关羽", );
Student s3("张飞", );
// 放入容器
deque<Student> d;
// 存入
d.push_back(s1);
d.push_back(s2);
d.push_back(s3);
// 遍历
for (Student stu : d)
{
cout << stu << endl;
} return ;
}

最新文章

  1. 从Vue.js窥探前端行业
  2. 一鼓作气 博客--第五篇 note5
  3. jquery.serialize
  4. Swift 04.Functions
  5. Android requires compiler compliance level 5.0 or 6.0. Found &#39;1.8&#39; instead. Please use Android Tools&gt;Fix project Properties.
  6. SQL server 2012
  7. ASP.NET Web API中的依赖注入
  8. Java链式方法 连贯接口(fluent interface)
  9. UVa 1301 - Fishnet
  10. jsp 页面实现增减行
  11. JS 导出图片,toDataURL
  12. poj 2186 (强连通缩点)
  13. HDOJ1728 BFS【STL__queue_的应用】
  14. hdu Text Reverse
  15. PHP验证码程序
  16. Android性能优化之Bitmap的内存优化
  17. 使用Log4Net进行错误日志记录
  18. 爬取mzi.com妹子图片网站(requests库)
  19. 第25月第3天 Mxshop项目记录01
  20. Java ——基础语法

热门文章

  1. Searching with regular sentences will only get you so far – if you need to find something a bit tricky turn to these advanced yet simple methods--转
  2. Static关键字深入理解
  3. LA 3026 - Period KMP
  4. 6.4 Android硬件访问服务编写HAL代码
  5. 【习题5-4 UVA-10763】Foreign Exchange
  6. 【SPOJ 694】Distinct Substrings (更直接的求法)
  7. libSVM介绍(二)
  8. Vue源码--深入模板渲染
  9. 【29.70%】【codeforces 723D】Lakes in Berland
  10. swift学习第二天:swift中的基本数据类型