template function for find a value : Function Template « Function « C++






template function for find a value

template function for find a value
 
#include <iostream>
#include <cstring>
using namespace std;

template <class X> int find(X object, X *list, int size)
{
  int i;

  for(i = 0; i <size; i++)
    if(object == list[ i ]) 
       return i;
  return -1;
}

int main()
{
  int a[] = {1, 2, 3, 4};
  char *c = "this is a test";
  double d[] = {1.1, 2.2, 3.3};

  cout << find(3, a, 4);
  cout << endl;
  cout << find('a', c, (int) strlen(c));
  cout << endl;
  cout << find(0.0, d, 3);

  return 0;
}



           
         
  








Related examples in the same category

1.A generic mode finding function.A generic mode finding function.
2.Function template: swap valuesFunction template: swap values
3.Simple template function to accept two parametersSimple template function to accept two parameters
4.Creating a custom algorithm based on templateCreating a custom algorithm based on template
5.find all template function
6.Using a Binary Function to Multiply Two Ranges
7.Making a Sequence of Random Numbers
8.write function object
9.Use a Function Object to Hold state
10.template function for bubble sort
11.template function for compacting the items
12.Template copy array function
13.function template for getting the max value
14.Overriding a template function.
15.Using Standard Parameters with Template Functions