#include <vector>
#include <iostream>
#include <stdexcept> // stdexcept header contains runtime_error
#include <deque>
using namespace std;
template<typename T>
class Stack {
public:
// return the top element of the Stack
const T& top() {
return stack.front();
}
// push an element onto the Stack
void push(const T& value);
// pop an element from the stack
void pop() {
stack.pop_front();
}
private:
deque<T> stack; // internal representation of the Stack
};
template<typename T>
void Stack<T>::push(const T& value) {
stack.push_front(value);
}
template <typename T>
- Class templates begin with `template` followed by a list of template parameters enclosed in angle brackets (< and >).
- Each template parameter that represents a **type** must be preceded by either of the interchangeable keywords `typename` or `class`.
- `T` acts as a placeholder for the element type.
- Can be any valid identifier
- Type parameters names must be **unique** inside a template definition.
T
to represent the element type.Animal
class template does not define it's own constructorsvector
default constructor.template
followed by the same set of template parameters as the class template.template <typename T>
void Stack<T>::push(const T& p) {
stack.push_front(p);
}
{
Stack<char> s;
s.push('P');
s.push('Q');
cout << "Stack top element is " << s.top() << endl;
s.pop();
cout << "Stack top element is " << s.top() << endl;
}
Stack top element is Q Stack top element is P
{
Stack<int> s;
s.push(10);
s.push(20);
cout << "Stack top element is " << s.top() << endl;
s.pop();
cout << "Stack top element is " << s.top() << endl;
}
Stack top element is 20 Stack top element is 10
Stack
used only a type parameter in its template declaration.template <class T, size_t N>
- Recall that keywords class and typename are **interchangeable** in template declarations.
fixeSizeArray
.// doulbe fixeSizeArray[100];
std::array<double, 100> fixeSizeArray;
template <typename T, size_t N>
class FixedStack {
public:
// return the top element of the Stack
const T& top() {
return stack.front();
}
// push an element onto the Stack
void push(const T& value) {
if (stack.size() < N)
stack.push_front(value);
else
throw runtime_error{"Stack is full"};
}
// pop an element from the stack
void pop() {
stack.pop_front();
}
private:
deque<T> stack; // internal representation of the Stack
};
{
FixedStack<char, 2> s;
s.push('A'); cout << "Added 'A' to stack" << endl;
s.push('B'); cout << "Added 'B' to stack" << endl;
s.push('C'); cout << "Added 'C' to stack" << endl;
}
Added 'A' to stack Added 'B' to stack
Standard Exception: Stack is full
template <typename T, class S, class Container = deque<T>>
which specifies that a stack uses a deque
by default to store the stack's elements of type T
.
Default type parameters must be the rightmost (trailing) parameters in a template's type-parameter list.
template <typename T, class Container=deque<T>>
class CustomStack {
public:
// return the top element of the Stack
const T& top() {
return stack.back();
}
// push an element onto the Stack
void push(const T& value) {
stack.push_back(value);
}
// pop an element from the stack
void pop() {
stack.pop_back();
}
private:
Container stack; // internal representation of the Stack
};
{
CustomStack<char> s;
s.push('P');
cout << "Stack top element is " << s.top() << endl;
s.pop();
}
Stack top element is P
{
CustomStack<char, vector<char>> s;
s.push('P');
cout << "Stack top element is " << s.top() << endl;
s.pop();
}
Stack top element is P
template <typename T>
T mymax(T x, T y) {
return (x > y) ? x : y;
}
mymax(10, 20)
20
mymax('a', '2')
'a'