| Home | Libraries | People | FAQ | More

## Class template deque

boost::container::deque

## Synopsis

    // In header: <boost/container/deque.hpp>

    template<typename T, typename Allocator = new_allocator<T> >
    class deque : protected deque_base< Allocator > {
    public:
      // types
      typedef T                                                                  value_type;
      typedef ::boost::container::allocator_traits< Allocator >::pointer         pointer;
      typedef ::boost::container::allocator_traits< Allocator >::const_pointer   const_pointer;
      typedef ::boost::container::allocator_traits< Allocator >::reference       reference;
      typedef ::boost::container::allocator_traits< Allocator >::const_reference const_reference;
      typedef ::boost::container::allocator_traits< Allocator >::size_type       size_type;
      typedef ::boost::container::allocator_traits< Allocator >::difference_type difference_type;
      typedef Allocator                                                          allocator_type;
      typedef implementation_defined                                             stored_allocator_type;
      typedef implementation_defined                                             iterator;
      typedef implementation_defined                                             const_iterator;
      typedef implementation_defined                                             reverse_iterator;
      typedef implementation_defined                                             const_reverse_iterator;

      // construct/copy/destruct
      deque();
      explicit deque(const allocator_type &) noexcept;
      explicit deque(size_type);
      deque(size_type, default_init_t);
      explicit deque(size_type, const allocator_type &);
      deque(size_type, default_init_t, const allocator_type &);
      deque(size_type, const value_type &,
            const allocator_type & = allocator_type());
      template<typename InIt>
        deque(InIt, InIt, const allocator_type & = allocator_type());
      deque(std::initializer_list< value_type >,
            const allocator_type & = allocator_type());
      deque(const deque &);
      deque(deque &&);
      deque(const deque &, const allocator_type &);
      deque(deque &&, const allocator_type &);
      deque & operator=(const deque &);
      deque & operator=(deque &&) noexcept(allocator_traits_type::propagate_on_container_move_assignment::value||allocator_traits_type::is_always_equal::value));
      deque & operator=(std::initializer_list< value_type >);
      ~deque();

      // public member functions
      void assign(size_type, const T &);
      template<typename InIt> void assign(InIt, InIt);
      void assign(std::initializer_list< value_type >);
      allocator_type get_allocator() const noexcept;
      const stored_allocator_type & get_stored_allocator() const noexcept;
      stored_allocator_type & get_stored_allocator() noexcept;
      iterator begin() noexcept;
      const_iterator begin() const noexcept;
      iterator end() noexcept;
      const_iterator end() const noexcept;
      reverse_iterator rbegin() noexcept;
      const_reverse_iterator rbegin() const noexcept;
      reverse_iterator rend() noexcept;
      const_reverse_iterator rend() const noexcept;
      const_iterator cbegin() const noexcept;
      const_iterator cend() const noexcept;
      const_reverse_iterator crbegin() const noexcept;
      const_reverse_iterator crend() const noexcept;
      bool empty() const noexcept;
      size_type size() const noexcept;
      size_type max_size() const noexcept;
      void resize(size_type);
      void resize(size_type, default_init_t);
      void resize(size_type, const value_type &);
      void shrink_to_fit();
      reference front() noexcept;
      const_reference front() const noexcept;
      reference back() noexcept;
      const_reference back() const noexcept;
      reference operator[](size_type) noexcept;
      const_reference operator[](size_type) const noexcept;
      iterator nth(size_type) noexcept;
      const_iterator nth(size_type) const noexcept;
      size_type index_of(iterator) noexcept;
      size_type index_of(const_iterator) const noexcept;
      reference at(size_type);
      const_reference at(size_type) const;
      template<class... Args> void emplace_front(Args &&...);
      template<class... Args> void emplace_back(Args &&...);
      template<class... Args> iterator emplace(const_iterator, Args &&...);
      void push_front(const T &);
      void push_front(T &&);
      void push_back(const T &);
      void push_back(T &&);
      iterator insert(const_iterator, const T &);
      iterator insert(const_iterator, T &&);
      iterator insert(const_iterator, size_type, const value_type &);
      template<typename InIt> iterator insert(const_iterator, InIt, InIt);
      iterator insert(const_iterator, std::initializer_list< value_type >);
      void pop_front() noexcept;
      void pop_back() noexcept;
      iterator erase(const_iterator) noexcept;
      iterator erase(const_iterator, const_iterator) noexcept;
      void swap(deque &) noexcept(allocator_traits_type::propagate_on_container_swap::value||allocator_traits_type::is_always_equal::value));
      void clear() noexcept;

      // friend functions
      friend bool operator==(const deque &, const deque &);
      friend bool operator!=(const deque &, const deque &);
      friend bool operator<(const deque &, const deque &);
      friend bool operator>(const deque &, const deque &);
      friend bool operator<=(const deque &, const deque &);
      friend bool operator>=(const deque &, const deque &);
      friend void swap(deque &, deque &);
    };

