C++ STL algorithm, min_element() code sample

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_element() to find the first occurrence of smallest element in a specified range where the ordering criterion may be specified by a binary predicate in C++ programming

To show: How to use the C++ algorithm, min_element() to find the first occurrence of smallest element in a specified range where the ordering criterion may be specified by a binary predicate in C++ programming

// C++ STL algorithm, min_element()

#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_lesser(int elem1, int elem2)

{

if(elem1 < 0)

elem1 = - elem1;

if(elem2 < 0)

elem2 = - elem2;

return (elem1 < elem2);

};

int main(void)

{

// searching a set container with elements of type CInt for the minimum element

CInt ci1 = 4, ci2 = 12, ci3 = -4;

// set container

set<CInt> st1;

// set iterators

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

// pushing data

st1.insert(ci1);

st1.insert(ci2);

st1.insert(ci3);

// print the data

cout<<"st1 set data: ";

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

cout<<*st1Iter<<",";

st1Iter = --st1.end();

cout<<*st1Iter<<endl;

cout<<"\nOperation: min_element(st1.begin(), st1.end())"<<endl;

st2Iter = min_element(st1.begin(), st1.end());

cout<<"The smallest element in st1 set is: "<<*st2Iter<<endl;

// searching a vector with elements of type int for the maximum element under default less than & mod_lesser() binary predicates

//

// container

vector <int> vec1;

// iterators

vector <int>::iterator vec1Iter, vec2Iter, vec3Iter;

int i, j;

// pushing data in ranges

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

vec1.push_back(i);

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

vec1.push_back(-2*j);

// print the data

cout<<"\nvec1 vector data: ";

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

cout<<*vec1Iter<<" ";

cout<<endl;

cout<<"\nOperation: min_element(vec1.begin(), vec1.end())"<<endl;

vec2Iter = min_element(vec1.begin(), vec1.end());

cout<<"The smallest element in vec1 vector is: "<<*vec2Iter<<endl;

cout<<"\nOperation: min_element(vec1.begin(), vec1.end(), mod_lesser)"<<endl;

vec3Iter = min_element(vec1.begin(), vec1.end(), mod_lesser);

cout<<"The smallest element in vec1 vector based on the mod_lesser binary predicate is: "<<*vec3Iter<<endl;

return 0;

}

Output examples:

st1 set data: CInt(-4),CInt(4),CInt(12)

Operation: min_element(st1.begin(), st1.end())

The smallest element in st1 set is: CInt(-4)

vec1 vector data: 1 2 3 4 -2 -4 -6 -8 -10

Operation: min_element(vec1.begin(), vec1.end())

The smallest element in vec1 vector is: -10

Operation: min_element(vec1.begin(), vec1.end(), mod_lesser)

The smallest element in vec1 vector based on the mod_lesser binary predicate is: 1

Press any key to continue . . .