C++ Library - <deque>


Advertisements

Introduction

Deque is acronym for Double Ended Queue. It is a sequence container that can change it's size runtime. Container is an object that holds data of same type. Sequence containers store elements strictly in linear sequence.

Elements of deque can be scattered in different chunks of memory. Container stores necessary information to allow direct access to any element in constant time. Unlike vectors, deque are not guaranteed to store all it's element at contiguous memory locations. Hence it does not allow direct access to data by offsetting pointers. But it enables direct access to any element using subscript operator [ ].

Deque can shrink or expand as needed from both ends at run time. The storage requirement is fulfilled automatically by internal allocator. Deque provides similar functionality as vectors, but provides efficient way to insert and delete data from any end.

Zero sized deques are also valid. In that case deque.begin() and deque.end() points to same location. But behavior of calling front() or back() is undefined.

Definition

Below is definition of std::deque from <deque> header file

template < class T, class Alloc = allocator<T> > class deque;

Parameters

  • T − Type of the element contained.

    T may be substituted by any other data type including user-defined type.

  • Alloc − Type of allocator object.

    By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.

Member types

Following member types can be used as parameters or return type by member functions.

Sr.No. Member types Definition
1 value_type T (First parameter of the template)
2 allocator_type Alloc (Second parameter of the template)
3 reference value_type&
4 const_reference const value_type&
5 pointer value_type*
6 const_pointer const value_type*
7 iterator a random access iterator to value_type
8 const_iterator a random access iterator to const value_type
9 reverse_iterator std::reverse_iterator <iterator>
10 const_reverse_iterator std::reverse_iterator <const_iterator>
11 size_type size_t
12 difference_type ptrdiff_t

Functions from <deque>

Below is list of all methods from <deque> header.

Constructors

Sr.No. Method & Description
1 deque::deque default constructor

Constructs an empty deque with zero element.

2 deque::deque fill constructor

construct a new deque with n elements and assign val to each element of deque

3 deque::deque range constructor

Constructs a deque with as many elements as in range of first to last.

4 deque::deque copy constructor

Constructs a deque with copy of each elements present in existing container.

5 deque::deque move constructor

Constructs a deque with the contents of other using move semantics.

6 deque::deque initializer list constructor

Constructs a deque from initialize list.

Destructor

Sr.No. Method & Description
1 deque::~deque

Destroys deque object by deallocating it's memory.

Member functions

Sr.No. Method & Description
1 deque::assign range version

Assign new values to the deque elements by replacing old ones.

2 deque::assign fill version

Assign new values to the deque elements by replacing old ones.

3 deque::assign initializer list version

Assign new values to the deque elements by replacing old ones.

4 deque::at

Returns reference to the element present at location n in the deque.

5 deque::back

Returns a reference to the last element of the deque.

6 deque::begin

Return a random access iterator pointing to the first element of the deque.

7 deque::cbegin

Returns a constant random access iterator which points to the beginning of the deque.

8 deque::cend

Returns a constant random access iterator which points to the beginning of the deque.

9 deque::clear

Destroys the deque by removing all elements from the deque and sets size of deque to zero.

10 deque::crbegin

Returns a constant reverse iterator which points to the reverser beginning of the container.

11 deque::crend

Returns a constant reverse iterator which points to the reverse end of the deque.

12 deque::emplace

Extends container by inserting new element at position.

13 deque::emplace_back

Inserts new element at the end of deque.

14 deque::emplace_front

Inserts new element at the beginning of deque.

15 deque::empty

Tests whether deque is empty or not.

16 deque::end

Returns an iterator which points to past-the-end element in the deque container.

17 deque::erase position version

Removes single element from the the deque.

18 deque::erase range version

Removes single element from the the deque.

19 deque::front

Returns a reference to the first element of the deque

20 deque::get_allocator

Returns an allocator associated with deque

21 deque::insert single element version

Extends container by inserting new element at position.

22 deque::insert fill version

Extends container by inserting new element in the container.

23 deque::insert range version

Extends container by inserting new element in the container.

24 deque::insert move version

Extends container by inserting new element in the container.

25 deque::insert initializer list version

Extends container by inserting new element in the container.

26 deque::max_size

Returns the maximum number of elements can be held by deque.

27 deque::operator= copy version

Assign new contents to the deque by replacing old ones and modifies size if necessary.

28 deque::operator= move version

Assign new contents to the deque by replacing old ones and modifies size if necessary.

29 deque::operator= initializer list version

Assign new contents to the deque by replacing old ones and modifies size if necessary.

30 deque::operator[]

Returns a reference to the element present at location n.

31 deque::pop_back

Removes last element from deque and reduces size of deque by one.

32 deque::pop_front

Removes first element from deque and reduces size of deque by one.

33 deque::push_back

Inserts new element at the end of deque and increases size of deque by one.

34 deque::push_back move version

Inserts new element at the end of deque and increases size of deque by one.

35 deque::push_front

Inserts new element at the front of deque and increases size of deque by one.

36 deque::push_front move version

Inserts new element at the front of deque and increases size of deque by one.

37 deque::rbegin

Returns a reverse iterator which points to the last element of the deque.

38 deque::rend

Returns a reverse iterator which points to the reverse end of the deque.

39 deque::resize

Changes the size of deque.

40 deque::resize value version

Changes the size of deque.

41 deque::shrink_to_fit

Requests the container to reduce it's capacity to fit its size.

42 deque::size

Returns the number of elements present in the deque.

43 deque::swap

Exchanges the content of deque with contents of another deque x.

Non-member overloaded functions

Sr.No. Method & Description
1 operator==

Tests whether two deques are equal or not.

2 operator!=

Tests whether two deques are equal or not.

3 operator<

Tests whether first deque is less than other or not.

4 operator<=

Tests whether first deque is less than or equal to other or not.

5 operator>

Tests whether first deque is greater than other or not.

6 operator>=

Tests whether first deque is greater than or equal to other or not.

7 swap

Exchanges the contents of two deque.

Advertisements