## Description

A double-ended queue is a sequence that supports random access to elements, constant time insertion and removal of elements at the end of the sequence, and linear time insertion and removal of elements in the middle.

### Template Parameters

1.     typename T

   The type of object that is stored in the deque

2.     typename Allocator = new_allocator<T>

   The allocator used for all internal memory management

### `deque` public construct/copy/destruct

1.     deque();

   Effects: Default constructors a deque.

   Throws: If allocator_type's default constructor throws.

   Complexity: Constant.

2.     explicit deque(const allocator_type & a) noexcept;

   Effects: Constructs a deque taking the allocator as parameter.

   Throws: Nothing

   Complexity: Constant.

3.     explicit deque(size_type n);

   Effects: Constructs a deque and inserts n value initialized values.

   Throws: If allocator_type's default constructor throws or T's value initialization throws.

   Complexity: Linear to n.

4.     deque(size_type n, default_init_t);

   Effects: Constructs a deque and inserts n default initialized values.

   Throws: If allocator_type's default constructor throws or T's default initialization or copy constructor throws.

   Complexity: Linear to n.

   Note: Non-standard extension

5.     explicit deque(size_type n, const allocator_type & a);

   Effects: Constructs a deque that will use a copy of allocator a and inserts n value initialized values.

   Throws: If allocator_type's default constructor throws or T's value initialization throws.

   Complexity: Linear to n.

6.     deque(size_type n, default_init_t, const allocator_type & a);

   Effects: Constructs a deque that will use a copy of allocator a and inserts n default initialized values.

   Throws: If allocator_type's default constructor throws or T's default initialization or copy constructor throws.

   Complexity: Linear to n.

   Note: Non-standard extension

7.     deque(size_type n, const value_type & value,
             const allocator_type & a = allocator_type());

   Effects: Constructs a deque that will use a copy of allocator a and inserts n copies of value.

   Throws: If allocator_type's default constructor throws or T's copy constructor throws.

   Complexity: Linear to n.

8.     template<typename InIt>
         deque(InIt first, InIt last, const allocator_type & a = allocator_type());

   Effects: Constructs a deque that will use a copy of allocator a and inserts a copy of the range [first, last) in the deque.

   Throws: If allocator_type's default constructor throws or T's constructor taking a dereferenced InIt throws.

   Complexity: Linear to the range [first, last).

9.     deque(std::initializer_list< value_type > il,
             const allocator_type & a = allocator_type());

   Effects: Constructs a deque that will use a copy of allocator a and inserts a copy of the range [il.begin(), il.end()) in the deque.

   Throws: If allocator_type's default constructor throws or T's constructor taking a dereferenced std::initializer_list iterator throws.

   Complexity: Linear to the range [il.begin(), il.end()).

10.     deque(const deque & x);

    Effects: Copy constructs a deque.

    Postcondition: x == *this.

    Complexity: Linear to the elements x contains.

11.     deque(deque && x);

    Effects: Move constructor. Moves x's resources to *this.

    Throws: If allocator_type's copy constructor throws.

    Complexity: Constant.

12.     deque(const deque & x, const allocator_type & a);

    Effects: Copy constructs a vector using the specified allocator.

    Postcondition: x == *this.

    Throws: If allocation throws or T's copy constructor throws.

    Complexity: Linear to the elements x contains.

13.     deque(deque && x, const allocator_type & a);

    Effects: Move constructor using the specified allocator. Moves x's resources to *this if a == allocator_type(). Otherwise copies values from x to *this.

    Throws: If allocation or T's copy constructor throws.

    Complexity: Constant if a == x.get_allocator(), linear otherwise.

14.     deque & operator=(const deque & x);

    Effects: Makes *this contain the same elements as x.

    Postcondition: this->size() == x.size(). *this contains a copy of each of x's elements.

    Throws: If memory allocation throws or T's copy constructor throws.

    Complexity: Linear to the number of elements in x.

