Can someone explain a couple lines of code for me?

Hi, I'm preparing for an exam and am reviewing some code. I have a completed template function that I'm trying to understand and would appreciate if someone explained how it is working. I don't need too much detail about loops or code structure, I just want to understand how the function is accomplishing the description.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  // Returns true just if target occurs among the elements of the container 
  // accessed via the forward iterators start up to stop.

template <typename I, typename T>
bool find(I start, I stop, T target)
{
   I iter = start;
     while(iter!= stop)
       {
          if(*iter == target)
             {
                return true;
             }
          ++iter;
       }
     return false;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  // Returns true just if target occurs among the elements of the container 
  // accessed via the forward iterators start up to stop.

template <typename I, typename T>
bool find(I start, I stop, T target)
{
   I iter = start; //Set the I iterator to be START
     while(iter!= stop) //WHILE start isn't at the end (STOP), then proceed
       {
          if(*iter == target) //IF the position that start is in is == to whatever the TARGET is...
             {
                return true;   //Return TRUE
             }
          ++iter;               //Increase the iterator to the next position..
       }
     return false;            //If START is at the end (I.e. START ==STOP), return false, we're done
}
Last edited on
Topic archived. No new replies allowed.