C++ STL algorithm, max() code example

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

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: Using the C++ max() to compare two objects and returns the larger of the two, where the ordering criterion may be specified by a binary predicate in C++ programming

To show: How to use the C++ algorithm, max() to compare two objects and returns the larger of the two, where the ordering criterion may be specified by a binary predicate in C++ programming

// C++ STL algorithm, max()

#include <vector>

#include <set>

#include <algorithm>

#include <iostream>

using namespace std;

class CInt;

ostream& operator<<(ostream& osIn, const CInt& rhs);

class CInt

{

public:

CInt(int n = 0) : m_nVal(n){}

CInt(const CInt& rhs) : m_nVal(rhs.m_nVal){}

CInt& operator=(const CInt& rhs)

{m_nVal = rhs.m_nVal; return *this;}

bool operator<( const CInt& rhs ) const

{return (m_nVal < rhs.m_nVal);}

friend ostream& operator<<(ostream& osIn, const CInt& rhs);

private:

int m_nVal;

};

inline ostream& operator<<(ostream& osIn, const CInt& rhs)

{

osIn<<"CInt("<<rhs.m_nVal<<")";

return osIn;

}

// return whether modulus of elem1 is greater than modulus of elem2

bool mod_greater(int elem1, int elem2)

{

if(elem1 < 0)

elem1 = - elem1;

if(elem2 < 0)

elem2 = - elem2;

return (elem1 > elem2);

};

int main(void)

{

// comparing integers directly using the max algorithm

int a = 11, b = -12, c = 20;

const int& result1 = max(a, b, mod_greater);

const int& result2 = max(b, c);

cout<<"The mod_greater() of the integers 11 and -12 is: "<<result1<<endl;

cout<<"The larger of the integers -12 and 20 is: "<<result2<<endl;

cout<<endl;

// comparing set containers with elements of type CInt using the max algorithm

CInt c1 = 1, c2 = 2, c3 = 3;

// set containers

set<CInt> st1, st2, st3;

// set iterators

set<CInt>::iterator st1_Iter, st2_Iter, st3_Iter;

// pushing/inserting data

st1.insert(c1);

st1.insert(c2);

st2.insert(c2);

st2.insert(c3);

// printing the data

cout<<"st1 set data: ";

for(st1_Iter = st1.begin(); st1_Iter != --st1.end(); st1_Iter++)

cout<<*st1_Iter<<",";

st1_Iter = --st1.end();

cout<<*st1_Iter<<endl;

cout<<"st2 set data: ";

for(st2_Iter = st2.begin(); st2_Iter != --st2.end(); st2_Iter++)

cout<<*st2_Iter<<",";

st2_Iter = --st2.end();

cout<<*st2_Iter<<endl;

st3 = max(st1, st2);

cout<<"st3 = max(st1, st2) = ";

for(st3_Iter = st3.begin(); st3_Iter != --st3.end(); st3_Iter++)

cout<<*st3_Iter<<",";

st3_Iter = --st3.end();

cout<<*st3_Iter<<endl;

// comparing vectors with integer elements using the max() algorithm

//

// containers

vector <int> vec1, vec2, vec3, vec4, vec5;

// iterators

vector <int>::iterator Iter1, Iter2, Iter3, Iter4, Iter5;

int i, j, k;

// pushing data in ranges

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

vec1.push_back(i);

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

vec2.push_back(j);

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

vec3.push_back(2*k);

// printing data

cout<<"\nvec1 vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

cout<<"vec2 vector data: ";

for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

cout<<"vec3 vector data: ";

for(Iter3 = vec3.begin(); Iter3 != vec3.end(); Iter3++)

cout<<*Iter3<<" ";

cout<<endl;

vec4 = max(vec1, vec2);

vec5 = max(vec1, vec3);

cout<<"vec4 vector = max(vec1,vec2) is: ";

for(Iter4 = vec4.begin(); Iter4 != vec4.end(); Iter4++)

cout<<*Iter4<<" ";

cout<<endl;

cout<<"vec5 vector = max(vec1,vec3) is: ";

for(Iter5 = vec5.begin(); Iter5 != vec5.end(); Iter5++)

cout<<*Iter5<<" ";

cout<<endl;

return 0;

}

Output examples:

The mod_greater() of the integers 11 and -12 is: 11

The larger of the integers -12 and 20 is: 20

st1 set data: CInt(1),CInt(2)

st2 set data: CInt(2),CInt(3)

st3 = max(st1, st2) = CInt(2),CInt(3)

vec1 vector data: 0 1 2 3

vec2 vector data: 0 1 2 3 4

vec3 vector data: 0 2 4

vec4 vector = max(vec1,vec2) is: 0 1 2 3 4

vec5 vector = max(vec1,vec3) is: 0 2 4

Press any key to continue . . .