deque<T>

Like the vector, deque is a linear container supporting random-access that looks very much like a dynamically-managed array. deque is an excellent choice when working with random-access lists where insertions are common at the beginning and end of the container.

Another linear container class is the list, which represents a list of elements that does not support random access.

Common deque<T> Member Functions
Constructor: deque<T> ()
Constructs a new empty deque.
deque<int> myDeque; // Constructs a new, empty deque.

Constructs a new deque. This is the default constructor.

Constructor: deque<T> (size_type numElems)
Constructor: deque<T> (size_type numElems, const T &elem)
Constructs a pre-initialized deque
deque<int> myDeque(10, 137); // Constructs a deque with 10 elements, all 137
deque<string> myDeque(10); // Constructs a deque with 10 elements, all the empty string

Constructs a new deque with the specified number of copies of the specified element. With a single parameter, this constructor creates a deque containing the specified number of elements, all initialized to the default. With two parameters, this constructor creates a deque of the specified number of copies of the specified element.

T& operator [] (size_type index)
const T& operator [] (size_type index) const
T& at (size_type index)
const T& at (size_type index) const
Access deque elements.
myDeque[10] = 137; // Element at position 10 is now 137.

Returns a reference to the stored element at the specified position. The operator[] function will not bounds-check and has undefined behavior when accessing beyond the bounds of the deque. The at() function will throw an out_of_range exception when reading beyond the ends of the array.

size_type size() const
Get container size
for(int i = 0; i < myDeque.size(); ++i) { ... }

Returns the number of elements stored in the container.

bool empty() const
Is container empty?
if(myDeque.empty()) { ... }

Returns whether the deque is empty. It's considered good practice to call empty to determine whether the deque is empty rather than checking whether size() returns zero.

void clear()
Empty container
myDeque.clear();

Removes all elements from the deque.

void resize(size_type size);
void resize(size_type size, const T& fill);
Resize deque
myDeque.resize(137); // Resizes the deque to 137 elements.
myDeque.resize(137, 42); // Resizes the deque to 137 elements, filling new entries with 137

Resizes the deque by adding or removing elements from the end of the deque. The first version resizes the deque to the specified size, trimming elements from the end if the new size is smaller than the old size and padding the end with new objects if the new size is greater than the old size. The newly-added elements are constructed using the default constructor.

The second version of the function works identically to the first, except that any newly-added elements are initialized to have the same value as the parameter.

iterator begin()
const_iterator begin() const
Get start iterator.
for(deque<int>::iterator itr = myDeque.begin(); /* ... */)

Returns an iterator to the first element in the deque.

iterator end()
const_iterator end() const
Get stop iterator.
for(/* ... */ itr != myDeque.end(); /* ... */)

Returns an iterator to the element one past the end of the deque. This iterator does not point to a valid element, so do not dereference it.

void push_back(const T& elem);
Append element
myDeque.push_back(137); // Appends 137 to the end of the deque.

Appends the specified element to the back of the deque, increasing the size by one.

void pop_back()
Delete last element
myDeque.pop_back();

Removes the last element in the deque. pop_back does not return a value, so if you want to get the value, call back first. pop_back does not do any bounds-checking, so make sure the deque is nonempty before calling.

T& back()
const T& back() const
Get last element
int lastElem = myDeque.back();

Returns a reference to the last element of the deque. There is no bounds-checking, so make sure the deque is nonempty before calling.

void push_front(const T& elem);
Prepend element
myDeque.push_front(137); // Prepends 137 to the front of the deque.

Prepends the specified element to the front of the deque, increasing the size by one.

void pop_front()
Delete first element
myDeque.pop_front();

Removes the first element in the deque. pop_front does not return a value, so if you want to get the value, call front first. pop_front does not do any bounds-checking, so make sure the deque is nonempty before calling.

T& front()
const T& front() const
Get first element
int firstElem = myDeque.front();

Returns a reference to the first element of the deque. There is no bounds-checking, so make sure the deque is nonempty before calling.

iterator insert(iterator position, const T& elem)
iterator insert(iterator position, size_type numCopies, const T&elem)
iterator insert(iterator position, InputIterator begin, InputIterator end)
Insert elements
myDeque.insert(myDeque.begin() + 3, 137); // Insert 137 after the third element
myDeque.insert(myDeque.begin() + 3, 42, 137); // Insert 42 copies of 137 after the third element
myDeque.insert(myDeque.begin(), otherContainer.begin(), otherContainer.end()); // Insert elements from another container

The first version inserts the specified element at the position indicated by the specified iterator. There is no bounds-checking. The size of the container will grow by one and elements will be shifted down.

The second version inserts the specified number of copies of the specified element at the specified position.

The third version inserts the elements in the specified iterator range into the deque at the specified position. The source iterators can be of any type.

iterator erase(iterator position)
iterator erase(iterator start, iterator stop)
Remove elements
myDeque.erase(myDeque.begin() + 7); // Erase seventh element.
myDeque.erase(myDeque.begin(), myDeque.end() - 4); // Erase all but last four elements

Removes elements from the deque without bounds-checking. The first version removes the element pointed at by the iterator, and the second removes all elements in the specified range.

Other deque<T> Member Functions
reverse_iterator begin()
const_reverse_iterator begin() const
Get start reverse iterator
for(deque<int>::reverse_iterator itr = myDeque.rbegin(); /* ... */)

Returns a reverse_iterator to the last element of the deque. reverse_iterators are like regular iterators except that they traverse in the opposite direction. Check for the end-of-container condition of reverse_iterators with rend.

reverse_iterator end()
const_reverse_iterator end() const
Get stop reverse iterator.
for(/* ... */ itr != myDeque.rend(); /* ... */)

Returns a reverse_iterator to the element one element before the start of the array. Like end, the iterator returned by rend does not actually reference a real element of the deque and you should take care not to actually dereference it.

size_type max_size() const
Get maximum container size
deque<int>::size_type maxElems = myDeque.max_size()

Returns the maximum number of elements the deque can hold, which is usually the number of unique memory addresses divided by the size of the elements stored in the container.

void swap(deque<T> &other)
Exchange two deques
myDeque.swap(myOtherdeque); // Exchanges the two deques

Exchanges the contents of this deque and another deque.

void assign(size_type numCopies, const T& elem)
void assign(InputIterator start, InputIterator end)
Reset deque content.
myDeque.assign(5, 137); // Reset content to five copies of the number 137
myDeque.assign(otherContainer.begin(), otherContainer.end()); // Copy other container into this deque.

Erases any stored content and replaces it with either the specified number of repeats of the specified elements or the elements contained in the range [start, end). In either case, the original deque contents are deleted.