C++ STL之deque学习记录
C++ STL之deque学习记录
头文件
#include<deque>
简介
参见http://www.cplusplus.com/reference/deque/deque/
deque (usually pronounced like "deck") is an irregular acronym of double-ended queue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back).
1. 随机访问,支持[]访问和迭代器访问,这点与vector相似,但是性能没有vector好
2. 可以进行插入和删除操作,这与list相似,但是性能没有list好
3. deque的元素在内存上并不连续,而是将其分成许多个连续的内存块,这与vector的连续内存以及list的分离内存都有所区别,因此就表现为以上的特点。
成员函数
构造函数(constructor):参见http://www.cplusplus.com/reference/deque/deque/deque/
1. 默认构造函数(defalult constructor)
deque():创建一个新的空双向对列,没有元素。
deque(size_type n):创建一个新的双向队列,大小为n
2. 填充构造函数(fill constructor)
deque(size_type n, const value_type &val):创建一个新的双向队列,大小为n,每一个元素都是val
例:deque<int> first(4, 100);//创建一个名为first的双向队列,大小为4,每个元素都是100
3. 范围构造函数(range constructor)
deque(iterator begin, iterator end): 创建一个新的双向队列,并且将迭代器begin和end中间的所有元素复制过来
例:deque<int> second(first.begin(), first.end());//创建了一个名为second的双向队列,并将first中从开始到结尾的元素复制给second
4. 拷贝构造函数(copy constructor)
denque(const deque& x): 用双向队列x构造一个相同的双向队列
迭代器(iterator)函数:
1. begin(), end(): iterator begin(); iterator end(); //return iterator to beginning(end);
2. rbegin(), rend(): iterator rbegin(); iterator rend(); //return iterator to reverse beginning(reverse end);
2. cbegin(), cend(), crbegin(), crend(): iterator cbegin(); iterator cend();iterator crbegin(); iterator crend(); //constant
容量(Capacity)函数:
1. size() : size_type size(); //return the size of current deque
2. max_size(): size_tyep max_size(); //return the maximum based on the system and implementation limitations
3. resize(): void resize(size_type n);
void resize(size_type n, const value_type &val);
解析:If n is smaller than the current container size, the content is reduced to its first n elements, removing
those beyond (and destroying them).If n is greater than the current container size, the content is expanded
by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements
are initialized as copies of val, otherwise, they are value-initialized.
4. empty(): bool empty(); //reutrn true if the deque is empty else return false
5. shrink_to_fit(): void shrink_to_fit(); //C++11 ,the deque allocates more memory than its size and it frees the memory that contain no element
访问函数(assess elements):
1. operator[]: value_type& operator[](size_type n); //return a reference of the element in index n which can be changed(assign)
const value_type& operator[](size_type n) const; //return a reference of the element in index n which can't be write
2. at: value_type& at(size_type n); //similar to the funciton operator[]
const value_type& at(size_type n) const;
3. front(), back(): value_type front(size_type n);
const value_type front(size_type n) const;
value_type back(size_type n);
const value_type back(size_type n) const;
修改函数(modifiers)
1. assign(): void assign(iterator begin, iterator end);
void assign(size_type n, value_type &val);
2. push_back(), push_front(): void push_back(const value_type& val); //push an element into the deque at back
void push_front(const value_type& val); //push an element into the deque at front
3. pop_back(), pop_front(): void pop_back(); //pop an element from the deque at back
void pop_front(); //pop an element from the deque at front
4. insert(): iterator insert(const iterator it, value_type &val); //insert a value before at specific position
iterator insert(const iterator it, size_type n, value_type &val); //insert n values before at specific posion
iteraotr insert(const iterator it, iterator begin, iterator end);
//about return value: reutrn an iterator that the newly insert element
5. erase(): iterator erase(const iterator position); //erase the element at specific position
iterator etase(cosnt iterator begin, iterator end); // erase the element at the range
//about the return value:n iterator pointing to the new location of the element that followed the last element erased by the function call.
6. swap(): void swap(deque& x); //swap the element of the two deques
7. clear(): void clear(); //clear the deque