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:
empty | Test whether deque is empty |
size | Return size |
max_size | Return maximum size [test winxp 32bit vs2008 value is: 1073741823] |
resize | Change size |
Element access:
operator[] | Access element |
at | Access element |
front | Access first element |
back | Access last element |
Modifiers:
assign | Assign list content |
push_back | Add element at the end |
pop_back | Delete last element |
insert | Insert elements |
erase | Erase elements |
swap | Swap content//algorithm exists swap, and the same behavior. |
clear | Clear content |
push_front | Insert element at beginning→list and deque unique |
pop_front | Delete first element→list 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