The Wayback Machine - https://web.archive.org/web/20241001080531/https://www.geeksforgeeks.org/deque-insert-function-in-c-stl/
Open In App

deque insert() function in C++ STL

Last Updated : 14 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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).

Syntax:

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: Program 1: 

CPP




// CPP program to illustrate the
// deque::insert() function
// insert elements by iterator
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 deque<int> dq = { 1, 2, 3, 4, 5 };
 
 deque<int>::iterator it = dq.begin();
 ++it;
 
 it = dq.insert(it, 10); // 1 10 2 3 4 5
 
 std::cout << "Deque contains:";
 for (it = dq.begin(); it != dq.end(); ++it)
  cout << ' ' << *it;
 cout << '\n';
 
 return 0;
}


Output:

Deque contains: 1 10 2 3 4 5

Time Complexity: O(n)

Auxiliary Space: O(1)

Program 2: 

CPP




// CPP program to illustrate the
// deque::insert() function
// program for second syntax
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 deque<int> dq = { 1, 2, 3, 4, 5 };
 
 deque<int>::iterator it = dq.begin();
 
 // 0 0 1 2 3 4 5
 dq.insert(it, 2, 0);
 
 std::cout << "Deque contains:";
 
 for (it = dq.begin(); it != dq.end(); ++it)
  cout << ' ' << *it;
 cout << '\n';
 
 return 0;
}


Output:

Deque contains: 0 0 1 2 3 4 5

Time Complexity: O(n)

Auxiliary Space: O(1)

Program 3: 

CPP




// CPP program to illustrate the
// deque::insert() function
// program for third syntax
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 deque<int> dq = { 1, 2, 3, 4, 5 };
 
 deque<int>::iterator it = dq.begin();
 ++it;
 
 vector<int> v(2, 10);
 
 // 1 10 10 2 3 4 5
 dq.insert(it, v.begin(), v.end());
 
 std::cout << "Deque contains:";
 for (it = dq.begin(); it != dq.end(); ++it)
  cout << ' ' << *it;
 cout << '\n';
 
 return 0;
}


Output:

Deque contains: 1 10 10 2 3 4 5

Time Complexity: O(n)

Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::at()at() function is us
4 min read
deque::pop_front() and deque::pop_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::pop_front() pop_front()
4 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
5 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::operator= This operator
4 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::begin()begin() function
3 min read
deque::emplace_front() and deque::emplace_back() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::emplace_front()This fun
5 min read
Deque::front() and deque::back() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::front()fron
4 min read
Deque::empty() and deque::size() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::empty() emp
4 min read
Difference Between deque::cbegin and deque::assign in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::at in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::back in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Deque::assign deque::assign is
3 min read
Difference Between deque::begin and deque::assign in C++
Deque or Double-ended queue are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the difference
3 min read
Difference Between deque::assign and deque::empty in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
2 min read
deque max_size() function in C++ STL
The deque::max_size() is a built-in function in C++ STL which returns the maximum number of elements that a deque container can hold. Syntax: deque_name.max_size()Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements that a deque container can hold. Below programs illustrate the a
1 min read
deque rend() function in C++ STL
The deque::rend() is an inbuilt function in C++ STL which returns a reverse iterator which points to the position before the beginning of the deque (which is considered its reverse end). Syntax: deque_name.rend()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the position before t
2 min read
deque rbegin() function in C++ STL
The deque::rbegin() is an inbuilt function in C++ STL which returns a reverse iterator which points to the last element of the deque (i.e., its reverse beginning). Syntax: deque_name.rbegin()Parameter: This function does not accept any parameters. Return value: It returns a reverse iterator which points to the last element of the deque. Below progr
2 min read
deque resize() function in C++ STL
The deque::resize() is an inbuilt function in C++ STL which changes the size of the deque. If the given size is greater than the current size, then new elements are inserted at the end of the deque. If the given size is smaller than the current size, then extra elements are destroyed. Syntax: deque_name.resize(n) Parameter: The function accepts onl
2 min read
deque::push_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::push_back() push_back()
2 min read
deque::push_front() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::push_front() push_front
2 min read
deque cbegin() in C++ STL
The cbegin() method in deque is a function in C++ STL which returns an iterator pointing to the first element of the container. Syntax: deque_name.cbegin() Return value: It returns a constant iterator pointing to the first element of the deque. This means, that the iterator can be used to traverse the queue, but not to modify it. That is, functions
2 min read
deque shrink_to_fit in C++ STL
The deque::shrink_to_fit() is a built-in function in C++ STL which reduces the capacity of the container to fit its size and destroys all elements beyond the capacity. This function does not reduce the size of the container. It is used when a container has been allocated more memory than it needed then this function makes free that amount of memory
3 min read
deque crend in C++ STL
The deque::crend() is an inbuilt function in C++ STL which returns a constant reverse iterator which points to the position before the first element of the deque. Syntax deque_name.crend() Parameters: This function does not accept any parameters. Return Type: This function returns constant reverse iterator of deque. Example-1: C/C++ Code // C++ pro
2 min read
deque get_allocator in C++ STL
deque::get_allocator() is a built in function in C++ STL which is used to get allocator of container deque. Syntax: Allocator_type get_allocator() Parameters: This function does not accept any parameter. Return value: Returns an allocator associated with deque. Below programs illustrate the working of deque::get_allocator() function. Example-1: C/C
2 min read
deque crbegin in C++ STL
deque::crbegin means constant_reverse_beginner and as the name suggests it returns a constant_reverse_iterator pointing to the last element of the deque. What is a constant iterator? A constant iterator is not used for modification. It is only used for accessing the elements. You can use non_const iterators for modifying the elements. Syntax: deque
2 min read
deque emplace in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.emplace() function inserts a ne
2 min read
Deque in C++ Standard Template Library (STL)
Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements. Unlike vectors, contiguous storage allocation may not be guaranteed. Double Ended Queues are basically an implementation of the data structure doub
4 min read
multiset insert() function in C++ STL
The multiset::insert() is a built-in function in C++ STL which insert elements in the multiset container or inserts the elements from a position to another position from one multiset to a different multiset. Syntax: iterator multiset_name.insert(element)Time Complexity: O(log n) Since the elements are always in sorted order, the newly inserted elem
4 min read
unordered_set insert() function in C++ STL
The unordered_set::insert() is a built-in function in C++ STL which is used to insert a new {element} in the unordered_set container. Each element is inserted only if it is not already present in the container (elements in an unordered_set have unique values). The insertion is done automatically at the position according to the container’s criterio
3 min read
unordered_multiset insert() function in C++ STL
The unordered_multiset::insert() is a built-in function in C++ STL that inserts new elements in the unordered_multiset. This increases the container size. Also notice that elements with the same value are also stored as many times they are inserted. Syntax: Unordered_multiset_name.insert(element) Parameters: This function accepts a single parameter
2 min read
vector insert() Function in C++ STL
The std::vector::insert() is a built-in function in C++ STL that inserts new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted. It is a member function of std::vector class template. The insert function is overloaded to work on multiple cases which are as follows: Tab
4 min read
Article Tags :
Practice Tags :