# My Training Period: xx hours

More code samples compiled using MicrosoftVisual C++ .Net, win32 empty console mode application. g++ compilation examples given at the end of this Module. Source code for this tutorial is available inC++ STL Algorithm source code.

The C++ STL algorithm skills that supposed to be acquired:

# remove_if()

• Eliminates elements that satisfy a predicate from a given range without disturbing the order of the remaining elements and returning the end of a new range free of the specified value.

`template<class ForwardIterator, class Predicate> ForwardIterator remove( ForwardIterator _First,`
`				ForwardIterator _Last, Predicate _Pred );`

# Parameters

## Parameter

_First

_Last

_Pred

#### Table 36.6

• The return value is a forward iterator addressing the new end position of the modified range, one past the final element of the remnant sequence free of the specified value.

• The range referenced must be valid; all pointers must be de-referenceable and within the sequence the last position is reachable from the first by incrementation.

• The order of the elements not removed remains stable.

• The operator== used to determine the equality between elements must impose an equivalence relation between its operands.

• The complexity is linear: there are(_Last – _First) comparisons for equality.

• List has a more efficient member function version of remove which re-links pointers.

// algorithm, remove_if()

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

bool greathan(int value)

{return value >5;}

int main()

{

vector <int> vec1;

vector <int>::iterator Iter1, new_end;

int i;

for(i = 0; i <= 9; i++)

vec1.push_back(i);

int j;

for(j = 0; j <= 2; j++)

vec1.push_back(4);

cout<<"Original vector vec1 data is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

random_shuffle(vec1.begin(), vec1.end());

cout<<"Random shuffle vector vec1 data is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// remove elements satisfying predicate greater than

new_end = remove_if(vec1.begin(), vec1.end(), greathan);

cout<<"Vector vec1 with elements greater than 5 removed is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// using erase, to change the sequence size,

vec1.erase(new_end, vec1.end());

cout<<"Vector vec1 resized elements greater than 5 removed is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

}

# replace()

• Examines each element in a range and replaces it if it matches a specified value.

`template<class ForwardIterator, class Type> void replace( ForwardIterator _First, ForwardIterator _Last,`
`						const Type& _OldVal, const Type& _NewVal );`

# Parameters

## Parameter

_First

_Last

_OldVal

_NewVal

#### Table 36.7

• The range referenced must be valid; all pointers must be de-referenceable and within the sequence the last position is reachable from the first by incrementation.

• The order of the elements not replaced remains stable.

• Theoperator== used to determine the equality between elements must impose an equivalence relation between its operands.

• The complexity is linear; there are (_Last – _First) comparisons for equality and at most (_Last – _First) assignments of new values.

// algorithm, replace()

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

int main()

{

vector <int> vec1;

vector <int>::iterator Iter1;

int i;

for(i = 0; i <= 9; i++)

vec1.push_back(i);

int j;

for(j = 0; j <= 2; j++)

vec1.push_back(5);

random_shuffle(vec1.begin(), vec1.end());

cout<<"The original random shuffle vector vec1 data is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// replace elements with other values…

replace (vec1.begin( ), vec1.end( ), 3, 23);

replace (vec1.begin( ), vec1.end( ), 7, 77);

replace (vec1.begin( ), vec1.end( ), 0, 21);

cout<<"The vector vec1 data with values replacement of 0, 3, 7 is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

}

# replace_copy()

• Examines each element in a source range and replaces it if it matches a specified value while copying the result into a new destination range.

template<class InputIterator, class OutputIterator, class Type>OutputIterator replace_copy( InputIterator _First, InputIterator _Last, OutputIterator _Result, const Type& _OldVal, const Type& _NewVal );

# Parameters

## Parameter

_First

_Last

_Result

_OldVal

_NewVal

#### Table 36.8

• The value return is an output iterator pointing to the position one past the final element in the destination range to where the altered sequence of elements is being copied.

• The source and destination ranges referenced must not overlap and must both be valid: all pointers must be de-referenceable and within the sequences the last position is reachable from the first by incrementation.

• The order of the elements not replaced remains stable.

• Theoperator== used to determine the equality between elements must impose an equivalence relation between its operands.

• The complexity is linear: there are (_Last – _First) comparisons for equality and at most (_Last – _First) assignments of new values.

// algorithm, replace_copy()

#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

int main()

{

vector <int> vec1;

list <int> lst1 (15);

vector <int>::iterator Iter1;

list <int>::iterator lstIter;

int i;

for (i = 0; i <= 9; i++)

vec1.push_back(i);

int j;

for (j = 0; j <= 3; j++)

vec1.push_back(7);

cout<<"The original vector vec1 data is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

random_shuffle(vec1.begin(), vec1.end());

int k;

for (k = 0; k <= 15; k++)

vec1.push_back(1);

cout<<"The original random shuffle vector vec1 with appended data is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// replace elements in one part of a vector with a value of 7 with a value of 70 and copy into another part of the vector

replace_copy(vec1.begin(), vec1.begin() + 14, vec1.end( )-15, 7, 70);

cout<<"The vector vec1 data with a replacement value of 7 is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// replace elements in a vector of a value 70 with a value of 1 and copy into a list

replace_copy(vec1.begin(), vec1.begin() + 14, lst1.begin(), 70, 1);

cout<<"The list copy lst1 of vec1 with the value 0 replacing the 7 is:\n";

for(lstIter = lst1.begin(); lstIter != lst1.end(); lstIter++)

cout<<*lstIter<<" ";

cout<<endl;

}

