N

Norlcayn的博客

A text-focused Halo theme

  • 首页
  • 文章
  • 默认分类
  • 关于
主页 CPP模板
文章

CPP模板

发表于 2025-06-28 更新于 昨天
作者 Administrator
50~65 分钟 阅读

模板的概念

模板就像一个”代码模具”,可以根据不同的类型生成相应的代码,大大提高复用性。

模板的优势在于:

代码重用:一份代码适用于多种类型

类型安全:编译期检查,避免运行时错误

性能优化:编译期生成代码,无运行时开销

灵活性:支持特化、偏特化等高级特性

需求:假设要实现求最大值的函数,需要支持int、double、string等类型:

// 传统做法:每种类型写一个函数
int maxInt(int a, int b) { return a > b ? a : b; }
double maxDouble(double a, double b) { return a > b ? a : b; }
string maxString(string a, string b) { return a > b ? a : b; }

问题:通过以上内容,可以看出代码重复、维护困难、容易出错。虽然这几个函数只是参数类型不同,但函数内的实现逻辑都是相同的,可是还是需要单独实现

函数模板

  • C++另一种编程思想称为泛型编程,主要利用的技术就是模板
  • C++提高两种模板机制:函数模板和类模板

语法:

tmplate<typename T>
函数声明或实现

解释:

  • template:声明创建模板
  • typename:表面后面的符号是一种数据类型,可以用class代替
  • T:通用的数据类型,名称可以替换,通常为大写字母

需求:利用函数将两个值进行交换

需求分析:因为不知道两个值的具体类型,所以设计函数时需要将C++中所有的数据类型都包含进去,采用传统的方式只能一一列举出来。这里就可以用到模板技术了

示例:

#include <iostream>
#include <string>

using namespace std;

// 函数模板
template <typename T>
void SwapTwoValue(T& a, T& b) {
    T temp = a;
    a = b;
    b = temp;
}

int main() {

    cout << "Before Swap" << endl;
    string a = "hello";
    string b = "world";

    int x = 10;
    int y = 20;

    bool flag1 = true;
    bool flag2 = false;

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "flag1 = " << flag1 << endl;
    cout << "flag2 = " << flag2 << endl;

    cout << "After Swap" << endl;

    SwapTwoValue(a, b);
    SwapTwoValue(x, y);
    SwapTwoValue(flag1, flag2);

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "flag1 = " << flag1 << endl;
    cout << "flag2 = " << flag2 << endl;

    /*
        Before Swap     After Swap
        a = hello       a = world
        b = world       b = hello
        x = 10          x = 20
        y = 20          y = 10
        flag1 = 1       flag1 = 0
        flag2 = 0       flag2 = 1
    */

    return 0;
}

函数模板注意事项

注意事项:

  • 自动类型推导,必须推导出一致的数据类型T才可以使用
  • 模板必须要确定出T的数据类型才可以使用

示例:

#include <iostream>
#include <string>

using namespace std;

// 函数模板
template <typename T>   // T的一致性要求
void ShowValue(T& a, T& b) {
    cout << a << " " << b << endl;
}

template <typename T>
void func() {
     cout << "hello world" << endl;
}

int main() {
    int a = 10;
    string b = "hello world";

    ShowValue(a, b);    // 报错,原因:自动推导类型不一致

    func(); // 报错,原因:模板必须要确定出T的数据类型才可以使用

    func<int>();// 正确,虽然int类型没有实际意义,但是确定了T的数据类型
    return 0;
}

普通函数与函数模板的区别

普通函数与函数模板的区别:

  • 普通函数调用时可以发生自动类型转换(隐式类型转换)
  • 函数模板调用时,如果利用自动类型推导,不会发生隐式类型转换
  • 如果利用显示指定类型的方式,可以发生隐式类型转换

普通函数:

#include <iostream>
#include <string>

using namespace std;

// 函数模板
int numAdd(int a, int b) {
    return a + b;
}

int main() {
    int a = 10;
    int b = 20;
    cout << numAdd(a, b) << endl;

    char c = 'a';
    cout << numAdd(a, c) << endl;   // 会将char类型转换为int类型(根据ASCII码)

    return 0;
}

函数模板:

#include <iostream>
#include <string>

using namespace std;

// 函数模板
template<typename T>
T numAdd(T a, T b) {
    return a + b;
}

int main() {
    int a = 10;
    int b = 20;
    cout << numAdd<int>(a, b) << endl;

    char c = 'a';
    // cout << numAdd(a,c) << endl;    // 报错

    cout << numAdd<int>(a,c) << endl;   // 指定泛型类型则正常编译

    return 0;
}

