The deque::insert() function is a built-in function in C++ which is used to insert elements in the deque.
The insert() function can be used in three ways:
- Extends deque by inserting a new element val at a position.
- Extends deque by inserting n new element of value val in the deque.
- Extends deque by inserting new element in the range [first, last).
deque_name.insert (iterator position, const value_type& val) or deque_name.insert (iterator position, size_type n, const value_type& val) or deque_name.insert (iterator position, InputIterator first, InputIterator last)
Parameters: The function accepts four parameters which are specified as below:
- position – Specifies the position where the element/elements are to be inserted.
- val – specifies the value to be assigned to newly inserted element.
- n – specifies the number of elements to insert. Each element is initialized to a copy of val.
- first, last – specifies the iterators specifying a range of elements which is to be inserted. The range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
Return value: The function returns an iterator that points to the first of the newly inserted elements.
Below programs illustrate the above function:
Deque contains: 1 10 2 3 4 5
Deque contains: 0 0 1 2 3 4 5
Deque contains: 1 10 10 2 3 4 5