Display 7.10 shows a function called search, which searches an array for a
specified integer. Give a function template version of search that can be
used to search an array of elements of any type. Give both the function
declaration and the function definition for the template. Hint: It is almost
identical to the function given in Display 7.10.
Function Declaration:
template<class BaseType>
int search(const BaseType a[],
int number_used, BaseType target);
//Precondition: number_used is <= the declared size of a.
//Also, a[0] through a[number_used − 1] have values.
//Returns the first index such that a[index] == target,
//provided there is such an index; otherwise, returns − 1.
Definition:
template<class BaseType>
int search(const BaseType a[], int number_used,
BaseType target)
{
int index = 0, found = false;
while ((!found) && (index < number_used))
if (target == a[index])
found = true;
else
index++;
if (found)
return index;
else
return − 1;
}