普通函数与函数模板的调用规则

调用规则如下:

  1. 如果函数模板和普通函数都可以实现,优先调用普通函数
  2. 可以通过空模板参数列表来强制调用函数模板
  3. 函数模板也可以发生重载
  4. 如果函数模板可以产生更好的匹配,优先调用函数模板
#include <iostream>
#include <string>

using namespace std;

// 普通函数
void print(int a, int b) {
    cout << "普通函数" << endl;
}

// 函数模板
template<typename T>
void print(T a, T b) {
    cout << "函数模板" << endl;
}

// 函数模板重载
template<typename T>
void print(T a, T b,T c) {
    cout << "函数模板重载" << endl;
}

int main() {
    int a = 10;
    int b = 20;
    print(a, b);    // 普通函数

    string c = "Hello";
    string d = "World";
    print(c, d);    // 函数模板

    // 通过空模板强制调用函数模板
    print<>(a, b);  // 函数模板

    // 函数模板也能发生重载
    print(a,b,30);   // 函数模板重载

    // 如果函数模板可以产生更好的匹配,优先调用函数模板
    char e = 'a';
    char f = 'b';
    print(e, f);    // 函数模板

    return 0;
}

函数模板的局限性

模板的通用性并不是万能的

例如:

template<class T>
void f(T a,T b) {
a = b;
}

在上述代码中提供的赋值操作,如果传入的a和b是一个数组,就无法实现了

再例如:

template<class T>
void f(T a,T b)
{
if(a > b){ ... }
}

在上述代码中,如果T的数据类型传入的是像Person这样的自定义数据类型,也无法正常运行

因此C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板

之前学习过在类中通过重写运算符的方式实现了对象之间的比较,现在可以通过具体化模板实现自定义类型的通用化

#include <iostream>
#include <string>

using namespace std;

class Person {
public:
    string name;
    int age;

    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }
};

// 对比两个数据是否相等的函数
template<typename T>
bool is_equal(T a, T b) {
    return a == b;
}

// 利用具体化Person的版本实现自定义数据类型比较,具体化优先调用
template<> bool is_equal<Person>(Person a, Person b) {
    return a.name == b.name && a.age == b.age;
}

// 比较两个普通数据类型
void test01() {
    int a = 10, b = 10;
    bool ret = is_equal(a, b);
    if (ret) {
        cout << "a == b" << endl;
    }
    else {
        cout << "a != b" << endl;
    }
}

// 比较两个自定义数据类型
void test02() {
    Person p1("Tom", 10);
    Person p2("Tom", 10);
    bool ret = is_equal(p1, p2);
    if (ret) {
        cout << "p1 == p2" << endl;
    }
    else {
        cout << "p1 != p2" << endl;
    }
}

int main() {
    test01();

    test02();


    return 0;
}

分析:通过具体化模板,对Person进行了特殊处理,所以可以让Person对象进行比较

总结:

  • 利用具体化的模板,可以解决自定义类型的通用化
  • 学习模板并不是为了写模板,而是在STL能够运用系统提供的模板

类模板

类模板语法

类模板作用:建立一个通用类,类中的成员、数据类型可以不具体制定,用一个虚拟的类型来代表

语法:

template<typename T>
类

解释:

template:声明创建模板

typename:表明其后面的符号是一种数据类型,可以用Class代替

T:通用的数据类型,名称可以替换,通常为大写字母

示例:

#include <iostream>
#include <string>

using namespace std;

template <class NameType, class AgeType>
class Person {
public:
    NameType name;
    AgeType age;

    Person(NameType name, AgeType age) {
        this->name = name;
        this->age = age;
    }
};

int main() {
    Person<string,int> p1("Tom", 18);
    cout << p1.name << " " << p1.age << endl;


    return 0;
}

类模板和函数模板区别

类模板与函数模板区别主要有两点:

  1. 类模板没有自动类型推导的使用方式
  2. 类模板在模板参数列表中可以有默认参数

示例:

#include <iostream>
#include <string>

using namespace std;

template <class NameType, class AgeType = int>  // int为AgeType的默认参数类型
class Person {
public:
    NameType name;
    AgeType age;

    Person(NameType name, AgeType age) {
        this->name = name;
        this->age = age;
    }
};

// 1. 类模板没有自动类型推导方式
void test01() {
    // Person p1("Tom", 10);   // 提示:缺少类模板"Person"的参数列表。也就是无法自动推导数据类型
    Person<string, int> p1("Tom", 10);
}

