C++之模板(二)

1、类模板

2、使用类模板

类模板在使用的时候要显示的调用是哪种类型,而不是像函数模板一样能够根据参数来推导出是哪种类型。

Stack.h

cpp 复制代码
#include <stdexcept>

template <typename T>
class Stack
{
public:
    explicit Stack(int maxSize);
    ~Stack();

    void Push(const T& elem);
    void Pop();
    T& Top();
    const T& Top() const;
    bool Empty() const;

private:
    T* elems_;
    int maxSize_;
    int top_;
};

template <typename T>
Stack<T>::Stack(int maxSize) : maxSize_(maxSize), top_(-1)
{
    elems_ = new T[maxSize_];
}

template <typename T>
Stack<T>::~Stack() {
    delete []elems_;
}

template <typename T>
void Stack<T>::Push(const T& elem)
{
    if (top_ + 1 >= maxSize_)
    {
        throw std::out_of_range("Stack<T>::Push stack full");
    }
    elems_[++top_] = elem;
}

template <typename T>
void Stack<T>::Pop()
{
    if (top_ + 1 <= 0)
    {
        throw std::out_of_range("Stack<T>::Pop stack empty");
    }
    --top_;
}

template <typename T>
T& Stack<T>::Top()
{
    if (top_ + 1 <= 0)
    {
        throw std::out_of_range("Stack<T>::Top stack empty");
    }
    return elems_[top_];
}

template <typename T>
const T& Stack<T>::Top() const
{
    if (top_ + 1 <= 0)
    {
        throw std::out_of_range("Stack<T>::Top stack empty");
    }
    return elems_[top_];
}

template <typename T>
bool Stack<T>::Empty() const {
    return top_ + 1 == 0;
}

main.cpp

cpp 复制代码
#include <iostream>
using namespace std;
#include "Stack.h"
int main() {
    Stack<int> s(10);
    s.Push(1);
    s.Push(2);
    s.Push(3);
    while (!s.Empty())
    {
        cout << s.Top() << endl;
        s.Pop();
    }

    return 0;
}

// 输出
3
2
1

3、非类型模板参数

Stack2.h

cpp 复制代码
#include <stdexcept>

template <typename T, int MAX_SIZE>
class Stack2
{
public:
    Stack2();
    ~Stack2();

    void Push(const T& elem);
    void Pop();
    T& Top();
    const T& Top() const;
    bool Empty() const;

private:
    T* elems_;
    int top_;
};

template <typename T, int MAX_SIZE>
Stack2<T, MAX_SIZE>::Stack2() : top_(-1)
{
    elems_ = new T[MAX_SIZE];
}

template <typename T, int MAX_SIZE>
Stack2<T, MAX_SIZE>::~Stack2() {
    delete []elems_;
}

template <typename T, int MAX_SIZE>
void Stack2<T, MAX_SIZE>::Push(const T& elem)
{
    if (top_ + 1 >= MAX_SIZE)
    {
        throw std::out_of_range("Stack2<T>::Push stack full");
    }
    elems_[++top_] = elem;
}

template <typename T, int MAX_SIZE>
void Stack2<T, MAX_SIZE>::Pop()
{
    if (top_ + 1 <= 0)
    {
        throw std::out_of_range("Stack2<T>::Pop stack empty");
    }
    --top_;
}

template <typename T, int MAX_SIZE>
T& Stack2<T, MAX_SIZE>::Top()
{
    if (top_ + 1 <= 0)
    {
        throw std::out_of_range("Stack2<T>::Top stack empty");
    }
    return elems_[top_];
}

template <typename T, int MAX_SIZE>
const T& Stack2<T, MAX_SIZE>::Top() const
{
    if (top_ + 1 <= 0)
    {
        throw std::out_of_range("Stack2<T>::Top stack empty");
    }
    return elems_[top_];
}

template <typename T, int MAX_SIZE>
bool Stack2<T, MAX_SIZE>::Empty() const {
    return top_ + 1 == 0;
}

main.cpp

cpp 复制代码
#include <iostream>
using namespace std;
#include "Stack2.h"
int main() {
    Stack2<int, 10> s;
    s.Push(1);
    s.Push(2);
    s.Push(3);
    while (!s.Empty())
    {
        cout << s.Top() << endl;
        s.Pop();
    }

    return 0;
}

// 输出
3
2
1
相关推荐
汉克老师2 小时前
GESP六级第二课:森林里的分叉城堡——二叉树的构造与存储
c++·二叉树·树·gesp·小学生·小学生学编程
老歌老听老掉牙3 小时前
三个等价答案:遗传算法如何逼近 Branin 函数
c++·算法·ga
是main不是漫4 小时前
【双指针 | LeetCode11 盛最多水的容器】O(n²) 超时,O(n) 封神:这道“装水题“只差一个思路
开发语言·数据结构·c++·算法
慧都产品智能化5 小时前
汽车软件年底自查:按安全影响排定缺陷整改顺序
c语言·c++·测试工具·单元测试·汽车·代码规范
敲上瘾17 小时前
Qt 主窗口QMainWindow:菜单栏、工具栏、状态栏与浮动窗口等
开发语言·c++·qt·ui·qt6.3
人间乄惊鸿客1 天前
c++ 右值
c++·算法
ShineWinsu1 天前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
瑶池酒剑仙1 天前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio
郝学胜-神的一滴2 天前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光2 天前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法