deque insert() function in C++ STL
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 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; } |
Deque contains: 1 10 2 3 4 5
Program 2:
// 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; } |
Deque contains: 0 0 1 2 3 4 5
Program 3:
// 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; } |
Deque contains: 1 10 10 2 3 4 5
Recommended Posts:
- deque resize() function in C++ STL
- deque rend() function in C++ STL
- deque rbegin() function in C++ STL
- deque max_size() function in C++ STL
- deque assign() function in C++ STL
- set insert() function in C++ STL
- deque::pop_front() and deque::pop_back() in C++ STL
- deque::emplace_front() and deque::emplace_back() in C++ STL
- deque::empty() and deque::size() in C++ STL
- deque::clear() and deque::erase() in C++ STL
- deque::front() and deque::back() in C++ STL
- unordered_multiset insert() function in C++ STL
- vector insert() function in C++ STL
- unordered_set insert() function in C++ STL
- multiset insert() function in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : BhushanPagare



