c++之模板編程

來源:互聯網
上載者:User

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

 

看代碼,不用解釋。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.