Use the generic remove algorithm : 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.1.Use the generic remove algorithm
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
  const int  N = 11;
  int array1[N{12034056708};
  vector<int> vector1;

  for (int i = 0; i < N; ++i)
    vector1.push_back(array1[i]);

  // Remove the zeros from vector1:
  vector<int>::iterator new_end;
  new_end = remove(vector1.begin(), vector1.end()0);

  for (int i = 0; i < (int)vector1.size(); ++i)
    cout << vector1[i];
  cout << "\n\n\n\n\n";
  // The size of vector1 remains the same:
  assert (vector1.size() == N);
  cout << vector1.size();

  cout << "\n\n\n\n\n";

  // The nonzero elements are left in [vector1.begin(), new_end).  Erase the rest:
  vector1.erase(new_end, vector1.end());

  // Show that 3 elements were removed and the nonzero elements remain, in their original order:
  assert (vector1.size() == N - 3);
  cout << vector1.size();
  cout << "\n\n\n\n\n";

  for (int i = 0; i < (int)vector1.size(); ++i)
    cout << vector1[i];

  return 0;
}
12345678708




11




8




12345678
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.