![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Deque::erase() Function
The C++ std::deque::erase() function is used to remove an element specified by the iterator or a range from the deque. It can delete a single element at a given position or a range of elements defined by two iterators. After erasing, it adjusts the size of the deque accordingly.
This function has 2 polymorphic variants: with position version or range version (you can find the syntaxes of all the variants below).
Syntax
Following is the syntax for std::deque::erase() function.
iterator erase (const_iterator position ); or iterator erase (const_iterator first, const_iterator last );
Parameters
- position − It indicates the iterator pointing to a single element to be removed.
- first, last − It indicates the iterators specifying a range in deque to remove the element.
position − Iterator points to the deque element.
Return value
It returns an iterator pointing to the new location of the element.
Exceptions
It throws an exception (undefined behaviour), if position is invalid.
Time complexity
The time complexity of this function is Linear i.e. O(n)
Example
Let's look at the following example, where we are going to use the position version to erase the element.
#include <iostream> #include <deque> int main() { std::deque<char> a = {'A', 'B', 'C', 'D', 'E'}; auto x = a.begin() + 2; a.erase(x); for (char num : a) { std::cout << num << " "; } std::cout << std::endl; return 0; }
Output
Output of the above code is as follows −
A B D E
Example
Consider the another scenario, where we are going to erase the elements using range version.
#include <iostream> #include <deque> int main() { std::deque<int> a = {1, 22,321,4321,12345}; auto x = a.begin() + 1; auto y = a.begin() + 4; a.erase(x, y); for (auto num : a) { std::cout << num << " "; } std::cout << "\n"; return 0; }
Output
Following is the output of the above code −
1 12345