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

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » STL Algorithms Modifying sequence operations » remove 
24.8.5.Use remove() to delete elements from a vector
#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()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.