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
相关推荐
李日灐7 小时前
C++STL: list(双链表) 简单介绍,了解迭代器类型,list sort 的弊端
开发语言·c++·list
打不了嗝 ᥬ᭄7 小时前
【Linux】多路转接 Select , Poll和Epoll
linux·网络·c++·网络协议·http
啊森要自信8 小时前
【C++的奇迹之旅】map与set应用
c语言·开发语言·c++
pu_taoc8 小时前
ffmpeg实战4-将PCM与YUV封装成MP4
c++·ffmpeg·pcm
2301_803554528 小时前
Pimpl(Pointer to Implementation)设计模式详解
c++·算法·设计模式
John_ToDebug8 小时前
从零开始:在 Windows 环境下拉取并编译 Chrome 源码全纪录
c++·chrome·windows
Dream it possible!8 小时前
LeetCode 面试经典 150_图的广度优先搜索_蛇梯棋(93_909_C++_中等)(广度优选搜索)
c++·leetcode·面试·广度优先
进击的荆棘8 小时前
C++起始之路——类和对象(上)
开发语言·c++
草莓熊Lotso9 小时前
《算法闯关指南:动态规划算法--斐波拉契数列模型》--04.解码方法
c++·人工智能·算法·动态规划
1nv1s1ble9 小时前
[c++] cpp快速添加sqlite_orm
c++·sqlite