15.     deque & operator=(deque && x) noexcept(allocator_traits_type::propagate_on_container_move_assignment::value||allocator_traits_type::is_always_equal::value));

    Effects: Move assignment. All x's values are transferred to *this.

    Throws: If allocator_traits_type::propagate_on_container_move_assignment is false and (allocation throws or value_type's move constructor throws)

    Complexity: Constant if allocator_traits_type:: propagate_on_container_move_assignment is true or this->get>allocator() == x.get_allocator(). Linear otherwise.

16.     deque & operator=(std::initializer_list< value_type > il);

    Effects: Makes *this contain the same elements as il.

    Postcondition: this->size() == il.size(). *this contains a copy of each of x's elements.

    Throws: If memory allocation throws or T's copy constructor throws.

    Complexity: Linear to the number of elements in il.

17.     ~deque();

    Effects: Destroys the deque. All stored values are destroyed and used memory is deallocated.

    Throws: Nothing.

    Complexity: Linear to the number of elements.

### `deque` public member functions

1.     void assign(size_type n, const T & val);

   Effects: Assigns the n copies of val to *this.

   Throws: If memory allocation throws or T's copy constructor throws.

   Complexity: Linear to n.

2.     template<typename InIt> void assign(InIt first, InIt last);

   Effects: Assigns the the range [first, last) to *this.

   Throws: If memory allocation throws or T's constructor from dereferencing InIt throws.

   Complexity: Linear to n.

3.     void assign(std::initializer_list< value_type > il);

   Effects: Assigns the the range [il.begin(), il.end()) to *this.

   Throws: If memory allocation throws or T's constructor from dereferencing std::initializer_list iterator throws.

   Complexity: Linear to il.size().

4.     allocator_type get_allocator() const noexcept;

   Effects: Returns a copy of the internal allocator.

   Throws: If allocator's copy constructor throws.

   Complexity: Constant.

5.     const stored_allocator_type & get_stored_allocator() const noexcept;

   Effects: Returns a reference to the internal allocator.

   Throws: Nothing

   Complexity: Constant.

   Note: Non-standard extension.

6.     stored_allocator_type & get_stored_allocator() noexcept;

   Effects: Returns a reference to the internal allocator.

   Throws: Nothing

   Complexity: Constant.

   Note: Non-standard extension.

7.     iterator begin() noexcept;

   Effects: Returns an iterator to the first element contained in the deque.

   Throws: Nothing.

   Complexity: Constant.

8.     const_iterator begin() const noexcept;

   Effects: Returns a const_iterator to the first element contained in the deque.

   Throws: Nothing.

   Complexity: Constant.

9.     iterator end() noexcept;

   Effects: Returns an iterator to the end of the deque.

   Throws: Nothing.

   Complexity: Constant.

10.     const_iterator end() const noexcept;

    Effects: Returns a const_iterator to the end of the deque.

    Throws: Nothing.

    Complexity: Constant.

11.     reverse_iterator rbegin() noexcept;

    Effects: Returns a reverse_iterator pointing to the beginning of the reversed deque.

    Throws: Nothing.

    Complexity: Constant.

12.     const_reverse_iterator rbegin() const noexcept;

    Effects: Returns a const_reverse_iterator pointing to the beginning of the reversed deque.

    Throws: Nothing.

    Complexity: Constant.

13.     reverse_iterator rend() noexcept;

    Effects: Returns a reverse_iterator pointing to the end of the reversed deque.

    Throws: Nothing.

    Complexity: Constant.

14.     const_reverse_iterator rend() const noexcept;

    Effects: Returns a const_reverse_iterator pointing to the end of the reversed deque.

    Throws: Nothing.

    Complexity: Constant.

15.     const_iterator cbegin() const noexcept;

    Effects: Returns a const_iterator to the first element contained in the deque.

    Throws: Nothing.

    Complexity: Constant.

16.     const_iterator cend() const noexcept;

    Effects: Returns a const_iterator to the end of the deque.

    Throws: Nothing.

    Complexity: Constant.

17.     const_reverse_iterator crbegin() const noexcept;

    Effects: Returns a const_reverse_iterator pointing to the beginning of the reversed deque.

    Throws: Nothing.

    Complexity: Constant.

18.     const_reverse_iterator crend() const noexcept;

    Effects: Returns a const_reverse_iterator pointing to the end of the reversed deque.

    Throws: Nothing.

    Complexity: Constant.

19.     bool empty() const noexcept;

    Effects: Returns true if the deque contains no elements.

    Throws: Nothing.

    Complexity: Constant.

20.     size_type size() const noexcept;

    Effects: Returns the number of the elements contained in the deque.

    Throws: Nothing.

    Complexity: Constant.

21.     size_type max_size() const noexcept;

    Effects: Returns the largest possible size of the deque.

    Throws: Nothing.

    Complexity: Constant.

22.     void resize(size_type new_size);

    Effects: Inserts or erases elements at the end such that the size becomes n. New elements are value initialized.

    Throws: If memory allocation throws, or T's constructor throws.

    Complexity: Linear to the difference between size() and new_size.

23.     void resize(size_type new_size, default_init_t);

    Effects: Inserts or erases elements at the end such that the size becomes n. New elements are default initialized.

    Throws: If memory allocation throws, or T's constructor throws.

    Complexity: Linear to the difference between size() and new_size.

    Note: Non-standard extension

24.     void resize(size_type new_size, const value_type & x);

    Effects: Inserts or erases elements at the end such that the size becomes n. New elements are copy constructed from x.

    Throws: If memory allocation throws, or T's copy constructor throws.

    Complexity: Linear to the difference between size() and new_size.

25.     void shrink_to_fit();

    Effects: Tries to deallocate the excess of memory created with previous allocations. The size of the deque is unchanged

    Throws: If memory allocation throws.

    Complexity: Constant.

26.     reference front() noexcept;

    Requires: !empty()

    Effects: Returns a reference to the first element of the container.

    Throws: Nothing.

    Complexity: Constant.

27.     const_reference front() const noexcept;

    Requires: !empty()

    Effects: Returns a const reference to the first element from the beginning of the container.

    Throws: Nothing.

    Complexity: Constant.

28.     reference back() noexcept;

    Requires: !empty()

    Effects: Returns a reference to the last element of the container.

    Throws: Nothing.

    Complexity: Constant.

29.     const_reference back() const noexcept;

    Requires: !empty()

    Effects: Returns a const reference to the last element of the container.

    Throws: Nothing.

    Complexity: Constant.

30.     reference operator[](size_type n) noexcept;

    Requires: size() > n.

    Effects: Returns a reference to the nth element from the beginning of the container.

    Throws: Nothing.

    Complexity: Constant.

31.     const_reference operator[](size_type n) const noexcept;

    Requires: size() > n.

    Effects: Returns a const reference to the nth element from the beginning of the container.

    Throws: Nothing.

    Complexity: Constant.

32.     iterator nth(size_type n) noexcept;

    Requires: size() >= n.

    Effects: Returns an iterator to the nth element from the beginning of the container. Returns end() if n == size().

    Throws: Nothing.

    Complexity: Constant.

    Note: Non-standard extension

33.     const_iterator nth(size_type n) const noexcept;

    Requires: size() >= n.

    Effects: Returns a const_iterator to the nth element from the beginning of the container. Returns end() if n == size().

    Throws: Nothing.

    Complexity: Constant.

    Note: Non-standard extension

34.     size_type index_of(iterator p) noexcept;

    Requires: size() >= n.

    Effects: Returns an iterator to the nth element from the beginning of the container. Returns end() if n == size().

    Throws: Nothing.

    Complexity: Constant.

    Note: Non-standard extension

35.     size_type index_of(const_iterator p) const noexcept;

    Requires: begin() <= p <= end().

    Effects: Returns the index of the element pointed by p and size() if p == end().

    Throws: Nothing.

    Complexity: Constant.

    Note: Non-standard extension

36.     reference at(size_type n);

    Requires: size() > n.

    Effects: Returns a reference to the nth element from the beginning of the container.

    Throws: std::range_error if n >= size()

    Complexity: Constant.

37.     const_reference at(size_type n) const;

    Requires: size() > n.

    Effects: Returns a const reference to the nth element from the beginning of the container.

    Throws: std::range_error if n >= size()

    Complexity: Constant.

38.     template<class... Args> void emplace_front(Args &&... args);

    Effects: Inserts an object of type T constructed with std::forward<Args>(args)... in the beginning of the deque.

    Throws: If memory allocation throws or the in-place constructor throws.

    Complexity: Amortized constant time

39.     template<class... Args> void emplace_back(Args &&... args);

    Effects: Inserts an object of type T constructed with std::forward<Args>(args)... in the end of the deque.

    Throws: If memory allocation throws or the in-place constructor throws.

    Complexity: Amortized constant time

40.     template<class... Args> iterator emplace(const_iterator p, Args &&... args);

    Requires: p must be a valid iterator of *this.

    Effects: Inserts an object of type T constructed with std::forward<Args>(args)... before p

    Throws: If memory allocation throws or the in-place constructor throws.

    Complexity: If p is end(), amortized constant time Linear time otherwise.

41.     void push_front(const T & x);

    Effects: Inserts a copy of x at the front of the deque.

    Throws: If memory allocation throws or T's copy constructor throws.

    Complexity: Amortized constant time.

42.     void push_front(T && x);

    Effects: Constructs a new element in the front of the deque and moves the resources of x to this new element.

    Throws: If memory allocation throws.

    Complexity: Amortized constant time.

43.     void push_back(const T & x);

    Effects: Inserts a copy of x at the end of the deque.

    Throws: If memory allocation throws or T's copy constructor throws.

    Complexity: Amortized constant time.

44.     void push_back(T && x);

    Effects: Constructs a new element in the end of the deque and moves the resources of x to this new element.

    Throws: If memory allocation throws.

    Complexity: Amortized constant time.

45.     iterator insert(const_iterator p, const T & x);

    Requires: p must be a valid iterator of *this.

    Effects: Insert a copy of x before p.

    Returns: an iterator to the inserted element.

    Throws: If memory allocation throws or x's copy constructor throws.

    Complexity: If p is end(), amortized constant time Linear time otherwise.

46.     iterator insert(const_iterator p, T && x);

    Requires: p must be a valid iterator of *this.

    Effects: Insert a new element before p with x's resources.

    Returns: an iterator to the inserted element.

    Throws: If memory allocation throws.

    Complexity: If p is end(), amortized constant time Linear time otherwise.

47.     iterator insert(const_iterator pos, size_type n, const value_type & x);

    Requires: pos must be a valid iterator of *this.

    Effects: Insert n copies of x before pos.

    Returns: an iterator to the first inserted element or pos if n is 0.

    Throws: If memory allocation throws or T's copy constructor throws.

    Complexity: Linear to n.

48.     template<typename InIt>
          iterator insert(const_iterator pos, InIt first, InIt last);

    Requires: pos must be a valid iterator of *this.

    Effects: Insert a copy of the [first, last) range before pos.

    Returns: an iterator to the first inserted element or pos if first == last.

    Throws: If memory allocation throws, T's constructor from a dereferenced InIt throws or T's copy constructor throws.

    Complexity: Linear to distance [first, last).

49.     iterator insert(const_iterator pos, std::initializer_list< value_type > il);

    Requires: pos must be a valid iterator of *this.

    Effects: Insert a copy of the [il.begin(), il.end()) range before pos.

    Returns: an iterator to the first inserted element or pos if il.begin() == il.end().

    Throws: If memory allocation throws, T's constructor from a dereferenced std::initializer_list throws or T's copy constructor throws.

    Complexity: Linear to distance [il.begin(), il.end()).

50.     void pop_front() noexcept;

    Effects: Removes the first element from the deque.

    Throws: Nothing.

    Complexity: Constant time.

51.     void pop_back() noexcept;

    Effects: Removes the last element from the deque.

    Throws: Nothing.

    Complexity: Constant time.

52.     iterator erase(const_iterator pos) noexcept;

    Effects: Erases the element at p.

    Throws: Nothing.

    Complexity: Linear to the elements between pos and the last element (if pos is near the end) or the first element if(pos is near the beginning). Constant if pos is the first or the last element.

53.     iterator erase(const_iterator first, const_iterator last) noexcept;

    Effects: Erases the elements pointed by [first, last).

    Throws: Nothing.

    Complexity: Linear to the distance between first and last plus the elements between pos and the last element (if pos is near the end) or the first element if(pos is near the beginning).

54.     void swap(deque & x) noexcept(allocator_traits_type::propagate_on_container_swap::value||allocator_traits_type::is_always_equal::value));

    Effects: Swaps the contents of *this and x.

    Throws: Nothing.

    Complexity: Constant.

55.     void clear() noexcept;

    Effects: Erases all the elements of the deque.

    Throws: Nothing.

    Complexity: Linear to the number of elements in the deque.

### `deque` friend functions

1.     friend bool operator==(const deque & x, const deque & y);

   Effects: Returns true if x and y are equal

   Complexity: Linear to the number of elements in the container.

2.     friend bool operator!=(const deque & x, const deque & y);

   Effects: Returns true if x and y are unequal

   Complexity: Linear to the number of elements in the container.

3.     friend bool operator<(const deque & x, const deque & y);

   Effects: Returns true if x is less than y

   Complexity: Linear to the number of elements in the container.

4.     friend bool operator>(const deque & x, const deque & y);

   Effects: Returns true if x is greater than y

   Complexity: Linear to the number of elements in the container.

5.     friend bool operator<=(const deque & x, const deque & y);

   Effects: Returns true if x is equal or less than y

   Complexity: Linear to the number of elements in the container.

6.     friend bool operator>=(const deque & x, const deque & y);

   Effects: Returns true if x is equal or greater than y

   Complexity: Linear to the number of elements in the container.

7.     friend void swap(deque & x, deque & y);

   Effects: x.swap(y)

   Complexity: Constant.