// 2. 类模板在模板参数列表中可以有默认参数
void test02() {
    Person<string> p1("Tom", 10);
    Person<string, float> p2("Bob",10.5);
}

int main() {

    test01();

    test02();

    return 0;
}

类模板中成员函数创建时机

类模板中成员函数和普通类中成员函数创建时机是有区别的:

  • 普通类中的成员函数一开始就可以创建
  • 类模板中的成员函数在调用时才创建
#include <iostream>
#include <string>

using namespace std;

class Person1 {
public:
    void ShowPerson1() {
        cout << "Show Person1" << endl;
    }
};

class Person2 {
public:
    void ShowPerson2() {
        cout << "Show Person2" << endl;
    }
};

template <class T>
class Person3 {
public:
    T obj;  // 这里表示是一个不确定的对象

    void ShowP1() {
        obj.ShowPerson1();  // 调用的是Person1中的成员函数
    }

    void ShowP2() {
        obj.ShowPerson2();  // 调用的是Person2中的成员函数
    }
};

int main() {
    // 当模板类定义完后,只要不调用,就不会生成泛型成员
    Person3<Person1> p1;    // 当指定的类型为Person1时
    p1.ShowP1();    // 成功运行
    p1.ShowP2();    // 报错

    return 0;
}

类模板对象做函数参数

一共有三种传入方式:

  1. 指定传入的类型 — 直接显示对象的数据类型
  2. 参数模板化 — 将对象中的参数变为模板进行传递
  3. 整个类模板化 — 将这个对象类型模板化进行传递

示例:

#include <iostream>
#include <string>

using namespace std;

// 类模板
template <class T1,class T2>
class Person {
public:
    T1 name;
    T2 age;

    Person(T1 name,T2 age) {
        this->name = name;
        this->age = age;
    }

    void showPerson() {
        cout << "姓名:" << this->name << " 年龄:" << this->age << endl;
    }
};

// 1. 传入指定类型
void printPerson1(Person<string,int> &p) {
    p.showPerson();
}

void test01() {
    Person<string,int> p1("Tom",10);
    printPerson1(p1);
}

// 2. 参数模板化
template<class T1,class T2>
void printPerson2(Person<T1,T2> &p) {
    p.showPerson();
}

void test02() {
    Person<string,int> p1("Bob",15);
    printPerson2(p1);
}

// 3. 整个类模板化
template<class T>
void printPerson3(T &p) {
    p.showPerson();
}

void test03() {
    Person<string,int> p1("Alice",8);
    printPerson3(p1);
}

int main() {
    test01();

    test02();

    test03();

    return 0;
}

类模板与继承

当类模板碰到继承时,需要注意以下几点:

  • 当子类继承的父类是一个类模板时,子类在声明的时候,需要指定出父类中T的类型
  • 如果不指定,编译器无法给子类分配内存
  • 如果想灵活指定出父类中T的类型,子类也需要变为类模板

类模板成员函数类外实现

#include <iostream>
#include <string>

using namespace std;

// 类模板中成员函数类外实现
template <class T1, class T2>
class Person {
public:
    T1 name;
    T2 age;

    Person(T1 name, T2 age);

    void showPerson();
};

// 类模板中构造函数类外实现
template <class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age) {   // 一定要在Person类后加上<>,否则只会被识别为普通类
    this->name = name;
    this->age = age;
}

// 类模板中成员函数类外实现
template <class T1, class T2>
void Person<T1, T2>::showPerson() {         // 一定要在Person类后加上<>,否则只会被识别为普通类
    cout << "name: " << name << " age: " << age << endl;
}

int main() {
    Person<string, int> p1("Tom", 10);

    p1.showPerson();

    return 0;
}

类模板分文件编写

问题:

  • 类模板中成员函数创建时机是在调用截断,导致分文件编写时链接不到

解决:

  • 解决方式1:直接包含.cpp源文件
  • 解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp,hpp是约定的名称,并不是强制

Person.hpp:

#pragma once
#include <iostream>
#include <string>
using namespace std;

template <class T1, class T2>
class Person {
public:
    T1 name;
    T2 age;

    Person(T1 name, T2 age);

    void showPerson();
};

// 构造函数
template <class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age) {
    this->age = age;
}

// 成员函数
template <class T1, class T2>
void Person<T1, T2>::showPerson() {
    cout << "name: " << name << " age: " << age << endl;
}

Main.cpp文件:

#include "Person.hpp"

