deque容器(C++)

3.3 deque 容器

3.1.1 deque 容器基本概念

功能:

  • 双端数组,可以对头端进行插入删除操作

deque 与vector 区别:

  • vector 对于头部的插入删除效率低,数据量越大,效率越低
  • deque 相对而言,对头部的插入删除速度会比vector 快
  • vector 访问元素时的速度会比deque 快,这和两者内部实现有关

deque 内部工作原理:

  • deque 内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实数据
  • 中控器维护的是每个缓冲区的地址,使得使用deque 时像一片连续的内存空间
  • deque 容器的迭代器也是支持随机访问的
3.3.2 deque 构造函数

功能描述:

  • deque 容器构造

函数原型:

  • deque<T> deqT; //默认构造形式
  • deque(beg, end); //构造函数将[beg,end]区间中的元素拷贝给本身
  • deque(n, elem); //构造函数将n个elem拷贝给本身
  • deque(const deque& deq); //构造拷贝函数

示例:

#include<iostream>
#include<deque>
using namespace std;

//deque构造函数

//为了防止篡改数据
//将打印设置为只读,加上const
void PrintDeque(const deque<int>& d) {
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		//*it = 100;//报错,容器中的数据只读,不可修改
		cout << *it << " ";
	}
	cout << endl;
}

void test01() {
	//默认
	deque<int>d1;
	for (int i = 0; i < 10; i++) {
		d1.push_back(i);
	}
	PrintDeque(d1);
	//区间元素拷贝
	deque<int> d2(d1.begin()+1, d1.end()-1);
	PrintDeque(d2);
	//拷贝构造
	deque<int> d3(d2);
	PrintDeque(d3);
	//n个elem
	deque<int> d4(4, 100);
	PrintDeque(d4);
}


//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
3.3.3 deque 赋值操作

功能描述:

  • 给deque 容器进行赋值

容器原型:

  • deque& operator=(const deque& deq); //重载等号操作符
  • assign(beg,end); //将[beg,end]区间中的数据拷贝赋值给本身
  • assign(n,elem); //将n个elem拷贝赋值给本身

示例:

#include<iostream>
#include<deque>
using namespace std;

void PriDeque(const deque<int>& d) {
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//deque赋值操作
void test01() {
	deque<int> d1;
	d1.push_back(0);
	d1.push_back(1);
	d1.push_back(2);
	d1.push_back(3);
	d1.push_back(4);
	PriDeque(d1);
	deque<int> d2;
	d2 = d1;
	PriDeque(d2);

	deque<int> d3;
	d3.assign(d1.begin()+1, d1.end()-1);
	PriDeque(d3);
	d3.assign(10,100);
	PriDeque(d3);
}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
3.3.4 deque 大小操作

功能描述:

  • 对deque 容器的大小进行操作

函数原型:

  • deque.empty(); //判断容器是否为空

  • deque.size(); //返回容器中元素的个数

  • deque.resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置

    ​ //如果容器变短,则末尾超出的容器长度的元素被删除

  • deque.resize(num,elem); //重新指定容器的长度为num,若容器变长,则以elem填充新位置

    ​ //如果容器变短,则末尾超出的容器长度的元素被删除

示例:

#include<iostream>
#include<deque>
using namespace std;

void PriDeque(const deque<int> d) {
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//deque大小操作
void test01() {
	deque<int> d1;
	for (int i = 0; i < 10; i++) {
		d1.push_back(i);
	}
	PriDeque(d1);

	if (d1.empty()) {
		cout << "容器为空" << endl;
	}
	else {
		cout << "容器不为空" << endl;
	}
	//deque容器没有容量操作
	cout << "d1的大小为:" << d1.size() << endl;
	d1.resize(5);
	PriDeque(d1);
	cout << "d1的大小为:" << d1.size() << endl;
	d1.resize(10, 10);
	PriDeque(d1);
	cout << "d1的大小为:" << d1.size() << endl;
}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}

总结:

  • deque 没有容量的概念
  • 判断是否为空 — empty
  • 返回元素个数 — size
  • 重新指定个数 — resize
3.3.5 deque 插入和删除

功能描述:

  • 向deque 容器中插入和删除数据

函数原型:

​ 两端插入操作:

  • push_back(elem); //在容器尾部添加一个数据
  • push_front(elem); //在容器头部插入一个数据
  • pop_back(); //删除容器最后一个数据
  • pop_front(); //删除容器第一个数据

​ 指定位置操作:

  • insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置
  • insert(pos,n,elem); //在pos位置插入n个elem的数据,无返回值
  • insert(pos,beg,end); //在pos位置插入[beg,end]区间的数据,无返回值
  • clear(); //清空容器内的所有数据
  • erase(beg,end); //删除[beg,end]区间的数据,返回下一个数据的位置
  • erase(pos); //删除pos位置指定的数据,返回下一个数据的位置

示例:

#include<iostream>
#include<deque>
using namespace std;

void PriDeque(const deque<int>& d) {
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//deque插入和删除
void test01() {
	//两端操作
	deque<int> d1;
	for (int i = 0; i < 10; i++) {
		d1.push_front(i);
	}
	PriDeque(d1);
	d1.pop_back();
	PriDeque(d1);
	d1.pop_front();
	PriDeque(d1);
	//指定位置操作
	d1.insert(d1.begin()+1, 10);
	PriDeque(d1);
	d1.insert(d1.begin(), 2, 10);
	PriDeque(d1);
	//按照区间插入
	deque<int> d2;
	d2.push_back(1);
	d2.push_back(2);
	d2.push_back(3);
	PriDeque(d2);
	d1.insert(d1.begin(), d2.begin(), d2.end());
	PriDeque(d1);
	//删除
	d1.erase(d1.begin() + 1);
	PriDeque(d1);
	d1.erase(d1.begin() + 1, d1.end() - 1);
	PriDeque(d1);
	d1.clear();
	PriDeque(d1);

}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}

总结:

  • 插入何删除提供的位置是迭代器
3.3.6 deque 数据存取

功能描述:

  • 对deque 中的数据做存取操作

函数原型:

  • at(int idx); //返回索引[idx]所指的数据
  • operator[idx]; //返回索引[idx]所指的数据
  • front(); //返回容器中第一个数据元素
  • back(); //返回容器中最后一个数据元素
#include<iostream>
#include<deque>
using namespace std;

void PriDeque(const deque<int>& d) {
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//deque数据存取
void test01() {
	deque<int> d;
	for (int i = 0; i < 10; i++) {
		d.push_back(i);
	}
	PriDeque(d);
	cout << "d中第三个元素是:" << d.at(2) << endl;
	cout << "d中第二个元素是:" << d[1] << endl;
	cout << "d中第一个元素是:" << d.front() << endl;
	cout << "d中最后一个个元素是:" << d.back() << endl;

}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
3.3.7 deque 排序

功能描述:

  • 利用算法实现对deque 容器进行排序

算法:

  • sort(iterator beg, iterator end) //对beg和end区间内元素进行排序

示例:

#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;

void PriDeque(const deque<int>& d) {
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//deque排序
void test01() {
	deque<int> d;
	d.push_back(5);
	d.push_back(2);
	d.push_back(1);
	d.push_back(3);
	d.push_back(4);
	PriDeque(d);
	//排序,默认从小到大
	//对于支持随机访问的迭代器的容器,都可以利用sort算法直接对其排序
	sort(d.begin(), d.end());
	PriDeque(d);

}

//主函数
int main() {
	test01();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三月江东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值