|< C++ STL Algorithm 10 | Main | C++ STL Algorithm 12 >| Site Index | Download |


 

 

MODULE 36_1

THE C++ STL ALGORITHM PART 11

 

 

 

 

My Training Period: xx hours

 

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

 

C++ STL algorithm abilities that supposed to be acquired:

 

         Able to understand and use the member functions of the algorithm.

         Able to understand how the containers, iterators and algorithm work.

 

What do we have in this page?

  1. Algorithm, remove_if() program example

  2. Algorithm, replace() program example

  3. Algorithm, replace_copy() program example

  4. Algorithm, replace_copy_if() program example

  5. Algorithm, replace_if() program example

  6. Algorithm, reverse() program example

 

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

Description

_First

A forward iterator pointing to the position of the first element in the range from which elements are being removed.

_Last

A forward iterator pointing to the position one past the final element in the range from which elements are being removed.

_Pred

The unary predicate that must be satisfied is the value of an element is to be replaced.

 

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;

}

 

Output:

 

C++ STL Algorithm remove_if() program example

 

replace()

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

 

Parameters

 

Parameter

Description

_First

A forward iterator pointing to the position of the first element in the range from which elements are being replaced.

_Last

A forward iterator pointing to the position one past the final element in the range from which elements are being replaced.

_OldVal

The old value of the elements being replaced.

_NewVal

The new value being assigned to the elements with the old value.

 

Table 36.7

// 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;

}

 

Output:

 

C++ STL Algorithm replace() program example

 

replace_copy()

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

Parameters

 

Parameter

Description

_First

An input iterator pointing to the position of the first element in the range from which elements are being replaced.

_Last

An input iterator pointing to the position one past the final element in the range from which elements are being replaced.

_Result

An output iterator pointing to the first element in the destination range to where the altered sequence of elements is being copied.

_OldVal

The old value of the elements being replaced.

_NewVal

The new value being assigned to the elements with the old value.

 

Table 36.8

// 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;

}

 

Output:

 

 

-----------------------------------------------------------------------

C++ STL Algorithm replace_copy() program example

 

replace_copy_if()

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

 

Parameter

Description

_First

An input iterator pointing to the position of the first element in the range from which elements are being replaced.

_Last

An input iterator pointing to the position one past the final element in the range from which elements are being replaced.

_Result

An output iterator pointing to the position of the first element in the destination range to which elements are being copied.

_Pred

The unary predicate that must be satisfied is the value of an element is to be replaced.

_Val

The new value being assigned to the elements whose old value satisfies the predicate.

 

Table 36.9

// 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;

}

 

Output:

 

C++ STL Algorithm replace_copy_if() program example

-------------------------------------------------------------------

 

 

replace_if()

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

 

Parameters

 

Parameter

Description

_First

A forward iterator pointing to the position of the first element in the range from which elements are being replaced.

_Last

An iterator pointing to the position one past the final element in the range from which elements are being replaced.

_Pred

The unary predicate that must be satisfied is the value of an element is to be replaced.

_Val

The new value being assigned to the elements whose old value satisfies the predicate.

 

Table 36.10

// 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;

}

 

Output:

 

C++ STL Algorithm replace_if() program example

 

reverse()

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

 

Parameters

 

Parameter

Description

_First

A bidirectional iterator pointing to the position of the first element in the range within which the elements are being permuted.

_Last

A bidirectional iterator pointing to the position one past the final element in the range within which the elements are being permuted.

 

Table 36.11

// 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:

 

C++ STL Algorithm reverse() program example

 

tenouk C++ STL tutorial

 

   

 

Further C++ STL algorithm related reading:

 

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

  2. C++ Templates programming tutorials.

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

  4. A complete C & C++ Standard Library documentation that includes STL.

 

 

 

 

|< C++ STL Algorithm 10 | Main | C++ STL Algorithm 12 >| Site Index | Download |


C++ STL Algorithm Classes:  Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8 | Part 9 | Part 10 | Part 11 | Part 12 |

Part 13 | Part 14 | Part 15

2003-2007 © Tenouk. All rights reserved.