# replace_copy_if()

• Examines each element in a source range and replaces it if it satisfies a specified predicate while copying the result into a new destination range.

template<class InputIterator, class OutputIterator, class Predicate, class Type>OutputIterator replace_copy_if( InputIterator _First, InputIterator _Last, OutputIterator _Result, Predicate _Pred, const Type& _Val );

# Parameters

#### Table 36.9

• The source and destination ranges referenced must not overlap and must both be valid: all pointers must be de-referenceable and within the sequences the last position is reachable from the first by incrementation.

• The order of the elements not replaced remains stable.

• Theoperator== used to determine the equality between elements must impose an equivalence relation between its operands.

• The complexity is linear; there are (_Last – _First) comparisons for equality and at most (_Last – _First) assignments of new values.

// algorithm, replace_copy_if()

#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

bool greaterthan(int value)

{return value > 5;}

int main()

{

vector <int> vec1;

list <int> lst1 (13);

vector <int>::iterator Iter1;

list <int>::iterator lstIter1;

int i;

for (i = 0; i <= 9; i++)

vec1.push_back(i);

int j;

for (j = 0; j <= 3; j++)

vec1.push_back(7);

cout<<"The original vector vec1 data is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

random_shuffle(vec1.begin(), vec1.end());

int k;

for(k = 0; k <= 13; k++)

vec1.push_back(3);

cout<<"The original random shuffle vector vec1 data with appended data is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// replace elements with a value of 7 in the 1st half of a vector with a value of 72 and copy it into the 2nd half of the vector

replace_copy_if(vec1.begin(), vec1.begin() + 14, vec1.end() -14, greaterthan, 72);

cout<<"The vector vec1 with values of 72 replacing those greater"

<<"\n than 5 in the 1st half & copied into the 2nd half is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// replace elements in a vector with a value of 72 with a value of -8 and copy into a list

replace_copy_if(vec1.begin(), vec1.begin() + 13, lst1.begin(), greaterthan, -8);

cout<<"A list copy of vector vec1 with the value -8\n replacing "

<<"those greater than 5 is:\n";

for(lstIter1 = lst1.begin(); lstIter1 != lst1.end(); lstIter1++)

cout<<*lstIter1<<" ";

cout<<endl;

}

# replace_if()

• Examines each element in a range and replaces it if it satisfies a specified predicate.

`template<class ForwardIterator, class Predicate, class Type>`
`   void replace_if( ForwardIterator _First, ForwardIterator _Last, Predicate _Pred, const Type& _Val );`

# Parameters

## Parameter

_First

_Last

_Pred

_Val

#### Table 36.10

• The range referenced must be valid; all pointers must be de-referenceable and within the sequence the last position is reachable from the first by incrementation.

• The order of the elements not replaced remains stable.

• The algorithm replace_if() is a generalization of the algorithm replace(), allowing any predicate to be specified, rather than equality to a specified constant value.

• Theoperator== used to determine the equality between elements must impose an equivalence relation between its operands.

• The complexity is linear: there are (_Last – _First) comparisons for equality and at most (_Last – _First) assignments of new values.

// algorithm, replace_if()

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

bool greaterthan(int value)

{return value > 4;}

int main()

{

vector <int> vec1;

vector <int>::iterator Iter1;

int i;

for (i = 1; i <= 10; i++)

vec1.push_back(i);

int j;

for (j = 0; j <= 2; j++)

vec1.push_back(8);

random_shuffle(vec1.begin(), vec1.end());

cout<<"The original random shuffle vector vec1 data is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// replace elements satisfying the predicate greaterthan with a value of 21

replace_if(vec1.begin(), vec1.end(), greaterthan, 21);

cout<<"The vector vec1 with a value 21 replacing those\n "

<<"elements satisfying the greater than 4 predicate is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

}

# reverse()

• Reverses the order of the elements within a range.

`template<class BidirectionalIterator> void reverse( BidirectionalIterator _First, BidirectionalIterator _Last );`

# Parameters

## Parameter

_First

_Last

#### Table 36.11

• The source range referenced must be valid; all pointers must be de-referenceable and within the sequence the last position is reachable from the first by incrementation.

// algorithm, reverse()

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

int main()

{

vector <int> vec1;

vector <int>::iterator Iter1;

int i;

for(i = 11; i <= 20; i++)

vec1.push_back(i);

cout<<"The original vector vec1 is:\n ";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// reverse the elements in the vector

reverse(vec1.begin(), vec1.end());

cout<<"The vector vec1 data with values reversed is:\n";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

}

# Output:

tenouk C++ STL tutorial

# Further C++ STL algorithm related reading:

1. Source code for this tutorial is available inC++ STL Algorithm source code.

2. C++ Templates programming tutorials.

3. Check thebest selling C / C++ and STL books at Amazon.com.

4. Acomplete C Standard Library.

5. Acomplete C++ Standard Library documentation that includes STL.