Use remove() to delete elements from a vector : remove « STL Algorithms Modifying sequence operations « C++ Tutorial






#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template<class InIter>
void show_range(const char *msg, InIter start, InIter end);

int main()
{
  vector<char> v;
  vector<char>::iterator itr, itr_end;

  for(int i=0; i<5; i++) {
    v.push_back('A'+i);
  }
  for(int i=0; i<5; i++) {
    v.push_back('A'+i);
  }

  show_range("Original contents of v:", v.begin(), v.end());

  // Remove all A's.
  itr_end = remove(v.begin(), v.end(), 'A');

  show_range("v after removing all A's:", v.begin(), itr_end);

  return 0;
}

template<class InIter>
void show_range(const char *msg, InIter start, InIter end) {
  InIter itr;

  cout << msg << endl;
  for(itr = start; itr != end; ++itr)
    cout << *itr << endl;
}








24.8.remove
24.8.1.Use the generic remove algorithm
24.8.2.Use std::remove to delete all element in a vector by value
24.8.3.Remove an element and then erase that element
24.8.4.Combine remove and erase together
24.8.5.Use remove() to delete elements from a vector
24.8.6.std::remove does not change the size of the container,it moves elements forward to fill gaps created and returns the new 'end' position.
24.8.7.Remove value from a vector with remove()