#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T>
inline int compare(const T &a,const T &b){
if(a>b)
return 1;
if(a<b)
return -1;
if(a==b)
return 0;
}
int main(){
int a=10,b=5;
double c=10.36,d=12.58;
string e="hello",f="hello";
cout<<"compare(a,b)="<<compare(a,b)<<endl;
cout<<"compare(c,d)="<<compare(c,d)<<endl;
cout<<"compare(e,f)="<<compare(e,f)<<endl;
cout<<compare(string("hello"),string("zhangd"))<<endl;
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T>
T juedui(T a){
if(a<0)
return a=-a;
return a;
}
int main(){
int a=-10086;
double b=-26.3;
int c=100;
cout<<juedui(a)<<endl;
cout<<juedui(b)<<endl;
cout<<juedui(c)<<endl;
system("pause");
return 0;
}#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
template<typename T1,typename T2>
T1& print(T1& s,T2 val){
s<<val;
return s;
}
int main(){
double d=0.88;
float f=-12.3;
string s="this is a test";
ofstream fin("c:\\hello.txt",ostream::app);
string desstr;
ostringstream oss(desstr);
print(cout,-3);
print(cout,d);
print(cout,f);
print(cout,s);
if(!fin){
cout<<"can't open it"<<endl;
}
print(fin,-3);
print(fin,d);
print(fin,f);
print(fin,s);
fin.close();
print(oss,-3);
print(oss,d);
print(oss,f);
print(oss,s);
cout<<oss.str()<<endl;
system("pause");
return 0;
}
#include<iostream>
#include<vector>
#include<string>
using namespace std;
template<typename T1,typename T2>
bool zhao(T1 beg,T1 end,T2 a){
bool flag=false;
while(beg!=end){
if(*beg==a){
flag=true;
break;
}
++beg;
}
return flag;
}
int main(){
int a[]={1,2,3,4,5,6,7,8,9,10};
vector<int>v(a,a+10);
if(zhao(v.begin(),v.end(),11)){
cout<<"i have found it"<<endl;
}else{
cout<<"sorry i can't it"<<endl;
}
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T>
void print(const T& c){
typename T::size_type t=0;
while(t!=c.size()){
cout<<c[t]<<" ";
++t;
}
}
int main(){
int a[]={1,4,5,8,7,6,9,4,5,8,1,2,3,6,7,4};
vector<int>v(a,a+16);
print(v);
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T>
void print(T &s1,T &s2){
while(s1!=s2){
cout<<*s1<<" ";
++s1;
}
}
int main(){
int a[]={1,4,5,8,7,6,9,4,5,8,1,2,3,6,7,4};
vector<int>v(a,a+16);
print(v.begin(),v.end());
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T>
void print(T &c){
typename T::iterator beg=c.begin();
typename T::iterator end=c.end();
while(beg!=end){
cout<<*beg<<" ";
++beg;
}
cout<<endl;
}
int main(){
int a[]={1,4,5,8,7,6,9,4,5,8,1,2,3,6,7,4};
vector<int>v(a,a+16);
print(v);
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T,int N>
int size(T (&arr)[N]){
return N;
}
int main(){
int a[14]={1,2,3,4,5,6,7,8,9,10};
cout<<size(a)<<endl;
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
template<typename T,int N>
void print(T (&arr)[N]){
for(int i=0;i!=N;++i){
cout<<arr[i]<<endl;
}
}
int main(){
int a[]={1,2,3,4,5,6,7,8,4,1};
print(a);
system("pause");
return 0;
}
看代码,不用解释。