Overview
The standard library provides double ended array with random access deque.
Details
deque are basically resizable, double ended, sequence of elements with random access. New elements can be added at both the ends and inserted in the middle. Note that unlike vector, deque does not offer contiguous sequence of elements.
Syntax
The syntax is as below. Template parameter T represents the datatype to store and Alloc represents the allocator used for storage.
template < class T, class Alloc = allocator<T> >class deque;
Members
It defines following types
Name | Description |
---|---|
value_type | The first template parameter (T) |
allocator_type | The second template parameter (Alloc) |
reference | value_type& |
const_reference | const value_type& |
pointer | allocator_traits<allocator_type>::pointer |
const_pointer | allocator_traits<allocator_type>::const_pointer |
iterator | a random access iterator to value_type convertible to const_iterator |
const_iterator | a random access iterator to const value_type |
reverse_iterator | reverse_iterator<iterator> |
const_reverse_iterator | reverse_iterator<const_iterator> |
difference_type | a signed integral type |
size_type | an unsigned integral type |
Operation
deque can be graphically represented as below.
Elements can be added at both the ends and can also be accessed randomly.
Complexity
The complexity of random access operation is O(1). Insertion or removal at the front and end is O(1). Insertion or removal at the middle is O(n) size().
Functionality
Constructors
In following constructors use default allocator. Custom allocators can be used by passing them as an additional argument.
Name | Description |
---|---|
deque () | Default Constructor. Example: //v:{} deque<int> v; |
deque (size_type n) | Constructs a container with n elements initialized with T(). Example: //v:{0,0,0,0,0} deque<int> v(5); |
deque (size_type n, const value_type& val) | Constructs a container with n elements initialized with with val. Example: //v:{10,10,10,10,10}
deque<int> v(5,10); |
deque (InputIterator first, InputIterator last) | Constructs a container and copies the elements in the range. Example: int a[5]{1,2,3,4,5};
|
deque (const deque & x) | copy constructor. |
deque (deque && x) | move constructor. |
deque (initializer_list<value_type> il) | initializer_list constructor. Example: //v:{1,2,3,4,5} deque<int> v{1,2,3,4,5}; |
Iterator
Name | Description |
---|---|
iterator begin() iterator end() | Returns iterator to beginning and end. Example: deque<int> v{1,2,3,4,5}; //prints 1 2 3 4 5 for (auto itr=v.begin(); itr!=v.end(); ++itr) cout << *itr << ' '; |
reverse_iterator rbegin() reverse_iterator rend() | Returns reverse_iterator to reverse beginning and reverse end. Example: deque<int> v{1,2,3,4,5}; //prints 5 4 3 2 1 for (auto itr=v.rbegin(); itr!=v.rend(); ++itr) cout << *itr << ' '; |
const_iterator cbegin const_iterator cend() | Returns const_iterator to beginning and end. Example: deque<int> v{1,2,3,4,5}; //prints 1 2 3 4 5 for (auto itr=v.cbegin(); itr!=v.cend(); ++itr) cout << *itr << ' '; |
const_reverse_iterator crbegin() const_reverse_iterator crend() | Returns const_reverse_iterator to reverse beginning and reverse end. Example: deque<int> v{1,2,3,4,5}; //prints 5 4 3 2 1 for (auto itr=v.crbegin(); itr!=v.crend(); ++itr) cout << *itr << ' '; |
Capacity
Name | Description |
---|---|
size_type size() | Returns the number of elements. Example: deque<int> v{1,2,3,4,5}; //prints 5 cout << v.size(); |
size_type max_size() | Returns maximum possible number of elements possible. A very large number. |
|
|
| |
bool empty() | Tests whether size is 0. |
void shrink_to_fit() | Reduces memory usage by freeing unused memory. |
Example: template<class Tp> struct NAlloc { typedef Tp value_type; int MemAllocated=0; NAlloc() = default; template<class T> NAlloc(const NAlloc<T>&) {} Tp* allocate(std::size_t n) { n *= sizeof(Tp); MemAllocated+=n; |
Element Access
Name | Description |
---|---|
reference operator[](size_type n) | Returns reference to the element at position n. No bounds check is done. Example: deque<int> v{1,2,3,4,5}; |
reference at(size_type n) | Returns reference to the element at position n. Exception is thrown for invalid input. Example: deque<int> v{1,2,3,4,5}; |
reference front() | Returns reference to the first element. Exception is thrown for invalid input.Example:deque<int> v{1,2,3,4,5}; |
reference back() | Returns reference to the last element. Exception is thrown for invalid input.Example:vector<int> v{1,2,3,4,5}; |
Modifiers
Name | Description |
---|---|
|
|
Example:int a[]{1,2,3,4,5}; deque<int> v{};v.assign(begin(a)+2,end(a)); | |
void push_back(const value_type& val) | Adds element at the end.Example:deque<int> v{};v.push_back(1); |
void pop_back() | Delete the last element.Example:vector<int> v{1}; //v:{} v.pop_back(); |
void push_front(const value_type& val) | Adds element at the end.Example:deque<int> v{};v.push_front(1); |
void pop_front() | Delete the last element.Example:deque<int> v{1}; //v:{} v.pop_front(); |
|
|
Example:int a[]{1,2,3,4,5}; deque<int> v{}; deque<int>::iterator itr; //(1) //v:{1,2,3,4,5} //itr = begin(v) itr = v.insert(cbegin(v),begin(a),end(a)); //(2) //v:{1,2,3,4,5,6} //itr = begin(v)+5 itr = v.insert(cend(v),1,6); //(3) //v:{1,2,3,4,5,6,7} //itr = begin(v)+6 itr = v.insert(cend(v),7); //(4) //v:{1,2,3,4,5,6,7,8} //itr = begin(v)+7 itr = v.insert(cend(v),move(8)); //(5) //v:{1,2,3,4,5,6,7,8,9,10} //itr = begin(v)+8 itr = v.insert(cend(v),{9,10}); | |
|
|
Example:deque<int> v{1,2,3,4,5};deque<int>::iterator itr; | |
void swap(deque & v) | Swap content with v. Note T has to be same. Example: deque<int> v{1,2,3,4,5}; deque<int> v2{5,4,3,2,1};
|
void clear() | Clears the contents. Example: deque<int> v{1,2,3,4,5};
|
iterator emplace(const_iterator pos, Args ...arg) | Construct and insert element in place using arg. Returns an iterator to the first element inserted. Example: deque<int> v{1,2,3,5};
|
void emplace_front(Args ...arg) | Construct and insert element at the end using arg. Example: vector<int> v{2,3,4,5};
|
void emplace_back(Args ...arg) | Construct and insert element at the end using arg. Example: vector<int> v{1,2,3,4};
|
No comments:
Post a Comment