STL algorithms
The C++ STL contains several standard generic algorithms which
can be used to process the elements of a container. Such standard
services such as sorting, searching, copying and modifying are provided.
To use the STL aglorithms, #include the header file <algorithm>.
Modifying algorithms
The two basic algorithms used to modify the elements of a container
are for_each( ) and transform( ).
They're similar, but differ slightly in their behaviors.
for_each( ) takes a range of elements in the container as specified
by a pair of iterators and a function that might modify its argument.
It passes each element in the range to the function.
void square( int &n ) // pass by reference
{
n = n * n;
}
void print( int n)
{
cout << n << endl;
}
list myList; // or any other container
// put some elements in the list
// put some elements in the list
myList.push_front( 42 );
myList.push_front( 4 );
myList.push_front( 2 );
myList.push_back( 99 );
myList.push_back( 17 );
// square each int
for_each( myList.begin(), myList.end(), square );
// print them out
for_each( myList.begin(), myList.end(), print );
transform( ) takes a range of elements in the container as specified
by a pair of iterators, an iterator which is the destination of the
transformation and a function that returns a modified version
of its argument.
int square( int n ) // by value
{
return n * n;
}
vector v1(15);
vector v2(15);
// put some elements into v1
v1.push_back( 12 );
v1.push_back( 43 );
v1.push_back( 77 );
// use transform( ) to put the squares of v1's elements
// into v2. Note that v2 must have sufficient capacity
transform( v1.begin(), v1.end(), v2.begin(), square);
// or use transform( ) in place of for_each by specifying
// the destination to be the same as the source
transform( v1.begin(), v1.end(), v1.begin(), square);
Two relatively simple to use modifying algorithms are
fill( ), replace( ), both which have variations
not shown here.
fill( ) is used to replace each element with
a given value. Here's a short test program with the
output it produces.
#include
#include
#include
#include
using namespace std;
int main ( )
{
vector< string > vString;
// store some strings into vString
vString.push_back( "steve" );
vString.push_back( "mary" );
vString.push_back( "john" );
vString.push_back( "sue" );
vString.push_back( "bob" );
vString.push_back( "steve" );
vector::const_iterator position;
cout << "Initial vString\n";
for (position = vString.begin(); position != vString.end(); ++position)
cout << *position << endl;
cout << endl;
// change vString[1] & vString[2] to "tommy"
fill( vString.begin() + 1, vString.begin() + 3, "tommy");
cout << "After changing vString[1] and vString[2] to \"tommy\" \n";
for (position = vString.begin(); position != vString.end(); ++position)
cout << *position << endl;
cout << endl;
// change "steve" to "bill" everywhere in the vector
replace( vString.begin(), vString.end(), string("steve"), string("bill"));
cout << "After replacing \"steve\" with \"bill\" \n";
for (position = vString.begin(); position != vString.end(); ++position)
cout << *position << endl;
return 0;
}
Initial vString
steve
mary
john
sue
bob
steve
After changing vString[1] and vString[2] to "tommy"
steve
tommy
tommy
sue
bob
steve
After replacing "steve" with "bill"
bill
tommy
tommy
sue
bob
bill
Sorting
The STL supports several sorting algorithms. The simplest
is sort( ) which uses the quicksort
algorithm. Note that sort( ) requires random access
to the container, so is not usable on all STL containers.
vector v1;
// put some elements into v1
// sort the elements
sort( v1.begin(), v1.end());
Last Modified: Monday, 28-Aug-2006 10:16:07 EDT