当前位置: 首页> 娱乐> 明星 > C++之模板(二)

C++之模板(二)

时间:2025/7/8 21:43:56来源:https://blog.csdn.net/CSDN_HZW/article/details/139760265 浏览次数:0次

1、类模板

2、使用类模板

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

Stack.h

#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

#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

#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

#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
关键字:C++之模板(二)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: