The <deque> header is one of the standard container template headers. It declares the deque class template and a few global functions that operate on deque objects.
A deque, short for double-ended queue, is similar to a vector, but the performance is constant when adding to or removing from the collection at the beginning and at the end.
If you need a vector of bool that behaves as a normal C++ container, you should use deque<bool> instead of vector<bool>. See <vector> later in this chapter for an explanation.
See Chapter 10 for information about containers in general.
deque class template | Double-ended queue |
template <class T, class Alloc = allocator<T> > class deque { public: typedef typename Alloc::reference reference; typedef typename Alloc::const_reference const_reference; typedef . . . iterator; typedef . . . const_iterator; typedef . . . size_type; typedef . . . difference_type; typedef T value_type; typedef Alloc allocator_type; typedef typename Alloc::pointer pointer; typedef typename Alloc::const_pointer const_pointer; typedef std::reverse_iterator<iterator> reverse_iterator; typedef std::reverse_iterator<const_iterator> const_reverse_iterator; explicit deque(const Alloc& = Alloc( )); explicit deque(size_type n, const T& value = T( ), const Alloc& = Alloc( )); template <class InputIterator> deque(InputIterator first, InputIterator last, const Alloc& = Alloc( )); deque(const deque<T,Alloc>& x); ~deque( ); deque<T,Alloc>& operator=(const deque<T,Alloc>& x); template <class InputIterator> void assign(InputIterator first, InputIterator last); void assign(size_type n, const T& t); allocator_type get_allocator( ) const; iterator begin( ); const_iterator begin( ) const; iterator end( ); const_iterator end( ) const; reverse_iterator rbegin( ); const_reverse_iterator rbegin( ) const; reverse_iterator rend( ); const_reverse_iterator rend( ) const; size_type size( ) const; size_type max_size( ) const; void resize(size_type sz, T c = T( )); bool empty( ) const; reference operator[](size_type n); const_reference operator[](size_type n) const; reference at(size_type n); const_reference at(size_type n) const; reference front( ); const_reference front( ) const; reference back( ); const_reference back( ) const; void push_front(const T& x); void push_back(const T& x); iterator insert(iterator position, const T& x); void insert(iterator position, size_type n, const T& x); template <class InputIterator> void insert (iterator position, InputIterator first, InputIterator last); void pop_front( ); void pop_back( ); iterator erase(iterator position); iterator erase(iterator first, iterator last); void swap(deque<T,Alloc>&); void clear( ); }; |
The deque class template represents a double-ended queue. It is one of the standard container types, like list and vector. Like a list, a deque yields amortized, constant performance when adding and removing items from the beginning and end of the container. Like a vector, performance is constant when accessing items at any index in the deque. Performance for inserting or removing items not at the start or end is linear with respect to the size of the container.
After inserting items at the beginning or end of the deque, all iterators become invalid. All references and pointers to items in the deque remain valid. After inserting in the middle of the deque, all iterators, references, and pointers to items in the deque become invalid.
After erasing an element from the beginning or end of the deque, all iterators and references remain valid, except those pointing to the erased element. After erasing an element from the middle of the deque, all iterators, references, and pointers to items in the deque become invalid.
Constructs an empty deque.
Constructs a deque with n copies of value.
Constructs a deque with copies of the elements in [first, last), unless InputIterator is an integral type, in which case the deque is constructed as though the arguments were cast as follows:
deque(static_cast<size_type>(first), static_cast<value_type>(last), alloc);
Erases the current contents of the deque and inserts the elements in [first, last), unless InputIterator is an integral type, in which case the arguments are interpreted as though they were cast as follows:
assign(static_cast<size_type>(first), static_cast<value_type>(last));
Erases the current contents of the deque and inserts n copies of t.
Returns the allocator object.
Returns the element at index n. If n >= size( ), the behavior is undefined.
Returns the element at index n. If n >= size( ), it throws out_of_range.
Returns the last element in the deque. The behavior is undefined if the deque is empty.
Returns an iterator that points to the first element of the deque.
Erases all elements from the deque.
Returns size( ) == 0.
Returns an iterator that points to the last element of the deque.
Erases the element at position.
Erases all the elements in the range [first, last).
Returns the first element of the deque. The behavior is undefined if the deque is empty.
Inserts x at position. If position is begin( ) or end( ), the performance is constant; at any other position, the performance is linear.
Inserts n copies of x at pos.
Inserts the elements in the range [first, last) starting at position, unless InputIterator is an integral type, in which case the arguments are interpreted as though they were cast:
insert(position, static_cast<size_type>(first), static_cast<value_type>(last));
If an exception is thrown, such as bad_alloc when there is insufficient memory for a new element, the deque is unchanged, and all iterators and references remain valid. If the exception is thrown from an element's copy constructor or assignment operator, however, the behavior is unspecified.
Returns the size of the largest possible deque.
Erases the first element of the deque. The behavior is undefined if the deque is empty.
Erases the last element of the deque. The behavior is undefined if the deque is empty.
Inserts x as the new first element of the deque.
Inserts x as the new last element of the deque.
Returns a reverse iterator that points to the last element of the deque.
Returns a reverse iterator that points to one position before the first element of the deque.
Returns the number of elements in the deque.
Changes the size of the deque to n. If n > size( ), one or more copies of c are added to the end of the deque to reach the desired size. If the new size is smaller than the current size, the first n elements are unchanged, and elements are erased from the end to reach the new size.
Exchanges all the elements in the deque with all the elements in that.
<list>, <vector>
operator== function template | Compares two deques for equality |
template<typename T, typename A>
bool operator==(const deque<T,A>& x, const deque<T,A>& y)
|
The == operator returns true if x and y are the same size and their elements are equal, that is, x.size( ) == y.size( ) && equals(x.begin( ), x.end( ), y.begin( )).
equals in <algorithm>
operator!= function template | Compares two deques for inequality |
template<typename T, typename A>
bool operator!=(const deque<T,A>& x, const deque<T,A>& y)
|
The != operator is equivalent to ! (x == y).
operator< function template | Compares two deques for less-than |
template<typename T, typename A>
bool operator<(const deque<T,A>& x, const deque<T,A>& y)
|
The < operator determines whether x is less than y using the same algorithm as lexicographical_compare(x.begin( ), x.end( ), y.begin( ), y.end( )).
lexicographical_compare in <algorithm>
operator<= function template | Compares two deques for less-than-or-equal |
template<typename T, typename A>
bool operator<=(const deque<T,A>& x, const deque<T,A>& y)
|
The <= operator is equivalent to ! (y < x).
operator> function template | Compares two deques for greater-than |
template<typename T, typename A>
bool operator>(const deque<T,A>& x, const deque<T,A>& y)
|
The > operator is equivalent to (y < x).
operator>= function template | Compares two deques for greater-than-or-equal |
template<typename T, typename A>
bool operator>=(const deque<T,A>& x, const deque<T,A>& y)
|
The >= operator is equivalent to ! (x < y).
swap function template specialization | Swaps the contents of two deques |
template<typename T, typename Alloc>
void swap(deque<T, Alloc>& x, deque<T, Alloc>& y)
|
The swap function template specialization is equivalent to calling x.swap(y).
swap in <algorithm>