Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
FunTemp
//g++ 7.4.0 ////////////////////////////////////////////////////////////////////////////// //FunTemp:Function Template Specialization //Binary Search template code credit goes to John R. // Hubbard, University Of Richmond; //Rest of the code and modifications are created by //Rezaul Hoque on May 17,2022; //contact:jewelmrh@yahoo.com;Dhaka,Bangladesh;https://rezaulhoque.wordpress.com,https://hoquestake.blogspot.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; ////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <string> template <class T> int search(T a[] ,T thing) { int n=5; for(int i=0; i<n; i++){ if(a[i]==thing) { return i; } } return -1; } ////////////////////////////////////////////////////// //Specialization for std::string // // // ////////////////////////////////////////////////// template <> int search<std::string>(std::string a[], std::string thing) { int first,last,mid,n=5; first=0; last=n-1; while(first <= last) { mid=(first +last)/2; if (thing<a[mid]) last = mid -1; else if (thing > a[mid]) first =mid +1; else return mid; } return -1; } int main() { int n=5,key; int g[n]={1,2,4,5,6}; std::string a[5]={"Hello","Hola","Ni Hao","Salam","Namastey"}; std::string thing; std::cout<<"Enter the thing you look for:\n"; while(std::cin>>key){ int loc=search<int>(g,key) ; if(loc== -1) std::cout <<key<<" is not found.\n"; else std::cout<<key<<" is at a["<<loc<<"].\n"; } return 0; }
run
|
edit
|
history
|
help
0
Void main
matrix
runtime template mode processor
Problema Siruri
Find value in sorted matrix
stackLinkedlist
顺序表的实现——动态分配
Handling new types without using RTTI
Cotton Farm 0.0.1.0
Graph DFS