#include<iostream>
using namespace std;
template <typename T>
T sum(T*begin,T*end)
{
T*p=begin;
T ans=0;
for(T*p=begin;p!=end;p++)
ans=ans+*p;
return ans;
}//模板
int main()
{
double a[]={1.1,2.2,3.3,4.4};
cout<<sum(a,a+4)<<endl;
return 0;
}
template<typename T>
struct Point
{
T x,y;
Point(T x=0,T y=0):x(x),y(y){}
} ;
template<typename T>
Point<T> operator + (const Point<T> &A,const Point<T> &B)
{
return Point<T>(A.x+B.x,A.y+B.y);
}
ostream& operator <<(ostream &out,const Point<T> &p)
{
out<<"("<<p.x<<","<<p.y<<")";
return out;
}
使用时
Point<int>a(1,2),b(3,4);
Point<double>c(1.1,2.2),d(3.3,4.4);
算法之路二:刘汝佳算法竞赛入门经典:c++模板
最新推荐文章于 2023-03-06 16:21:24 发布