C++ Deque::insert() Function



The C++ std::deque::insert() function is used to insert the element at specified position. It takes parameters for the position where insertion should occur and the element to insert. When the elements are inserted it adjusts the container size dynamically.

Unlike vectors, deque supports support insertion at both ends and within the container due to their double-linked structure.

This function has 5 polymorphic variants: with using the single element version, fill version, range version, move version and the initializer list version (you can find the syntaxes of all the variants below).

Syntax

Following is the syntax for std::deque::insert() function.

iterator insert (const_iterator position, const value_type& val);
or	
iterator insert (const_iterator position, size_type n, const value_type& val);
or
iterator insert (const_iterator position, InputIterator first, InputIterator last);
or
iterator insert (const_iterator position, value_type&& val);
or
iterator insert (const_iterator position, initializer_list<value_type> il);

Parameters

  • position − It indicates the position in the container where the new elements are inserted.
  • val − It indicates the value to be assigned to newly inserted element.
  • n − It indicates the number of elements to insert.
  • first, last − It indicates the iterators specifying a range of elements.
  • il − It indicates an initializer_list object.

Return value

It returns an iterator that points to the newly inserted element.

Exceptions

If reallocation fails bad_alloc exception is thrown.

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 insert the element at the beginning of the deque.

#include <iostream>
#include <deque>
int main()
{
    std::deque<char> a = {'B', 'C', 'D'};
    a.insert(a.begin(), 'A');
    for (auto x = a.begin(); x != a.end(); ++x) {
        std::cout << *x << " ";
    }
    std::cout << std::endl;
    return 0;
}

Output

Following is the output of the above code −

A B C D

Example

Consider the following example, where we are going to use the range version to insert the element.

#include <iostream>
#include <deque>
#include <vector>
int main()
{
    std::deque<char> a = {'A', 'B', 'C'};
    std::vector<char> b = {'D', 'E'};
    a.insert(a.begin() + 1, b.begin(), b.end());
    for (auto x = a.begin(); x != a.end(); ++x) {
        std::cout << *x << " ";
    }
    std::cout << std::endl;
    return 0;
}

Output

Output of the above code is as follows −

A D E B C

Example

In the following example, we are going to use the initializer_list version to insert the element.

#include <iostream>
#include <deque>
int main()
{
    std::deque<int> a = {1,22,333};
    a.insert(a.begin() + 1, {421, 532});
    for (auto x = a.begin(); x != a.end(); ++x) {
        std::cout << *x << " ";
    }
    std::cout << std::endl;
    return 0;
}

Output

If we run the above code it will generate the following output −

1 421 532 22 333

Example

Following is the example, where we are going to use the move version to insert the element.

#include <iostream>
#include <deque>
#include <algorithm>
int main()
{
    std::deque<int> a = {01, 12, 23};
    std::vector<int> b = {34, 45};
    std::move(b.begin(), b.end(), std::inserter(a, a.begin() + 1));
    for (auto x = a.begin(); x != a.end(); ++x) {
        std::cout << *x << " ";
    }
    std::cout << std::endl;
    return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

1 34 45 12 23
deque.htm
Advertisements