Skip to content

vector_pro::erase

C++
size_type erase(size_type position);
size_type erase(size_type from, size_type exclude_to);
iterator_pro<value_type> erase(const_iterator_pro<value_type> position);
iterator_pro<value_type> erase(const_iterator_pro<value_type> from, const_iterator_pro<value_type> exclude_to);

Erase elements

Removes from the vector either a single element ( position ) or a range of elements ( [from, exclude_to) ).

This effectively reduces the container size by the number of elements removed, which are destroyed.

Please note that if the variables position, from, or exclude_to exceed the bounds of the vector, this function will throw an exception . You will also get one when from is gt exclude_to .

Parameters

  1. position
    Position in the container where the target element is.
    Can be size_type / iterator_pro / const_iterator_pro .
  2. from , exclude_to
    The range of elements we are going to erase.
    Can be size_types or (const_)iterator_pros.
    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.

Return value

The return value will point to the new location of the element that followed the last element erased by the function call.

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

Example

C++
// erasing from vector_pro
#include <iostream>
#include "vector_pro.h"

/**
 * Output:
 * myvector contains: 4 5 7 8 9 10
 */

int main ()
{
  vector_pro<int> myvector;

  // set some values (from 1 to 10)
  for (int i=1; i<=10; i++) myvector.push_back(i);

  // erase the 6th element
  myvector.erase (myvector.begin()+5);

  // erase the first 3 elements:
  myvector.erase (myvector.begin(),myvector.begin()+3);

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}

Complexity

Linear on the number of elements erased (destructions) plus the number of elements after the last element deleted (moving).