C++ STL algorithm, max_element() code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

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

 

// C++ STL algorithm, max_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 maximum element

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

// container

set<CInt> st1;

// iterators

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

 

// pushing data

st1.insert(c1);

st1.insert(c2);

st1.insert(c3);

 

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

st2_Iter = max_element(st1.begin(), st1.end());

cout<<"The largest element in st1 set is: "<<*st2_Iter<<endl;

cout<<endl;

// searching a vector with elements of type int for the maximum

// element under default less than & mod_lesser binary predicates

//

// container

vector <int> vec;

// iterators

vector <int>::iterator vec_Iter, vec1_Iter, vec2_Iter;

int i, j;

 

// pushing data

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

vec.push_back(i);

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

vec.push_back(-j);

 

// printing data

cout<<"vec vector data: ";

for(vec_Iter = vec.begin(); vec_Iter != vec.end(); vec_Iter++)

cout<<*vec_Iter<<" ";

cout<<endl;

vec1_Iter = max_element(vec.begin(), vec.end());

vec2_Iter = max_element(vec.begin(), vec.end(), mod_lesser);

cout<<"The largest element in vec vector is: "<<*vec1_Iter<<endl;

cout<<"The largest element in vec vector under the mod_lesser() binary predicate is: "<<*vec2_Iter<<endl;

return 0;

}

 

Output examples:

 

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

The largest element in st1 set is: CInt(2)

vec vector data: 0 1 2 3 -1 -2 -3 -4

The largest element in vec vector is: 3

The largest element in vec vector under the mod_lesser() binary predicate is: -4

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index