Skip to content

vector_pro::insert

C++
size_type insert(size_type position, const value_type& val);

iterator_pro<value_type> insert(iterator_pro<value_type> position, const value_type& val);
iterator_pro<value_type> insert(iterator_pro<value_type> position, size_type num, const value_type& val);
iterator_pro<value_type> insert(iterator_pro<value_type> position, const_iterator_pro<value_type> from, const_iterator_pro<value_type> exclude_to);
iterator_pro<value_type> insert(iterator_pro<value_type> position, const std::initializer_list<value_type>& li);

const_iterator_pro<value_type> insert(const_iterator_pro<value_type> position, const value_type& val);
const_iterator_pro<value_type> insert(const_iterator_pro<value_type> position, size_type num, const value_type& val);
const_iterator_pro<value_type> insert(const_iterator_pro<value_type> position, const_iterator_pro<value_type> from, const_iterator_pro<value_type> exclude_to);
const_iterator_pro<value_type> insert(const_iterator_pro<value_type> position, const std::initializer_list<value_type>& li);

Insert elements

The vector is extended by inserting new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.

This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.

Because vectors use an array as their underlying storage, inserting elements in positions other than the vector end causes the container to relocate all the elements that were after position to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list).

Parameters

  1. position
    Position in the container where the new elements are inserted.
    Can be size_type / iterator_pro / const_iterator_pro .
  2. val
    Value to be copied to the inserted elements.
  3. num
    Number of elements to insert. Each element is initialized to a copy of val.
  4. from , exclude_to
    The range of elements we are going to insert.
    Can be size_type / iterator_pro / const_iterator_pro .
    The range includes all the elements between from and exclude_to, including the element pointed by from but not the one pointed by exclude_to.
  5. li
    An initializer_list object. Copies of these elements are inserted at position (in the same order).

Return value

The return value will point to the first of the newly inserted elements.

Type will be same as the position. ( size_type / iterator_pro / const_iterator_pro )

Example

C++
// inserting into a vector_pro
#include <iostream>
#include "vector_pro.h"

/**
 * Output:
 * myvector contains: 501 502 503 300 300 400 400 200 100 100 100
 */

int main ()
{
  vector_pro<int> myvector (3,100);
  iterator_pro<int> it;

  it = myvector.begin();
  it = myvector.insert ( it , 200 );

  myvector.insert (it,2,300);

  // "it" no longer valid, get a new one:
  it = myvector.begin();

  vector_pro<int> anothervector (2,400);
  myvector.insert (it+2,anothervector.begin(),anothervector.end());

  std::initializer_list<int> myarray = { 501,502,503 };
  myvector.insert (myvector.begin(), myarray);

  std::cout << "myvector contains:";
  for (it=myvector.begin(); it != myvector.end(); it++)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

Complexity

Linear on the number of elements inserted (copy/move construction) plus the number of elements after position (moving).