previous | start | next

A template version of the find function

template <class Iterator, class T>
bool find(Iterator start, Iterator limit, const T& x)
{
  for(Iterator p = start; p != limit; p++) {
     if ( *p == x ) {
         return true;
     }
  }
  return false;
}

int main()
{
    int a[] = {1,2,3,5,8,13,21,34};  // int array with 8 elements
    vector z(a, a + 8);              // vector initialized with 
                                     // the 8 elements from a.
    vector::iterator startz = z.begin();
    vector::iterator limitz = z.end();

    bool ba, bz;

    ba = find(a, a + 8, 21);     // ba will be set to true
    bz = find(start, limitz, 21) // bz will also be set to true



previous | start | next