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
相关推荐
Queenie_Charlie11 分钟前
数字去重(set)
数据结构·c++·set
Ayanami_Reii1 小时前
区间不同数的个数-树状数组/线段树/莫队/主席树
数据结构·c++·算法·线段树·树状数组·主席树·莫队
大筒木老辈子1 小时前
C++笔记---并发支持库(atomic)
java·c++·笔记
zero_hz1 小时前
核心区分:用户态/内核态切换 vs. 程序阻塞
c++·io·内核态用户态
胡萝卜3.01 小时前
深入C++可调用对象:从function包装到bind参数适配的技术实现
开发语言·c++·人工智能·机器学习·bind·function·包装器
看见繁华1 小时前
C++ 高级
开发语言·c++
点云SLAM2 小时前
constexpr 和 explicit 在 C++ 中被提出的动机
开发语言·c++·explicit关键字·隐式转换·constexpr关键字·c++11/17/20
冷崖2 小时前
工厂模式-创建型
c++·设计模式
qq_310658512 小时前
mediasoup源码走读(六)——NetEQ
服务器·c++·音视频
qq_433554543 小时前
C++树形DP(树上分组背包)
c++·算法·深度优先