C++:模板

Cpp 中有两种模板:函数模板 和 类模板

他们都不能直接使用。使用时,必须对类型参数赋以具体的数据类型,才能得到可使用的模板函数和模板类(实例化)

函数模板

函数模板代表了一组函数的集合,不是一个实实在在的函数,不可以直接使用,编译系统也不为它产生任何可执行代码

cpp 复制代码
#include <iostream>
using namespace std;

template <class T>
T Max(T a, T b) 
{
    return a > b? a : b;
}

int main()
{
    cout << Max(10, 20) << endl;
    cout << Max(20.5, 15.3) << endl;
    cout << Max<int>(10, 20.5) << endl;
    cout << Max<double>(10, 20.5) << endl;
    return 0;
}

模板函数的重载

cpp 复制代码
#include <iostream>
#include <cstring>


template <class T>
T Max(T a, T b) 
{
    return a > b? a : b;
}

char* Max(char* a, char* b) 
{
    return (std::strcmp(a, b) > 0? a : b);
}

int main()
{
    std::cout << Max(5,6) << std::endl;
    std::cout << Max("g", "s") << std::endl;
    return 0;
}

类模板

类模板不是一个实实在在的类,是一组类的集合。

没有可执行的代码,使用时要实例化为模板类
类模板外定义成员函数

定义最开始要有与类模板完全一致的前缀

类名后要有"类型参数表"

cpp 复制代码
#include <iostream>
using namespace std;

template <class T1, class T2>
class Example 
{
protected:
    T1 x;
    T2 y;
public:
    Example(T1, T2);
    void print() { cout << "x = " << x << ", y = " << y << endl; }
};

template <class T1, class T2>
Example<T1, T2>::Example(T1 a, T2 b)
{
    x = a;
    y = b;
}

int main()
{
    Example<int,int> a(10,10);
    Example<int,double> b(20,30.5);
    Example<char,int> c('M',20);

    a.print();
    b.print();
    c.print();

    return 0;
}

程序实例

cpp 复制代码
#include <iostream>
#include <cstring>
#include <iomanip>

const int SUM = 5;

template <typename T>
class Student 
{
protected:
    std::string ID;
    std::string name;
    T score;
public:
    Student(std::string id="000", std::string na=" ", T sc=0)
    {
        ID = id;
        name = na;
        score = sc;
    }
    void Set(std::string ID, std::string name, T score)
    {
        this->ID = ID;
        this->name = name;
        this->score = score;
    }
    std::string GetID()
    {
        return ID;
    }
    std::string GetName()
    {
        return name;
    }
    T GetScore()
    {
        return score;
    }
};

class Group 
{
protected:
    Student<int> st[SUM];
    int sum;
public:
    Group();
    void Input();
    void Output();
    void SortByScore();
    void SortByName();
};


Group::Group()
{
    sum = SUM;
}

void Group::Input()
{
    int i;
    std::string id,na;
    int sc;
    for(i=0;i<sum;i++)
    {
        std::cout<<"Input ID: ";
        std::cin>>id;
        std::cout<<"Input name: ";
        std::cin>>na;
        std::cout<<"Input score: ";
        std::cin>>sc;
        st[i].Set(id,na,sc);
    }
}

void Group::Output()
{
    int i;
    std::cout<<"\n学号   姓名   成绩"<<std::endl;
    for(i=0;i<sum;i++)
    {
        std::cout<<std::setw(6)<<st[i].GetID()<<std::setw(6)<<st[i].GetName()<<std::setw(6)<<st[i].GetScore()<<std::endl;
    }
}

void Group::SortByScore()
{
    int i,j;
    Student<int> temp;
    for(i=0;i<sum-1;i++)
    {
        for(j=i+1;j<sum;j++)
        {
            if(st[i].GetScore()<st[j].GetScore())
            {
                temp = st[i];
                st[i] = st[j];
                st[j] = temp;
            }
        }
    }
}

void Group::SortByName()
{
    int i,j;
    Student<int> temp;
    for(i=0;i<sum-1;i++)
    {
        for(j=i+1;j<sum;j++)
        {
            if(strcmp(st[i].GetName().c_str(),st[j].GetName().c_str())>0)
            {
                temp = st[i];
                st[i] = st[j];
                st[j] = temp;
            }
        }
    }
}

int main()
{
    Group g;
    g.Input();
    g.Output();
    g.SortByScore();
    g.Output();
    g.SortByName();
    g.Output();
    return 0;
}
相关推荐
愚润求学10 分钟前
【Linux】进程间通信(一):认识管道
linux·运维·服务器·开发语言·c++·笔记
珊瑚里的鱼25 分钟前
【滑动窗口】LeetCode 1658题解 | 将 x 减到 0 的最小操作数
开发语言·c++·笔记·算法·leetcode·stl
落樱弥城29 分钟前
角点特征:从传统算法到深度学习算法演进
人工智能·深度学习·算法
晚秋大魔王39 分钟前
OpenHarmony 开源鸿蒙南向开发——linux下使用make交叉编译第三方库——wget
java·linux·运维·开发语言·华为·harmonyos
heath ceTide42 分钟前
轻量、优雅、高扩展的事件驱动框架——Hibiscus-Signal
java·开发语言
_extraordinary_42 分钟前
Java 常用的Arrays函数
java·开发语言
_extraordinary_1 小时前
Java 类和对象
java·开发语言
Aliano2171 小时前
TestNGException ClassCastException SAXParserFactoryImpl是Java自带的Xerces解析器——解决办法
java·开发语言·python
共享家95271 小时前
哈希的原理、实现
c++·算法
漫谈网络1 小时前
回调函数应用示例
开发语言·python·回调函数