Jump to content

function template

- - - - -

  • Please log in to reply
1 reply to this topic

#1
wilk_u

wilk_u

    Newbie

  • Members
  • Pip
  • 1 posts
i'm training usage of templates. made code as foolows:
#include <iostream>;

#include <conio.h>

using namespace std;


template<typename T>

size_t filter(T arr[], size_t size, bool (*fun)(T)){


       T i = 0;

       T j = size;

       T pom;

       size_t counter = 0;

       do 

{ 

if (less10(arr[i]) && less10(arr[j])) 

{ 

    i++;   

} 

else if (less10(arr[i]) && (less10(arr[j])==false)) 

{ 

     i++;   

     j--; 

} 

else if ((less10(arr[i])==false) && less10(arr[j])) 

{ 

     pom=arr[i];            

     arr[i]=arr[j]; 

     arr[j]=pom; 

     i++;                        

     j--; 

} 

else if ((less10(arr[i])==false) && (less10(arr[j])==false)) 

{ 

     j--;   

} 


} while(i=j);

return counter;

}



template <typename T>

void printTable(T arr[], size_t size){

     //size_t size = sizeof(arr)/sizeof(arr[0]);

     for(int i = 0; i<size; i++){

               cout << arr[i] << " ";

             }   

}



template <typename T>

bool less10(T a) {

return a < 10;

}


int main(){

    int arr_i[] = {1,20,3,40,50,6,7,80};

    size_t size_i = sizeof(arr_i)/sizeof(arr_i[0]);

    printTable(arr_i, size_i);

    size_t wrong_i = filter(arr_i,size_i,less10<int>);

    printTable(arr_i, size_i); 

    getch();   

}



filter function doesn't execute.
am i using templates wrong? i will be grateful for any advice.
greetings.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
the condition for your do-while loop is always true: use i==j instead of i=j
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users