int main() {
    Person<string, int> p1("Tom", 10);

    p1.showPerson();

    return 0;
}

类模板与友元

全局函数类内实现 - 直接在类内声明友元即可

全局函数类外实现 - 需要提前让编译器知道全局函数的存在

类内实现

#include <iostream>
#include <string>

using namespace std;

template <class T1,class T2>
class Person {
    // 全局函数做友元,并且类内实现
    friend void printPerson1(Person<T1, T2>& p) {
        cout << "姓名: " << p.name << " 年龄: " << p.age << endl;
    };

private:
    T1 name;
    T2 age;

public:
    Person(T1 name,T2 age) {
        this->name = name;
        this->age = age;
    }
};

void test01() {
    Person<string,int> p1("张三",18);
    printPerson1(p1);
}

int main() {
    test01();

    return 0;
}

类外实现(较复杂)

#include <iostream>
#include <string>

using namespace std;

// 因为全局函数中参数为Person类,所以需要提前声明
template <class T1,class T2>
class Person;

// 类外实现,一定要提前实现全局函数
template <class T1,class T2>
void printPerson2(Person<T1, T2>& p) {
    cout << "姓名:" << p.name << " 年龄:" << p.age << endl;
}

template <class T1,class T2>
class Person {
    // 全局函数做友元,并且类外实现
    friend void printPerson2<>(Person<T1, T2>& p);  // 加上<>使普通函数变为模板函数

private:
    T1 name;
    T2 age;

public:
    Person(T1 name,T2 age) {
        this->name = name;
        this->age = age;
    }
};

void test02() {
    Person<string,int> p("Tom", 18);
    printPerson2(p);
}

int main() {
    test02();

    return 0;
}

类模板案例

案例描述:实现一个通用的数组类,要求如下:

  • 可以对内置数据类型以及自定义数据类型的数据进行存储
  • 将数组中的数据存储到堆区
  • 构造函数可以传入数组的容量
  • 提供对应的拷贝构造函数以及operator=防止浅拷贝问题
  • 提供尾插法和尾删法对数组中的数据进行增加和删除
  • 可以通过下标的方式访问数组中的元素
  • 可以获取数组中当前元素个数和数组容量

通用数组类

首先是通用数组类的定义(MyArray.hpp):

#pragma once
#include <iostream>
#include <string>

using namespace std;

// - 可以对内置数据类型以及自定义数据类型的数据进行存储
// - 将数组中的数据存储到堆区
// - 构造函数可以传入数组的容量
// - 提供对应的拷贝构造函数以及operator = 防止浅拷贝问题
// - 提供尾插法和尾删法对数组中的数据进行增加和删除
// - 可以通过下标的方式访问数组中的元素
// - 可以获取数组中当前元素个数和数组容量

template <typename T>
class MyArray {
private:
    T* pAddress;    // 指针指向堆区开辟的真实数组
    int size;       // 当前数组中元素个数
    int capacity;   // 当前数组的容量

public:
    MyArray(int capacity) {
        cout << "构造函数被调用" << endl;
        this->capacity = capacity;
        this->size = 0;
        this->pAddress = new T[capacity];
    }

    // 析构函数
    ~MyArray() {
        cout << "析构函数被调用" << endl;
        if (pAddress != NULL) {
            delete[] pAddress;
            pAddress = NULL;
        }
    }

    // 拷贝构造函数
    MyArray(const MyArray &myArray) {
        cout << "拷贝构造函数被调用" << endl;
        this->size = myArray.size;
        this->capacity = myArray.capacity;

        // 深拷贝
        this->pAddress = new T[myArray.capacity];
        // 将myArray中的数据拷贝给当前对象
        for (int i = 0; i < myArray.size; i++) {
            this->pAddress[i] = myArray.pAddress[i];
        }
    }

    // 重载operator= 防止浅拷贝
    MyArray &operator=(const MyArray &myArray) {
        cout << "operator=" << endl;
        // 先判断原来堆区是否有数据
        if (this->pAddress != NULL) {
            delete[] this->pAddress;
            this->pAddress = NULL;
            this->size = 0;
            this->capacity = 0;
        }

        this->size = myArray.size;
        this->capacity = myArray.capacity;
        this->pAddress = new int[this->capacity];
        for (int i = 0; i < this->size; i++) {
            this->pAddress[i] = myArray.pAddress[i];
        }

        return *this;   // 返回自身
    }

    // 尾插法
    void Push_Back(const T& val) { 
        if (this->size == this->capacity) { 
            return;
        }

        this->pAddress[this->size] = val;
        this->size++;
    }

