69.deque容器

本文深入探讨了C++ STL中的deque容器,详细介绍了其特点、构造、赋值、大小操作、双端插入删除、数据存取及排序等功能,并通过代码示例展示了如何使用deque进行高效的数据管理和操作。

Vector容器是单向开口的连续内存空间,deque则是一种双向开口的连续线性空间。所谓的双向开口,意思是可以在头尾两端分别做元素的插入和删除操作

deque容器
    双端数组  没有容量 
    API  赋值、构造、大小、交换、插入 、删除
    头部删除 头部插入
    pop_front
    push_front
    3种迭代器  iterator 普通  reverse_iterator 逆序迭代器  const_iterator只读迭代器
    排序 sort 引用头文件 algorithm  
    sort(d.begin(),d.end()) 从小到大

main.cpp

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <deque>
#include <algorithm>
/*
deque构造函数
deque<T> deqT;//默认构造形式
deque(beg, end);//构造函数将[beg, end)区间中的元素拷贝给本身。
deque(n, elem);//构造函数将n个elem拷贝给本身。
deque(const deque &deq);//拷贝构造函数。

3.3.3.2 deque赋值操作
assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。
deque& operator=(const deque &deq); //重载等号操作符
swap(deq);// 将deq与本身的元素互换

3.3.3.3 deque大小操作
deque.size();//返回容器中元素的个数
deque.empty();//判断容器是否为空
deque.resize(num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
deque.resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置,如果容器变短,则末尾超出容器长度的元素被删除。
*/
void printDeque(const deque<int>&d)
{
	//iterator 普通迭代器  reverse_iterator 逆序迭代器  const_iterator 只读迭代器
	for (deque<int>::const_iterator it = d.begin(); it != d.end();it++)
	{
		//*it = 100000;
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	deque<int>d;

	d.push_back(10);
	d.push_back(40);
	d.push_back(30);
	d.push_back(20);

	printDeque(d);

	deque<int>d2(d.begin(), d.end());
	d2.push_back(10000);

	//交换
	d.swap(d2);

	printDeque(d);


	// d2  10 40 30 20
	if (d2.empty())
	{
		cout << "为空" << endl;
	}
	else
	{
		cout << "不为空 大小为:" <<d2.size() << endl;
	}
}

/*
deque双端插入和删除操作
push_back(elem);//在容器尾部添加一个数据
push_front(elem);//在容器头部插入一个数据
pop_back();//删除容器最后一个数据
pop_front();//删除容器第一个数据

3.3.3.5 deque数据存取
at(idx);//返回索引idx所指的数据,如果idx越界,抛出out_of_range。
operator[];//返回索引idx所指的数据,如果idx越界,不抛出异常,直接出错。
front();//返回第一个数据。
back();//返回最后一个数据
3.3.3.6 deque插入操作
insert(pos,elem);//在pos位置插入一个elem元素的拷贝,返回新数据的位置。
insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
3.3.3.7 deque删除操作
clear();//移除容器的所有数据
erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
erase(pos);//删除pos位置的数据,返回下一个数据的位置。

*/

void test02()
{
	deque<int>d;
	d.push_back(10);
	d.push_back(30);
	d.push_back(20);
	d.push_front(100);
	d.push_front(200);

	printDeque(d); // 200 100 10 30 20

	//删除 头删 尾删
	d.pop_back();
	d.pop_front();
	printDeque(d); // 100 10 30

	cout << "front: " << d.front() << endl;
	cout << "back: " << d.back() << endl;

	//插入
	deque<int>d2;
	d2.push_back(50);
	d2.push_back(60);
	d2.insert(d2.begin(), d.begin(), d.end());
	printDeque(d2);  //  100 10 30 50 60


}

//排序规则
bool myCompare(int v1, int v2)
{
	return v1 > v2; // 100 10
}

//排序 sort
void test03()
{
	deque<int>d;
	d.push_back(5);
	d.push_back(15);
	d.push_back(3);
	d.push_back(40);
	d.push_back(7);

	printDeque(d);
	//排序
	sort(d.begin(), d.end());

	printDeque(d);

	//从大到小
	sort(d.begin(), d.end(), myCompare);
	printDeque(d);

}

int main(){

	//test01();

	//test02();

	test03();

	system("pause");
	return EXIT_SUCCESS;
}

 

运行结果 实验三/003.cpp: In function ‘int main()’: 实验三/003.cpp:64:42: error: no matching function for call to ‘std::queue<std::tuple<int, int, int, int> >::push(<brace-enclosed initializer list>)’ q.push({sy, sx, startColor, startDir}); ^ In file included from /usr/local/include/c++/7.3.0/queue:64:0, from 实验三/003.cpp:2: /usr/local/include/c++/7.3.0/bits/stl_queue.h:251:7: note: candidate: void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >; std::queue<_Tp, _Sequence>::value_type = std::tuple<int, int, int, int>] push(const value_type& __x) ^~~~ /usr/local/include/c++/7.3.0/bits/stl_queue.h:251:7: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const value_type& {aka const std::tuple<int, int, int, int>&}’ /usr/local/include/c++/7.3.0/bits/stl_queue.h:256:7: note: candidate: void std::queue<_Tp, _Sequence>::push(std::queue<_Tp, _Sequence>::value_type&&) [with _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >; std::queue<_Tp, _Sequence>::value_type = std::tuple<int, int, int, int>] push(value_type&& __x) ^~~~ /usr/local/include/c++/7.3.0/bits/stl_queue.h:256:7: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::queue<std::tuple<int, int, int, int> >::value_type&& {aka std::tuple<int, int, int, int>&&}’ 实验三/003.cpp:68:14: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z auto [y, x, color, dir] = q.front(); ^ 实验三/003.cpp:68:14: error: ‘std::tuple<int, int, int, int> <anonymous>’ has incomplete type auto [y, x, color, dir] = q.front(); ^~~~~~~~~~~~~~~~~~ 实验三/003.cpp:82:41: error: no matching function for call to ‘std::queue<std::tuple<int, int, int, int> >::push(<brace-enclosed initializer list>)’ q.push({y, x, color, newDir}); ^ In file included from /usr/local/include/c++/7.3.0/queue:64:0, from 实验三/003.cpp:2: /usr/local/include/c++/7.3.0/bits/stl_queue.h:251:7: note: candidate: void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >; std::queue<_Tp, _Sequence>::value_type = std::tuple<int, int, int, int>] push(const value_type& __x) ^~~~ /usr/local/include/c++/7.3.0/bits/stl_queue.h:251:7: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const value_type& {aka const std::tuple<int, int, int, int>&}’ /usr/local/include/c++/7.3.0/bits/stl_queue.h:256:7: note: candidate: void std::queue<_Tp, _Sequence>::push(std::queue<_Tp, _Sequence>::value_type&&) [with _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >; std::queue<_Tp, _Sequence>::value_type = std::tuple<int, int, int, int>] push(value_type&& __x) ^~~~ /usr/local/include/c++/7.3.0/bits/stl_queue.h:256:7: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::queue<std::tuple<int, int, int, int> >::value_type&& {aka std::tuple<int, int, int, int>&&}’ 实验三/003.cpp:89:41: error: no matching function for call to ‘std::queue<std::tuple<int, int, int, int> >::push(<brace-enclosed initializer list>)’ q.push({y, x, color, newDir}); ^ In file included from /usr/local/include/c++/7.3.0/queue:64:0, from 实验三/003.cpp:2: /usr/local/include/c++/7.3.0/bits/stl_queue.h:251:7: note: candidate: void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >; std::queue<_Tp, _Sequence>::value_type = std::tuple<int, int, int, int>] push(const value_type& __x) ^~~~ /usr/local/include/c++/7.3.0/bits/stl_queue.h:251:7: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const value_type& {aka const std::tuple<int, int, int, int>&}’ /usr/local/include/c++/7.3.0/bits/stl_queue.h:256:7: note: candidate: void std::queue<_Tp, _Sequence>::push(std::queue<_Tp, _Sequence>::value_type&&) [with _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >; std::queue<_Tp, _Sequence>::value_type = std::tuple<int, int, int, int>] push(value_type&& __x) ^~~~ /usr/local/include/c++/7.3.0/bits/stl_queue.h:256:7: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::queue<std::tuple<int, int, int, int> >::value_type&& {aka std::tuple<int, int, int, int>&&}’ 实验三/003.cpp:99:47: error: no matching function for call to ‘std::queue<std::tuple<int, int, int, int> >::push(<brace-enclosed initializer list>)’ q.push({ny, nx, newColor, dir}); ^ In file included from /usr/local/include/c++/7.3.0/queue:64:0, from 实验三/003.cpp:2: /usr/local/include/c++/7.3.0/bits/stl_queue.h:251:7: note: candidate: void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >; std::queue<_Tp, _Sequence>::value_type = std::tuple<int, int, int, int>] push(const value_type& __x) ^~~~ /usr/local/include/c++/7.3.0/bits/stl_queue.h:251:7: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const value_type& {aka const std::tuple<int, int, int, int>&}’ /usr/local/include/c++/7.3.0/bits/stl_queue.h:256:7: note: candidate: void std::queue<_Tp, _Sequence>::push(std::queue<_Tp, _Sequence>::value_type&&) [with _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >; std::queue<_Tp, _Sequence>::value_type = std::tuple<int, int, int, int>] push(value_type&& __x) ^~~~ /usr/local/include/c++/7.3.0/bits/stl_queue.h:256:7: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::queue<std::tuple<int, int, int, int> >::value_type&& {aka std::tuple<int, int, int, int>&&}’ In file included from /usr/local/include/c++/7.3.0/deque:64:0, from /usr/local/include/c++/7.3.0/queue:60, from 实验三/003.cpp:2: /usr/local/include/c++/7.3.0/bits/stl_deque.h: In instantiation of ‘void std::deque<_Tp, _Alloc>::_M_destroy_data(std::deque<_Tp, _Alloc>::iterator, std::deque<_Tp, _Alloc>::iterator, const std::allocator<_CharT>&) [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >; std::deque<_Tp, _Alloc>::iterator = std::_Deque_iterator<std::tuple<int, int, int, int>, std::tuple<int, int, int, int>&, std::tuple<int, int, int, int>*>]’: /usr/local/include/c++/7.3.0/bits/stl_deque.h:1045:24: required from ‘std::deque<_Tp, _Alloc>::~deque() [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >]’ /usr/local/include/c++/7.3.0/bits/stl_queue.h:96:11: required from here /usr/local/include/c++/7.3.0/bits/stl_deque.h:2071:6: error: invalid use of incomplete type ‘std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >::value_type {aka class std::tuple<int, int, int, int>}’ if (!__has_trivial_destructor(value_type)) In file included from /usr/local/include/c++/7.3.0/bits/move.h:54:0, from /usr/local/include/c++/7.3.0/bits/nested_exception.h:40, from /usr/local/include/c++/7.3.0/exception:143, from /usr/local/include/c++/7.3.0/ios:39, from /usr/local/include/c++/7.3.0/ostream:38, from /usr/local/include/c++/7.3.0/iostream:39, from 实验三/003.cpp:1: /usr/local/include/c++/7.3.0/type_traits:2555:11: note: declaration of ‘std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >::value_type {aka class std::tuple<int, int, int, int>}’ class tuple; ^~~~~ In file included from /usr/local/include/c++/7.3.0/deque:64:0, from /usr/local/include/c++/7.3.0/queue:60, from 实验三/003.cpp:2: /usr/local/include/c++/7.3.0/bits/stl_deque.h: In instantiation of ‘void std::deque<_Tp, _Alloc>::pop_front() [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >]’: /usr/local/include/c++/7.3.0/bits/stl_queue.h:287:2: required from ‘void std::queue<_Tp, _Sequence>::pop() [with _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >]’ 实验三/003.cpp:69:15: required from here /usr/local/include/c++/7.3.0/bits/stl_deque.h:1582:40: error: invalid use of incomplete type ‘class std::tuple<int, int, int, int>’ != this->_M_impl._M_start._M_last - 1) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~ In file included from /usr/local/include/c++/7.3.0/bits/move.h:54:0, from /usr/local/include/c++/7.3.0/bits/nested_exception.h:40, from /usr/local/include/c++/7.3.0/exception:143, from /usr/local/include/c++/7.3.0/ios:39, from /usr/local/include/c++/7.3.0/ostream:38, from /usr/local/include/c++/7.3.0/iostream:39, from 实验三/003.cpp:1: /usr/local/include/c++/7.3.0/type_traits:2555:11: note: declaration of ‘class std::tuple<int, int, int, int>’ class tuple; ^~~~~ In file included from /usr/local/include/c++/7.3.0/deque:64:0, from /usr/local/include/c++/7.3.0/queue:60, from 实验三/003.cpp:2: /usr/local/include/c++/7.3.0/bits/stl_deque.h:1586:6: error: cannot increment a pointer to incomplete type ‘std::tuple<int, int, int, int>’ ++this->_M_impl._M_start._M_cur; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /usr/local/include/c++/7.3.0/bits/stl_deque.h: In instantiation of ‘void std::_Deque_base<_Tp, _Alloc>::_M_initialize_map(std::size_t) [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >; std::size_t = long unsigned int]’: /usr/local/include/c++/7.3.0/bits/stl_deque.h:492:26: required from ‘std::_Deque_base<_Tp, _Alloc>::_Deque_base() [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >]’ /usr/local/include/c++/7.3.0/bits/stl_deque.h:888:23: required from ‘std::deque<_Tp, _Alloc>::deque() [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >]’ /usr/local/include/c++/7.3.0/bits/stl_queue.h:153:6: required from ‘std::queue<_Tp, _Sequence>::queue() [with _Seq = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >; _Requires = void; _Tp = std::tuple<int, int, int, int>; _Sequence = std::deque<std::tuple<int, int, int, int>, std::allocator<std::tuple<int, int, int, int> > >]’ 实验三/003.cpp:60:38: required from here /usr/local/include/c++/7.3.0/bits/stl_deque.h:684:74: error: invalid application of ‘sizeof’ to incomplete type ‘std::tuple<int, int, int, int>’ const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp)) ^ /usr/local/include/c++/7.3.0/bits/stl_deque.h:715:31: error: invalid application of ‘sizeof’ to incomplete type ‘std::tuple<int, int, int, int>’ % __deque_buf_size(sizeof(_Tp))); ^ /usr/local/include/c++/7.3.0/bits/stl_deque.h: In instantiation of ‘void std::_Deque_base<_Tp, _Alloc>::_M_deallocate_node(std::_Deque_base<_Tp, _Alloc>::_Ptr) [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >; std::_Deque_base<_Tp, _Alloc>::_Ptr = std::tuple<int, int, int, int>*]’: /usr/local/include/c++/7.3.0/bits/stl_deque.h:743:20: required from ‘void std::_Deque_base<_Tp, _Alloc>::_M_destroy_nodes(std::_Deque_base<_Tp, _Alloc>::_Map_pointer, std::_Deque_base<_Tp, _Alloc>::_Map_pointer) [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >; std::_Deque_base<_Tp, _Alloc>::_Map_pointer = std::tuple<int, int, int, int>**]’ /usr/local/include/c++/7.3.0/bits/stl_deque.h:665:20: required from ‘std::_Deque_base<_Tp, _Alloc>::~_Deque_base() [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >]’ /usr/local/include/c++/7.3.0/bits/stl_deque.h:1045:65: required from ‘std::deque<_Tp, _Alloc>::~deque() [with _Tp = std::tuple<int, int, int, int>; _Alloc = std::allocator<std::tuple<int, int, int, int> >]’ /usr/local/include/c++/7.3.0/bits/stl_queue.h:96:11: required from here /usr/local/include/c++/7.3.0/bits/stl_deque.h:609:59: error: invalid application of ‘sizeof’ to incomplete type ‘std::tuple<int, int, int, int>’ _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp)));
最新发布
10-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值