c++ 模板函数初试

来源:互联网 发布:连环call软件 编辑:程序博客网 时间:2024/05/16 04:29
#include <stdlib.h>  
#include <malloc.h>  
#include <string.h> 
#include <iostream>
using namespace std;


template <class T>
int find(T* A,int len,T k){
    int start=0,end=(len-1);
    T value=0;
    while(start<=end){
        int pos=(start+end)/2;
        value=*(A+pos);
        printf("find start=%d,end=%d,v=%d,k=%d\r\n",start,end,value,k);
        if(value==k){
            return pos;
        }else if(value<k){
            start=pos;
        }else{
            end=pos;
        }
    }
    return -1;
}


int main(){


    int A[10]={1,2,3,4,5,6,7,8,9,10};
    int res=find(A,10,5);
    printf("find res=%d\r\n",res); 
    return 0;

}


sin@sinpc:~/wkspace/soft/prj/c/sort$ ./a.out 
find start=0,end=9,v=5,k=5
find res=4