c++之模板编程

97 篇文章 0 订阅
92 篇文章 0 订阅

#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;
}

 

看代码,不用解释。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值