Write a template function that returns the largest value and

Write a template function that returns the largest value and the smallest value(use call-by reference) stored in a vector, the function should also returns a new list which stores all of the values between two immediately adjacent values in the original vector. Assume the operator

Solution

#include <iostream>
#include <vector>
using namespace std;

template<typename T>
void myFunc( vector<T> input, vector<T> &minMax, vector<T> &reqList ){
   typename vector<T>::iterator it = input.begin();
   minMax.clear();
   reqList.clear();
   minMax.push_back( *it ); //minimum
   minMax.push_back( *it ); //maximum
   for( ; it != input.end(); it++ ){
       if( *it < minMax[0] ){ minMax[0] = *it; }
       if( *it > minMax[1] ){ minMax[1] = *it; }

       if( it + 1 != input.end() ){
           int left = *it;
           int right = *(it+1);
           if( left > right ){
               int temp = left;
               left = right;
               right = temp;
           }
           left++;          
           for(; left < right; left++){
               reqList.push_back( left );
           }
       }
   }
}

int main(){

   vector<int> input;

   input.push_back(5);
   input.push_back(2);
   input.push_back(3);
   input.push_back(6);
   vector<int> minMax, reqList;

   myFunc<int>(input,minMax,reqList);
   cout << \"Min: \" << minMax[0] << endl;
   cout << \"Max: \" << minMax[1] << endl;
   for(int i =0 ; i <reqList.size(); i++){
       cout << reqList[i] << \" \";
   } cout << endl;
}

 Write a template function that returns the largest value and the smallest value(use call-by reference) stored in a vector, the function should also returns a n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site