#include <iostream>
#include <deque>
#include <vector>
using namespace std;
int main(){
//deque表示的是双端队列,删除和添加元素都在双端进行,具有队列和栈的性质,通常的队列只能在一段进行添加,另一端进行删除
//虽然可以进行erase操作,但是性能下降会比较明显;
deque<int> first;
deque<int> second;
deque<int> third;
first.assign(7,100);
deque<int>::iterator It;
It = first.begin()+1;
for(auto print:first){
cout << print << " ";
}
cout << endl;
second.assign(It,first.end()-1);
int mytins[]={11,22,4,23};
third.assign(mytins,mytins+3);
cout << "Size of first: " << int(first.size()) << endl;
cout << "Size of second: " << int(second.size()) << endl;
cout << "Size of second: " << int(third.size()) << endl;
//at操作:
deque<int> mydeque(10);
for(size_t i=0;i<mydeque.size();++i)
mydeque.at(i)=i;
for(auto print:mydeque)
cout << print << " ";
cout << endl;
//.back():用于返回当前队列的最后一个元素
cout << mydeque.back() << endl;
//.begin() .end() 是两个迭代器
for(auto It = mydeque.begin();It!=mydeque.end();++It)
cout << *It << " ";
cout << endl;
//.cbegin() .cend() 来进行反向的迭代访问
for(auto It=mydeque.cbegin();It!=mydeque.cend();++It)
cout << *It << " ";
cout << endl;
//.clear() 用销毁当前队列里面的元素;
mydeque.clear();
mydeque.push_back(10);
mydeque.push_back(1);
mydeque.push_front(2);
for(auto It=mydeque.begin();It!=mydeque.end();++It)
cout << *It << " ";
cout << endl;
//.emplace(),通过在位置插入新元素来扩展容器,双端队列可以高效的在开头和结尾插入或者删除元素,如果在其他位置插入或
//者删除元素,那么效率就会降低;
deque<int> mydeque1={10,20,30};
auto It1 = mydeque1.emplace(mydeque1.begin()+1,100);
mydeque1.emplace(It1,200);
mydeque1.emplace(mydeque1.end(),100);
mydeque1.emplace_back(1);
mydeque1.emplace_front(10);
cout << "mydeque1 contains: ";
for(auto print:mydeque1)
cout << print << " ";
cout << endl;
//.empty() 用来判断是否为空的操作,返回值是一个bool值
cout << boolalpha << mydeque1.empty() << noboolalpha << endl;
//.erase() 支持两种操作,一种是按照位置进行删除,另一种是按照范围进行删除;
deque<int> newdeque;
for(size_t i=0;i<20;++i)
newdeque.push_front(i);
newdeque.erase(newdeque.begin()+2);
newdeque.erase(newdeque.begin()+4,newdeque.begin()+7);
for(auto print:newdeque)
cout << print << " ";
cout << endl;
//.front()用于返回队列的第一个元素
mydeque1.push_front(20);
cout << mydeque1.front() << endl;
//.get_allocator用于返回一个拷贝的deque关联的allocator对象
deque<int> mydeque2;
int *p;
unsigned int i=0;
p = mydeque2.get_allocator().allocate(5);
for(i=0;i<5;i++)
mydeque2.get_allocator().construct(&p[i],i);
cout << "The allocated contains: ";
for(i=0;i<5;i++)
cout << p[i] << " ";
for(i=0;i<5;i++)
mydeque2.get_allocator().destroy(&p[i]);
mydeque2.get_allocator().deallocate(p,5);
//.inerst() 在某个位置精确的插入一些元素,虽然不建议这样做,但是是可以实现的;
//下面的几种方式都是insert支持的重载函数:
auto it = mydeque2.begin();
mydeque2.insert(it+2,10);
mydeque2.insert(it+2,3,1);
vector<int> myvector(3,13);
mydeque2.insert(mydeque2.begin()+2,myvector.begin(),myvector.end());
for(auto It=mydeque2.begin();It!=mydeque2.end();++It)
cout << *It << " ";
cout << endl;
//.max_size():当前容器所能够存放的最大值;
cout << mydeque2.max_size() << endl;
//operator=
deque<int> newfirst(3);
deque<int> newsecond(5);
newsecond = newfirst;
newfirst = deque<int>();
cout << "Size of newfirst: " << int(newfirst.size()) << endl;
cout << "Size of newsecond: " << int(newsecond.size()) << endl;
//.[]同样说可以使用[]来访问下标元素的;
//.pop_back() 用于队列的最后一个元素
mydeque1.pop_back();
//.pop_front()用于删除开始的第一个元素
mydeque2.pop_front();
//.push_back()用于在最后一个添加元素;
mydeque2.push_back(10);
//.push_front()用于在开始位置添加元素;
mydeque2.push_front(20);
//.rbegin()和rend()用来进行反向遍历;
for(auto It=mydeque1.rbegin();It!=mydeque1.rend();++It)
cout << *It << " ";
cout << endl;
//.resize() 用于更改size的大小,三种情况和vector类似;
//.shrink_to_fit()和vector的是一样的;
//.size()用于显示当前队列的大小;
mydeque2.size();
//.swap()也可以用来交换两个相同大小的双向队列;
mydeque1.swap(mydeque2);
return 0;
}
deque支持的常见的操作
最新推荐文章于 2024-06-05 09:00:00 发布