【2020.12.26】模板

冒泡排序函数

一个针对int数组的冒泡排序函数,如果排序main()函数中的char数组(carr),要对函数进行改动。

#include <iostream>

//一个针对int数组的冒泡排序函数
void Sort(int* arr, int nLength)
{
    int i, k;
    for (i = 0; i < nLength - 1; i++)
    {
        for (k = 0; k < nLength - 1 - i; k++)
        {
            if (arr[k] > arr[k + 1])
            {
                int tmp = arr[k];
                arr[k] = arr[k + 1];
                arr[k + 1] = tmp;
            }
        }
    }
}

int main()
{
    int iarr[] = { 9,8,7,6,5,4,3,2,1 };
    char carr[] = { 9,8,7,6,5,4,3,2,1 };

    Sort(iarr, 9);

    return 0;
}

模板格式

在函数中使用模板

template <class 形参名称, class 形参名称, ...>
返回类型 函数名称(参数列表)
{
    函数体
}

在结构体/类中使用模板

template <class 形参名称, class 形参名称, ...>
class 类名
{
    ...
}

使用模板后的冒泡排序函数

#include <iostream>

//使用模板后的一个冒泡排序函数
template <class T>
void Sort(T* arr, int nLength)
{
    int i, k;
    for (i = 0; i < nLength - 1; i++)
    {
        for (k = 0; k < nLength - 1 - i; k++)
        {
            if (arr[k] > arr[k + 1])
            {
                T tmp = arr[k];
                arr[k] = arr[k + 1];
                arr[k + 1] = tmp;
            }
        }
    }
}

int main()
{
    int iarr[] = { 9,8,7,6,5,4,3,2,1 };
    char carr[] = { 9,8,7,6,5,4,3,2,1 };

    //对int数组进行排序
    Sort(iarr, 9);

    //对char数组进行排序
    Sort(carr, 9);

    return 0;
}

使用模板排序结构体/类的准备工作

由于>符号无法对结构体/类进行比较:

            if (arr[k] > arr[k + 1])
            {
                T tmp = arr[k];
                arr[k] = arr[k + 1];
                arr[k + 1] = tmp;
            }

所以需要对>符号进行重载:

class Base
{
private:
    int x;
    int y;

public:
    Base(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    bool operator>(Base& base)
    {
        return this->x > base.x && this->y > base.y;
    }
};

重载>符号后,即可使用>符号进行比较。

使用模板后能对结构体/类进行冒泡排序的函数

#include <iostream>

class Base
{
private:
    int x;
    int y;

public:
    Base(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    bool operator>(Base& base)
    {
        return this->x > base.x && this->y > base.y;
    }
};

//使用模板后的一个冒泡排序函数
template <class T>
void Sort(T* arr, int nLength)
{
    int i, k;
    for (i = 0; i < nLength - 1; i++)
    {
        for (k = 0; k < nLength - 1 - i; k++)
        {
            if (arr[k] > arr[k + 1])
            {
                T tmp = arr[k];
                arr[k] = arr[k + 1];
                arr[k + 1] = tmp;
            }
        }
    }
}

int main()
{
    int iarr[] = { 9,8,7,6,5,4,3,2,1 };
    char carr[] = { 9,8,7,6,5,4,3,2,1 };

    Base b1(5, 5), b2(4, 4), b3(3, 3), b4(2, 2), b5(1, 1);
    Base barr[] = {b1, b2, b3, b4, b5};

    //对int数组进行排序
    Sort(iarr, 9);

    //对char数组进行排序
    Sort(carr, 9);

    //对Base数组进行排序
    Sort(barr, 5);
    
    return 0;
}

在结构体/类中使用模板

原来的样子:

#include <iostream>

struct Base
{
    int x;
    int y;

    char a;
    char b;

    int Max()
    {
        if(x > y)
        {
            return x;
        }
        else
        {
            return y;
        }
    }

    char Min()
    {
        if(a < b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
};

修改后的样子:

注意:如果传入的是自定义数据类型,则需要重载运算符。

#include <iostream>

template<class T, class C>
struct Base
{
    T x;
    T y;

    C a;
    C b;

    T Max()
    {
        if(x > y)
        {
            return x;
        }
        else
        {
            return y;
        }
    }

    C Min()
    {
        if(a < b)
        {
            return a;
        }
        else
        {
            return b;
        }
    }
};

int main()
{
    Base <int, char> Base;

    Base.x = 1;
    Base.y = 2;

    Base.a = 3;
    Base.b = 4;

    int ret = Base.Max();

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值