C++ STL algorithm, min() program 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++ min() member to compare two objects and returns the lesser 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, min() to compare two objects and returns the lesser of the two, where the ordering criterion may be specified by a binary predicate in C++ programming

// C++ STL algorithm, min()

#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 less than modulus of elem2

bool mod_lesser(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 min algorithm with binary predicate mod_lesser & with default less than

int a = 9, b = -12, c = 12;

const int &result1 = min(a, b, mod_lesser);

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

cout<<"The mod_lesser of the integers 9 and -12 is: "<<result1<<endl;

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

cout<<endl;

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

CInt ci1 = 2, ci2 = 3, ci3 = 4;

// containers

set<CInt> st1, st2, st3;

// iterators

set<CInt>::iterator st1Iter, st2Iter, st3Iter;

// pushing data

st1.insert(ci1);

st1.insert(ci2);

st2.insert(ci2);

st2.insert(ci3);

// printing data

cout<<"st1 set data: ";

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

cout<<*st1Iter<<",";

st1Iter = --st1.end();

cout<<*st1Iter<<endl;

cout<<"st2 set data: ";

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

cout<<*st2Iter<<",";

st2Iter = --st2.end();

cout<<*st2Iter<<endl;

st3 = min(st1, st2);

cout<<"st3 = min(st1, st2) data: ";

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

cout<<*st3Iter<<",";

st3Iter = --st3.end();

cout<<*st3Iter<<endl;

// comparing vectors with integer elements using min() 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 = 1; i <= 4; i++)

vec1.push_back(i);

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

vec2.push_back(j);

for(k = 1; k <= 3; 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;

cout<<"\nOperation: vec4 = min(vec1, vec2)"<<endl;

vec4 = min(vec1, vec2);

cout<<"vec4 vector = min(vec1,vec2) data: ";

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

cout<<*Iter4<<" ";

cout<<endl;

cout<<"\nOperation: vec5 = min(vec1, vec3)"<<endl;

vec5 = min(vec1, vec3);

cout<<"vec5 vector = min(vec1,vec3) data: ";

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

cout<<*Iter5<<" ";

cout<<endl;

return 0;

}

Output examples:

The mod_lesser of the integers 9 and -12 is: 9

The lesser of the integers -12 and 12 is: -12

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

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

st3 = min(st1, st2) data: CInt(2),CInt(3)

vec1 vector data: 1 2 3 4

vec2 vector data: 1 2 3

vec3 vector data: 2 4 6

Operation: vec4 = min(vec1, vec2)

vec4 vector = min(vec1,vec2) data: 1 2 3

Operation: vec5 = min(vec1, vec3)

vec5 vector = min(vec1,vec3) data: 1 2 3 4

Press any key to continue . . .