| | | | | |

Generic Algorithms and Predicate Objects

  • Function classes may be passed to generic algorithm as template parameter
  • Commonly used to pass predicates such as LessThan
template <class I, class P>
I g_max_element (I beg, I end, const P& p)
{
  if (beg == end)
    return end;
  I max(beg);
  while (++beg != end)
    if (p(*max, *beg))
      max = beg;
  return max;
}
    // usage:
    GreaterThan <sometype> gt;
    Vitr = g_max_element (V.Begin(), V.End(), gt)
    // min element of V = *Vitr
    Litr = g_max_element (L.Begin(), L.End(), gt)
    // min element of L = *Litr
    Ditr = g_max_element (D.Begin(), D.End(), gt)
    // min element of D = *Ditr
    Aitr = g_max_element (A, A + size, gt)
    // min element of A = *Aitr
    
    

| | Top of Page | 8. Generic Algorithms - 5 of 12