Problem E: 数量的类模板(关于模板类的用法)

7 篇文章 0 订阅

 

Append Code

int main()

{

    Data<int> iData[1001];

    Data<double> dData[1001];

    int cases, num;

    char ch;

    int u;

    double v;

    Data<int> a(10), b(20);

    Data<double> c(3.14), d(-4.1);

    cout<<"a + b = "<<(a + b)<<endl;

    cout<<"max(a, b) = "<<(a > b ? a : b)<<endl;

    cout<<"min(a, b) = "<<(a < b ? a : b)<<endl;

    cout<<"c + d = "<<(c + d)<<endl;

    cout<<"max(c, d) = "<<(c > d ? c : d)<<endl;

    cout<<"min(c, d) = "<<(c < d ? c : d)<<endl;

    cin>>cases;

    for (int i = 0; i < cases; i++)

    {

        cin>>ch;

        cin>>num;

        for (int j = 0; j < num; j++)

        {

            if (ch == 'i')

            {

                cin>>u;

                iData[j].setValue(u);

            }

            else if (ch == 'd')

            {

                cin>>v;

                dData[j].setValue(v);

            }

        }

        if (ch == 'i')

        {

            cout<<GetResult<int>::getMax(iData, num);

            cout<<" "<<GetResult<int>::getMin(iData, num);

            cout<<" "<<GetResult<int>::getSum(iData, num)<<endl;

        }

        else if (ch == 'd')

        {

            cout<<GetResult<double>::getMax(dData, num);

            cout<<" "<<GetResult<double>::getMin(dData, num);

            cout<<" "<<GetResult<double>::getSum(dData, num)<<endl;

        }

    }

    return 0;

}

 这确实是前期看见比较懵的题,我也一样,写题解的目的就是为了和大家分享我对这个题的理解和认识,那么话不多说,进入正题:

首先,什么是模板,模板就是通用性高的代码部分,将其中的某些变量或者函数替换成像cin,cout和template<class T>这种泛用性比较高的语句,而对于这道题,最关键的莫过于就是template<class T>,

1、类模板的定义格式
template <typename M,typename R,typename A,typename O>
class Test
{
private:
     M val;
public:
    R func(A a)
    {
         O a;
     }
};

2、类模板的使用
模板类的类型参数不能隐式打断,也就是不能自动实例化,必须显式的指定类型参数。
类名<类型> 对象

3、类模板的静态成员
类中的成员要在类中声明,类外定义(具有const属性的外除),类模板的静态成员也一样。
template
class Test
{
static int val1;
static T val2;
public:
};
templateint Test::val1 = value;
templateT Test:: val2 = value;
(原文链接:https://blog.csdn.net/kubassss/article/details/108817074)

直接
还要注意只要是template的尖括号内的模板函数结束后,如果想再次定义一个模板函数,就要再次使用template<>来定义,即写一次用一次。

还有就是如果要应用一个模板内的数据类型来声明一个新变量时,要注意在前面声明模板变量,即如果我现在定义了一个template<class T>,那么在这个模板类内,如果再声明一个新的类变量,要在变量前面加上<T>类型,如(template<class T>;classData{};Data<T> sum(arr[0]);),

最后附上代码:

#include <iostream>
#include <numeric>
#include <functional>
#include <algorithm>
#include <iomanip>
using namespace std;
template<class T>
class Data{
public:
    T value;
public:
    Data(T a=0):value(a){}
    bool operator>(Data<T> a)
    {return value>a.value;}
    bool operator<(Data<T> a)
    {return value<a.value;}
    Data operator+(Data<T> a)
    {return Data(value+a.value);}
    friend ostream& operator<<(ostream &os,const Data &a)
    {
        os<<fixed<<setprecision(2)<<a.value;
        return os;
    }
   void  setValue(T a)
   {value=a;}
};
template<class T>
class GetResult
{
 public:
     static Data<T> getSum(Data<T> *arr, int num)
     {
          Data<T> sum(arr[0]);
         for(int i=1;i<num;i++)
            sum=sum+arr[i];//由于运算符的重载得以实现,用+=的话要写上value属性
         return sum;
     }
     static Data<T> getMax(Data<T> *arr, int num)
     {   Data<T> sum(arr[0]);
         for(int i=0;i<num;i++)
            if(sum<arr[i])
          sum=arr[i];
         return sum;
     }
     static Data<T> getMin(Data<T> *arr, int num)
     {   Data<T> sum(arr[0]);
         for(int i=0;i<num;i++)
            if(sum>arr[i])
             sum=arr[i];
         return sum;
     }
};


拓展:关于模板函数:上大神的博客吧:
(原文链接:https://blog.csdn.net/low5252/article/details/94622335)

                                                                                                             记录当下,我的编程之路。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值