template <typename 타입이름>
class 클래스템플릿이름{
// 클래스 멤버 선언
}
클래스의 일반화된 선언이다.
함수 템플릿과 동작은 같으며, 그 대상이 클래스라는 점만 다르다.
#include <iostream>
using std::cout, std::endl;
template<typename T>
class Queue {
private:
size_t _size;
size_t _capacity;
T* _items;
public:
Queue()
: _size(0)
, _capacity(4)
, _items(new T[_capacity]) {
}
~Queue() {
delete[] _items;
}
void push(T item) {
if (_size < _capacity) {
_items[_size++] = item;
}
else {
size_t newCapacity = _capacity * 2;
T* oldItems = _items;
T* newItems = new T[newCapacity];
std::copy(oldItems, oldItems + _capacity, newItems);
//copy(src 복사를 시작할 배열 주소, src 복사를 끝낼 마지막 배열 주소, dest);
_capacity = newCapacity;
_items = newItems;
delete[] oldItems;
push(item);
}
}
void cap() {
cout << "capacity : " << _capacity << endl;
}
void pop() {
--_size;
}
T& top() {
return _items[_size - 1];
}
};
int main() {
Queue<int> q0;
q0.push(1);
q0.push(2);
q0.push(4);
cout << q0.top() << endl;
q0.pop();
cout << q0.top() << endl;
q0.push(4);
q0.push(4);
q0.push(4);
q0.cap();
Queue<std::string> q1;
q1.push("abc");
q1.push("123");
q1.push("ABC");
cout << q1.top() << endl;
q1.pop();
cout << q1.top() << endl;
}
4
2
capacity : 8
ABC
123
#include <iostream>
using std::cout, std::endl;
template<typename T, int N>
class Queue {
private:
size_t _size;
T _items[N];
public:
Queue()
: _size(0)
, _items{} {
}
void push(T item) {
if (_size < N)
_items[_size++] = item;
else
throw std::out_of_range("overflow");
}
void pop() {
if (_size == 0)
throw std::out_of_range("underflow");
--_size;
}
T& top() {
return _items[_size - 1];
}
};
int main() {
Queue<int, 3> q0;
q0.push(1);
q0.push(2);
q0.push(4);
cout << q0.top() << endl;
q0.pop();
cout << q0.top() << endl;
Queue<std::string, 1> q1;
try {
q1.push("abc");
q1.push("123");
q1.push("ABC");
cout << q1.top() << endl;
q1.pop();
cout << q1.top() << endl;
}
catch (std::out_of_range& e) {
cout << e.what() << endl;
}
}
4
2
overflow
template<typename T, int N>
class Queue {
private:
size_t _size;
T _items[N];
public:
Queue();
void push(T item);
void pop();
T& top();
};
template<typename T, int N>
Queue<T, N>::Queue()
: _size(0)
, _items{} {
}
template<typename T, int N>
void Queue<T, N>::push(T item) {
if (_size < N)
_items[_size++] = item;
else
throw std::out_of_range("overflow");
}
template<typename T, int N>
void Queue<T, N>::pop() {
if (_size == 0)
throw std::out_of_range("underflow");
--_size;
}
template<typename T, int N>
T& Queue<T, N>::top() {
return _items[_size - 1];
}
#include <iostream>
#include <vector>
using std::cout, std::endl;
template<typename T>
class Queue {
private:
std::vector<T> _items;
public:
Queue();
void push(T item);
void pop();
T& top();
};
template<typename T>
Queue<T>::Queue()
: _items{} {
}
template<typename T>
void Queue<T>::push(T item) {
_items.push_back(item);
}
template<typename T>
void Queue<T>::pop() {
if (_items.size() == 0)
throw std::out_of_range("underflow");
_items.pop_back();
}
template<typename T>
T& Queue<T>::top() {
return _items.back();
}
int main() {
Queue<int> q0;
q0.push(1);
q0.push(2);
q0.push(4);
cout << q0.top() << endl;
q0.pop();
cout << q0.top() << endl;
Queue<std::string> q1;
try {
q1.push("abc");
q1.push("123");
q1.push("ABC");
cout << q1.top() << endl;
q1.pop();
cout << q1.top() << endl;
}
catch (std::out_of_range& e) {
cout << e.what() << endl;
}
}
4
2
ABC
123