pickzy.com

C  |  C++  |  Objective-C  |  VC++  |  Win32  |  MFC  |  Java  |  Php  |  Delphi  |  Visual Basic  |  .Net  |  Networking  |  General  |  Games  |  Jobs  |  Javascript  |  




Menu

pickSourcecode.com


        

 




 

Cpp > Programs

 

ReverseCompare cpp example

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;


//
//  Unary  function  to  output  its  argument  to  cout
//
template<class T>

class Print: public unary_function<T, 
void>
{

public:

void operator()(T& arg1)
{

cout << arg1 << " ";

}

};


//
//  Compare  function
//
bool ReverseCompare(
int arg1, int arg2);

//
//  Display  all  elements  in  a  container
//
template<class Container>

void ShowElements(Container& c, char* text);

int main()
{

typedef vector<int> VectorInt;

typedef VectorInt::iterator Itor;


//  create  an  integer  vector
VectorInt vInt1(7);

Itor first1 = vInt1.begin();

Itor last1  = vInt1.end();

generate(first1, last1, rand);

ShowElements(vInt1, "Random number sequence");


//  sort  the  sequence  in  descending  order
nth_element(first1, first1 + 1, last1, ReverseCompare);

ShowElements(vInt1, "After nth_element()");


return 0;

}


//
//  Compare  function
//
bool ReverseCompare(
int arg1, int arg2)
{

return (arg1 > arg2);

}


//
//  Display  all  elements  in  a  container
//
template<class Container>

void ShowElements(Container& c, char* text)
{

Print<Container::value_type> DoPrint;


cout << text << ":\n";

for_each(c.begin(), c.end(), DoPrint);
cout << "\n\n";

}

 
Privacy Policy | About Us