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
相关推荐
无限进步_6 分钟前
C语言实现贪吃蛇游戏完整教程【最终版】
c语言·开发语言·c++·git·游戏·github·visual studio
被星1砸昏头13 分钟前
自定义操作符高级用法
开发语言·c++·算法
嘿BRE24 分钟前
【C++】智能指针
c++
楼田莉子32 分钟前
Linux学习之库的原理与制作
linux·运维·服务器·c++·学习
浅念-44 分钟前
C++第一课
开发语言·c++·经验分享·笔记·学习·算法
charlie1145141911 小时前
现代嵌入式C++教程:对象池(Object Pool)模式
开发语言·c++·学习·算法·嵌入式·现代c++·工程实践
HABuo1 小时前
【linux进程控制(三)】进程程序替换&自己实现一个bash解释器
linux·服务器·c语言·c++·ubuntu·centos·bash
一只小bit1 小时前
Qt 多媒体:快速解决音视频播放问题
前端·c++·qt·音视频·cpp·页面
凯子坚持 c1 小时前
C++大模型SDK开发实录(二):DeepSeek模型接入、HTTP通信实现与GTest单元测试
c++·http·单元测试
uoKent1 小时前
c++中的运算符重载
开发语言·c++