Chapter Index | Slide Index | Frames [ Windows ] | Narrative On | <- prev | next -> |

Generic Find (Sequential Search)

template <class I, typename T>
I g_find (I beg, I end, const T& t)
// classic sequential search
{
  for ( ; beg != end; ++beg)
    if (t == *beg)
      return beg;
  return end;
}

// usage:
Litr = g_find (L.Begin(), L.End(), t) // location of t in List L
Vitr = g_find (V.Begin(), V.End(), t) // location of t in Vector V
Ditr = g_find (D.Begin(), D.End(), t) // location of t in Deque D
Aitr = g_find (A, A + size, t)        // location of t in array A
// note that return of End() iterator (invalid iterator) means "not found"

<- prev | next -> | Top of Page | 8. Generic Algorithms - 3 of 12