Skip to content

Latest commit

 

History

History
22 lines (12 loc) · 2.6 KB

CppFunctorSameXandY.md

File metadata and controls

22 lines (12 loc) · 2.6 KB

A functor that sees if a certain class has equal X and Y values.

 


#include <cassert> #include <functional> #include <vector> #include <algorithm> struct Player {   Player(const int anyX, const int anyY) : x(anyX), y(anyY) {}   int x,y;   //And more... }; //From http://www.richelbilderbeek.nl/CppFunctorSameXandY.htm template <class T> struct SameXandY : public std::unary_function<T,bool> {   explicit SameXandY(const T& position) : mPosition(position) {}   bool operator()(const T& position) const   {     return position.x == mPosition.x && position.y == mPosition.y;   }   private:   const T mPosition; }; int main() {   std::vector<Player> v;   for (int i=0; i!=10; ++i) v.push_back(Player(i,i));   assert( std::find_if(v.begin(), v.end(), SameXandY<Player>(Player(3,5))) == v.end());   assert( std::find_if(v.begin(), v.end(), SameXandY<Player>(Player(5,5))) != v.end()); }