#pragma warning (disable : 4786)
#include <iostream>
#include <vector>
#include <iterator>
#include <functional>
#include <algorithm>
using namespace std;
const int VectorSize = 10;
template<class T>
class Print: public unary_function<T, void>
{
public:
void operator()(T& arg1)
{
cout << arg1 << " ";
}
};
template<class T>
class GreaterThanTwo: public unary_function<T, bool>
{
public:
bool operator()(T& arg1)
{
return (arg1 > 2);
}
};
template<class T>
class WithinOne: public binary_function<T, T, bool>
{
public:
bool operator()(T& arg1, T& arg2)
{
return (abs(arg2 - arg1) <= 1);
}
};
template<class Container, class Iterator>
void SequenceRange(Container& c, Iterator& first, Iterator& last);
template<class Container, class Iterator>
void ShowElement(Container& c, Iterator& itor);
int main()
{
Print<int> DoPrint;
vector<int> vInt(VectorSize);
typedef vector<int>::iterator Itor;
for (int i = 0; i < VectorSize; ++i)
vInt[i] = i + i / 3;
Itor first = vInt.begin();
Itor last = vInt.end();
cout << "for_each()\n";
for_each(first, last, DoPrint);
cout << "\n";
Itor retItor = find(first, last, 2);
cout << "find(first, last, 2) = ";
ShowElement(vInt, retItor);
cout << "\n";
retItor = find(first, last, 10);
cout << "find(first, last, 10) = ";
ShowElement(vInt, retItor);
cout << "\n";
GreaterThanTwo<int> IsGreaterThanTwo;
retItor = find_if(first, last, IsGreaterThanTwo);
cout << "find(first, last, IsGreaterThanTwo) = ";
ShowElement(vInt, retItor);
cout << "\n";
int retSize = count(first, last, 3);
cout << "count_if(first, last, 3) = " << retSize << "\n";
retSize = count_if(first, last, IsGreaterThanTwo);
cout << "count_if(first, last, IsGreaterThanTwo) = " << retSize << "\n";
retItor = adjacent_find(first, last);
cout << "adjacent_find(first, last) = ";
ShowElement(vInt, retItor);
cout << "\n";
WithinOne<int> IsWithinOne;
retItor = adjacent_find(first, last, IsWithinOne);
cout << "adjacent_find(first, last, IsWithinOne) = ";
ShowElement(vInt, retItor);
cout << "\n";
vInt[3] = 2;
retItor = adjacent_find(first, last);
cout << "adjacent_find(first, last) = ";
ShowElement(vInt, retItor);
cout << "\n";
return 0;
}
template<class Container, class Iterator>
void ShowElement(Container& c, Iterator& itor)
{
if (itor != c.end())
cout << *itor;
else
cout << "last";
}
template<class Container, class Iterator>
void SequenceRange(Container& c, Iterator& first, Iterator& last)
{
first = c.begin();
last = c.end();
}