Write a template function that returns the largest value and
Solution
#include<iostream>
#include<vector>
//change Type to type of vector you want , if you want float type just change int to float
#define Type int
using namespace std;
template<typename type>
void largeAndSmall(std::vector<type> &a,type &tmin, type &tmax)
{
tmax = a[0] , tmin = a[0];
for ( int i = 0 ; i < a.size() ; i++ )
{
if( a[i] < tmin )
tmin = a[i] ;
if( a[i] > tmax )
tmax = a[i] ;
}
}
int main()
{
std::vector <Type> int_arr;
int_arr.push_back(3);
int_arr.push_back(0);
int_arr.push_back(9);
int_arr.push_back(-8);
int_arr.push_back(-1);
cout<<\"Elements of vector are: \";
for ( int i = 0 ; i < int_arr.size() ; i++ )
{
cout<<int_arr[i]<<\"\\t\";
}
cout<<endl;
//print largest and smallest element in a vector
Type max,min;
largeAndSmall(int_arr,min,max);
cout<<\"Max = \"<<max<<\"\\t\"<<\"Min = \"<<min<<endl;
}
