数据结构与算法分析课后习题第三章(3)

Exercises 3.3 Implement the STL find routine that returns the iterator containing the first occurrence of x in the range that begins at start and extends up to but not include end. If x is not found, end is returned. This is a non-class(global function) with signature:

template

Iterator find(Iterator start, Iterator end, const Object &x)

//find.hpp

#ifndef FIND_HPP__
#define FIND_HPP__

template
Iterator find(Iterator start, Iterator end, const Object& x)
{
 while(start != end)
 {
  if(*start == x)
   return start;
  ++start;
 }

 return end;
}

#endif

 

//main.cpp

#include "find.hpp"
#include
#include
#include

using namespace std;

int main()
{
 int a[] = {0, 1, 2, 3, 4, 5, 6};
 list lst(a, a + 7);
 vector vec(a, a + 7);

 std::cout << *find(lst.begin(),find(lst.begin(), lst.end(), 2), 3) << "/n";
 std::cout << *find(vec.begin(), vec.end(), 4);

 return 0;
}

你可能感兴趣的:(c++,数据结构,数据结构,算法,iterator,object,iostream,vector)