    // 尾删法
    void Pop_Back() {
        if (this->size == 0) {
            return;
        }
        this->size--;
    }

    // 通过下标访问数组中的元素,并且满足:arr[0] = 100这种形式
    T& operator[](int index) {
        return this->pAddress[index];
    }

    // 获取数组中元素的个数
    int Get_Size() {
        return this->size;
    }

    // 获取数组的容量
    int Get_Capacity() {
        return this->capacity;
    }

};

内置数据类型测试

#include <iostream>
#include <string>
#include "MyArray.hpp"

using namespace std;

// 打印数组
template<typename T>
void PrintArray(MyArray<T>& arr) {
    for (int i = 0; i < arr.Get_Size(); i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

void test01() {
    MyArray<int> arr1(5);   // 基本使用

    MyArray<int> arr2(arr1);    // 拷贝构造

    MyArray<int> arr3(10);  
    arr3 = arr2;    // operator=重写
}

void test02() {
    MyArray<int> arr1(5);
    for (int i = 0; i < arr1.Get_Capacity(); i++) {
        arr1.Push_Back(i);
    }
    PrintArray(arr1);

    cout << "arr1的容量为: " << arr1.Get_Capacity() << endl;    // 5
    cout << "arr1的元素个数为: " << arr1.Get_Size() << endl;    // 5

    arr1.Pop_Back();
    cout << "调用一次尾删方法后: " << endl;
    cout << "arr1的容量为: " << arr1.Get_Capacity() << endl;    // 5
    cout << "arr1的元素个数为: " << arr1.Get_Size() << endl;    // 4
}

int main() {
    //test01();

    test02();

    system("pause");

    return 0;
}

自定义数据类型测试

#include <iostream>
#include <string>
#include "MyArray.hpp"

using namespace std;

// 打印数组
template<typename T>
void PrintArray(MyArray<T>& arr) {
    for (int i = 0; i < arr.Get_Size(); i++)
    {
        cout << arr[i] << endl;
    }

}

// 自定义数据类型
class Person {
public:
    string name;
    int age;

    Person() {  // 一定要提供默认构造函数,否则会报错
        this->name = "";
        this->age = 0;
    }

    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }

    // 方法1:友元函数(推荐)
    friend ostream& operator<<(ostream& out, const Person& p) {
        out << "姓名:" << p.name << " 年龄:" << p.age;
        return out;  // 返回ostream引用,支持链式调用
    }
};

void test1() {
    MyArray<Person> arr(4);
    Person p1("张三", 18);
    Person p2("李四", 19);
    Person p3("王五", 20);
    Person p4("赵六", 21);
    arr.Push_Back(p1);
    arr.Push_Back(p2);
    arr.Push_Back(p3);
    arr.Push_Back(p4);

    PrintArray(arr);
}

int main() {
    test1();

    system("pause");

    return 0;
}
CPP
自学系列 编程语言 CPP模板
许可协议:  CC BY 4.0
分享

相关文章

6月 28, 2025

CPP错误案例

数组倒序 将一组数组倒序排序,如{1,2,3,4,5}=>{5,4,3,2,1} 错误写法: #include <iostream> using namespace std; int main() { // 数组倒序 int arr_02[] = { 1,2,3,4,5 }; length =

6月 28, 2025

CPP基础案例

通讯录管理系统 系统需求 通讯录是一个可以记录亲人、好友信息的工具 本案例主要利用C++来实现基础的通讯录管理系统 系统中需要实现的主要功能如下: 添加联系人:向通讯录中添加新人,信息包括姓名、性别、年龄、联系电话、家庭住址,最多记录1000人 显示联系人:显示通讯录中所有联系人信息 删除联系人:按

6月 28, 2025

CPP文件操作

C++文件操作 关键字:ifstream、ofstream、ios:: 程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放 通过文件可以将数据持久化 C++中对文件操作需要包含头文件 文件类型分为两种: 文本文件 - 文件以文本的ASCII码形式存储在计算机中 二进制文件 - 文件以文本

下一篇

CPP_STL

上一篇

CPP面向对象

最近更新

  • Hello Halo
  • 状态机
  • 数据表设计规范
  • 缓存优化
  • 事务失效

热门标签

Halo 异常处理 Redisson 并行 Vue 并发 递归 请求限流 SpringAMQP 类加载器

目录

©2026 Norlcayn的博客. 保留部分权利。

使用 Halo 主题 Chirpy