1, the basic concept
Function: double-ended array , can be inserted into the head end of the deletion
deque and vector difference:
vector for the low efficiency for the head insertion and deletion , the greater the amount of data, the less efficient the
deque contrast, insertion and deletion of vector faster than the speed of the head
will be faster than the access speed deque element vector, both of which inner and implemented related
Iterator deque container also supports random access
2, constructors
The default constructor form
deque<T> deqT
Constructor [beg, end) section of the copy to the elements themselves.
deque(beg,end)
The constructor of n elem copy to itself.
deque(n,elem)
Copy constructor
deque(const deque& deq)
If you want to limit the data is read-only state, then the state must also change the iterator
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>&d)
{
for(deque<int>::const_iterator it=d.begin();it!=d.end();it++)
{
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(),d1.end()) ;
printDeque(d2);
//第三种
deque<int>d3(10,100) ;
printDeque(d3);
//第四种
deque<int>d4(d3);
printDeque(d4);
}
int main()
{
test01();
}
deque and vector container configured almost the same manner
3, assignment
Equality operator overloading
vector& operator=(const vector&vec)
The [beg, end) copy of the data interval is assigned to itself
assign(beg,end)
The n copies assigned to itself elem
assign(n,elem);
code show as below
#include<iostream>
using namespace std;
#include<deque>
void printDeque(const deque<int>&d)
{
for(deque<int>::const_iterator it=d.begin();it!=d.end();it++)
{
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;
d2=d1;
printDeque(d2);
//第二种
deque<int>d3;
d3.assign(d1.begin(),d1.end()) ;
printDeque(d3);
//第三种
deque<int>d4;
d4.assign(10,100);
printDeque(d4);
}
int main()
{
test01();
}
4, the capacity, the size of the operation
For determining the size and capacity of the container deque
Determining whether the container is empty
empty()
Returns the number of elements of the container
size()
The length of the container is re-designated num
resize(int num)
If the container becomes long, filled with new values in a default position. If the container becomes short, the elements of the container beyond the end of the length are deleted.
The length of the container is re-designated num
resize(int num,elem);
If the container becomes longer, the value to fill the new position places elem. If the container becomes short, the elements of the container does not exceed the length of the tail is removed
deque no capacity concept, but the size of the concept
because it is an array of double-ended
code show as below
#include<iostream>
using namespace std;
#include<deque>
void printDeque(deque<int>&d)
{
for(deque<int>::iterator it=d.begin();it!=d.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
void test01()
{
deque<int>d1;
for(int i=0;i<10;i++)
{
d1.push_back(i);
}
printDeque(d1);
if(d1.empty())
{
cout<<"d1为空"<<endl;
}
else
{
cout<<"d1不为空"<<endl;
cout<<"d1的大小为:"<<d1.size()<<endl;
}
//重新指定大小
d1.resize(15,2);
printDeque(d1);
d1.resize(5);
printDeque(d1);
}
int main()
{
test01();
}
to sum up:
deque no capacity concept
Determine whether an empty ------ empty
The number of elements ----------- size Returns
Reassign the number ------------ resize
5, insert and delete
Deque to insert and delete data
Prototype:
Add a data container in the tail
push_back(elem);
Inserting a container header data
push_front(elem)
Delete the last data container
pop_back()
Delete the first container data
pop_front()
Specifies the position of the operation:
insert a copy of the position pos elem element, the return position of the new data
insert(pos,elem)
Inserts n elem data position pos, no return value.
insert(pos,n,elem)
在pos位置插入[beg.end)区间的数据,无返回值。
insert(pos,beg,end)
清空容器的所有数据
clear()
删除[beg.end)区间的数据,返回下一个数据的位置。
erase(beg,end)
删除pos位置的数据,返回下一个数据的位置。
erase(pos)
其中,pos都是迭代器
代码如下
#include<iostream>
using namespace std;
#include<deque>
void printDeque(deque<int>&d)
{
for(deque<int>::iterator it=d.begin();it!=d.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
//两端操作
void test01()
{
deque<int> d;
//尾插
d.push_back(10);
d.push_back(20);
//头插
d.push_front(100);
d.push_front(200);
//200 100 10 20
printDeque(d);
//尾删
d.pop_back();
//200 100 10
printDeque(d);
//头删
d.pop_front();
//100 10
printDeque(d);
}
void test02()
{
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_front(100);
d.push_front(200);
//200 100 10 20
printDeque(d);
d.insert(d.begin(),1000);
//1000,200 100 10 20
printDeque(d);
d.insert(d.begin(),2,1000);
//1000,1000,1000,200 100 10 20
printDeque(d);
//指定位置插入区间
deque<int> d2;
d2.push_back(1);
d2.push_back(2);
d2.push_front(10);
d2.push_front(20);
d.insert(d.begin(),d2.begin(),d2.end());
printDeque(d);
}
void test03()
{
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_front(100);
d.push_front(200);
//200 100 10 20
printDeque(d);
d.erase(d.begin());
//100 10 20
printDeque(d);
//删除指定项
deque<int>::iterator it=d.begin();
it++;
d.erase(it);
printDeque(d);
//清空
//d.clear();
d.erase(d.begin(),d.end());
printDeque(d);
}
int main()
{
//test01();
//test02();
test03();
}
6、数据存取
返回索引idx所指的数据
at(int idx)
返回索引idx所指的数据
operator[]
返回容器中第一个数据元素
front()
返回容器中最后一个数据元素
back()
代码如下
#include<iostream>
using namespace std;
#include<deque>
void printDeque(deque<int>&d)
{
for(deque<int>::iterator it=d.begin();it!=d.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
//两端操作
void test01()
{
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_front(100);
d.push_front(200);
printDeque(d);
for(int i=0;i<d.size();i++)
{
cout<<d[i]<<" ";
}
cout<<endl;
for(int i=0;i<d.size();i++)
{
cout<<d.at(i)<<" ";
}
cout<<endl;
cout<<"第一个元素为:"<<d.front()<<endl;
cout<<"最后一个元素为:"<<d.back()<<endl;
}
int main()
{
test01();
}
7、排序
对beg和end区间内元素进行排序
sort(iterator beg,iterator end)
代码如下
#include<iostream>
using namespace std;
#include<deque>
#include<algorithm>
void printDeque(deque<int>&d)
{
for(deque<int>::iterator it=d.begin();it!=d.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
//两端操作
void test01()
{
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_front(100);
d.push_front(200);
printDeque(d);
cout<<"排序后:"<<endl;
sort(d.begin(),d.end());
printDeque(d);
}
int main()
{
test01();
}
默认sort都是升序排序,也可以添加函数改为降序排序