C++ STL algorithm, another remove() code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Removes all elements that match value from the range (First, Last). It returns an iterator equal to Last - n, where n = number of elements removed. The last n elements of the range have undefined values. The size of the container remains the same in C++ programming

To show: How to use the C++ algorithm, remove() to remove all elements that match value from the range (First, Last). It returns an iterator equal to Last - n, where n = number of elements removed. The last n elements of the range have undefined values. The size of the container remains the same in C++ programming

 

// Illustrates how to use the remove function.

//

// functions:

// remove() - remove all elements from the sequence that match value.

// begin() - returns an iterator that points to the first element in a sequence.

// end() - returns an iterator that points one past the end of a sequence.

#include <iostream>

#include <vector>

#include <algorithm>

#include <functional>

using namespace std;

 

int main(void)

{

// define a template class vector of integers

typedef vector<int> IntVector;

// define an iterator for template class vector of integer using typedef to simplify the name

typedef IntVector::iterator IntVectorIt;

// vector containing numbers

IntVector Numbers(8);

// iterators

IntVectorIt start, end, it, last;

// location of first element of Numbers

start = Numbers.begin();

// one past the location last element of Numbers

end = Numbers.end();

// Initialize Numbers vector

Numbers[0] = 10;

Numbers[1] = 20;

Numbers[2] = 10;

Numbers[3] = 15;

Numbers[4] = 12;

Numbers[5] = 7;

Numbers[6] = 9;

Numbers[7] = 10;

cout<<"Before calling remove(), Numbers vector are: ";

// print content of Numbers

for(it = start; it != end; it++)

cout<<*it<<" ";

cout<<endl;

// remove all elements from Numbers that match 10

last = remove(start, end, 10);

cout<<"After calling remove(), Numbers vector are: ";

// print content of Numbers

for(it = start; it != end; it++)

cout<<*it<<" ";

cout<<endl;

// print number of elements removed from Numbers

cout<<"Total number of elements removed from Numbers vector = "<<end - last<<endl;

// print only the valid elements of Number

cout<<"Valid elements of Numbers vector are: ";

for(it = start; it != last; it++)

cout<<*it<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

Before calling remove(), Numbers vector are: 10 20 10 15 12 7 9 10

After calling remove(), Numbers vector are: 20 15 12 7 9 7 9 10

Total number of elements removed from Numbers vector = 3

Valid elements of Numbers vector are: 20 15 12 7 9

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index