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;
#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
- c++ 模板函数初试
- 【c/c++】函数模板和模板函数
- C/C++ 函数模板
- c++:函数模板
- 【C++】函数模板
- 【c++】函数模板
- C语言函数模板
- C++:函数模板
- 【C++】模板函数总结!!!
- C/C++:函数模板与类模板
- [C/C++]模板函数与模板类
- 【.Net码农】初试T4模板
- C语言初试
- C++:创建线程初试
- 学习C++模板---模板函数
- Linux C/C++ 模板:函数模板
- C++,template,类模板和函数模板
- C++:函数模板、类模板及其特化
- 使用Seam Framework + JBoss 5.0 开发第一个Web应用 - 简单投票程序
- iOS:App Distribution(二)--注册 &真机调试 &发布 使用总结
- LeetCode - Remove Duplicates from Sorted List
- 由前序和中序遍历建立二叉树并后序遍历输出二叉树
- 卸载Ubuntu
- c++ 模板函数初试
- 栈的习题
- 黑马程序员——Java高新技术(1)
- Servlet
- makefile 调用其他makefile
- 设置ssh免密码登录服务器
- 第17章,进程间通信
- 交通部:高速高铁等公共领域拟实现一卡通
- MySQL Performance: Linux I/O