[ create a new paste ] login | about

Link: http://codepad.org/9RsCb0Gl    [ raw code | output | fork ]

C++, pasted on Jan 26:
#include <algorithm>
#include <functional>
#include <iostream>

template <typename T>
struct in_range : public std::unary_function<T, bool>
{
	explicit in_range(const T& _min, const T& _max) : min_(_min), max_(_max) {}
	bool operator()(const T& v) const { return (v >= min_ && v <= max_); }

	const T min_;
	const T max_;
};

template <class _FwdIt, class _Pred>
_FwdIt max_element_if(_FwdIt _First, _FwdIt _Last, _Pred _Pred1)
{
	typedef typename std::iterator_traits<_FwdIt>::value_type value_type;
	
	_FwdIt _Found = _First = std::find_if(_First, _Last, _Pred1);
	if (_First != _Last)
	{
		for (; ++_First != _Last; )
			if (_Pred1(*_First))
				if (*_First > *_Found)
					_Found = _First;
	}

	return (_Found);
}

int main()
{
   const size_t size = 10;
   int mass[size] = {-7,-5,-4,-3,-2,-1,0,1,2,3};
   std::cout << *max_element_if(mass, mass+size, in_range<int>(-5, 0));
   return 0;
}


Output:
1
0


Create a new paste based on this one


Comments: