Skip to content

Instantly share code, notes, and snippets.

@sinannar
Created January 19, 2012 15:18
Show Gist options
  • Save sinannar/1640560 to your computer and use it in GitHub Desktop.
Save sinannar/1640560 to your computer and use it in GitHub Desktop.
recursive template function that find smallest one of an array
#include<iostream>
using namespace std;
template<class T>
T findSmallest(T* arr,int size)
{
if(size==1)
return arr[0];
else{
if(arr[0]<findSmallest(&arr[1],size-1))
return arr[0];
else return findSmallest(&arr[1],size-1);
}
}
int main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
char b[10]={'-','b','c','d','e','f','g','h','i','j'};
cout<<findSmallest(a,10)<<endl;
cout<<findSmallest(b,10)<<endl;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment