Chapter 8.顺序容器deque

来源:互联网 发布:淘宝开服装店怎么进货 编辑:程序博客网 时间:2024/05/16 14:51
deque简介

deque被实现为双端队列,由多个连续内存块构成,deque是list和vector的兼容,分为多个块,每一个块大小是512字节,块通过map块管理,map块里保存每个块得首地址。因此该容器也有索引操作operator[ ],效率没vector高。,同样的也可以通过[]、at()来随机访问元素,所以在实现上面deque不保证在内存中以严格的连续地址存放,所以实现上面比vector要复杂的多

deque的优点
1.可以通过[]/at()来实现随机访问元素
2.可以通过迭代器以多种顺序来遍历各元素
3.可以高效的通过push_front()/pop_front/push_back()/pop_back()在其队头或队尾添加或移除元素
Capacity:
emptyTest whether deque is emptysizeReturn sizemax_sizeReturn maximum size [test winxp 32bit vs2008 value is: 1073741823]resizeChange size
Element access:
operator[]Access elementatAccess elementfrontAccess first elementbackAccess last element
Modifiers:
assignAssign list contentpush_backAdd element at the endpop_backDelete last elementinsertInsert elementseraseErase elementsswapSwap content//algorithm exists swap, and the same behavior.clearClear contentpush_frontInsert element at beginning→list and deque uniquepop_frontDelete first elementlist and deque unique
Allocator:
get_allocator
eg:  deque<int> mydeque;  int * p;  unsigned int i;  // allocate an array of 5 elements using deque's allocator:  p=mydeque.get_allocator().allocate(5);  // assign some values to array  for (i=0; i<5; i++) p[i]=i;  cout << "The allocated array contains:";  for (i=0; i<5; i++) cout << " " << p[i];  cout << endl;  mydeque.get_allocator().deallocate(p,5);
Output:
The allocated array contains: 0 1 2 3 4