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
相关推荐
小卓(friendhan2005)6 分钟前
高并发内存池
c++
汉克老师1 小时前
GESP2024年9月认证C++二级( 第三部分编程题(1) 数位之和 )
c++·循环结构·分支结构·gesp二级·gesp2级·求余数·拆数字
lxl13071 小时前
C++算法(3)二分算法
数据结构·c++·算法
星火开发设计2 小时前
C++ 异常处理:try-catch-throw 的基本用法
java·开发语言·jvm·c++·学习·知识·对象
白太岁2 小时前
C++:(3) 线程的关联、条件变量、锁和线程池
开发语言·c++
仰泳的熊猫2 小时前
题目1474:蓝桥杯基础练习VIP-阶乘计算
数据结构·c++·算法·蓝桥杯
WBluuue2 小时前
数据结构与算法:dp优化——树状数组/线段树优化
数据结构·c++·算法·leetcode·动态规划
华科大胡子2 小时前
《Effective C++》学习笔记:条款02
c++·编程语言·inline·const·enum·define
tankeven2 小时前
HJ84 统计大写字母个数
c++·算法
㓗冽3 小时前
阵列(二维数组)-基础题79th + 饲料调配(二维数组)-基础题80th + 求小数位数个数(字符串)-基础题81th
数据结构·c++·算法