类模板

来源:互联网 发布:如何优化win10开机速度 编辑:程序博客网 时间:2024/06/10 20:13
//Queue.h
#ifndef queue_h
#define queue_h
// declaration that Queue is a template needed for friend declaration in QueueItem
template <class Type> class Queue;
// function template declaration must precede friend declaration in QueueItem
template <class T>
std::ostream& operator<<(std::ostream&, const Queue<T>&);
template <class Type> class QueueItem {
friend class Queue<Type>;
// needs access to item and next
friend std::ostream& operator<< <Type> (std::ostream&, const Queue<Type>&);
// private class: no public section
QueueItem(const Type &t): item(t), next(0) {}
Type item; // value stored in this element
QueueItem *next; // pointer to next element in the Queue
};
template <class Type> class Queue {
// needs access to head
friend std::ostream& operator<< <Type> (std::ostream&, const Queue<Type>&);
public:
// empty Queue
Queue(): head(0), tail(0) { }
// construct a Queue from a pair of iterators on some sequence
template <class It>
Queue(It beg, It end):
head(0), tail(0) { copy_elems(beg, end); }
// copy control to manage pointers to QueueItems in the Queue
Queue(const Queue &Q): head(0), tail(0)
{ copy_elems(Q); }
Queue& operator=(const Queue&); // left as exercise for the reader
~Queue() { destroy(); }
// replace current Queue by contents delimited by a pair of iterators
template <class Iter> void assign(Iter, Iter);
// return element from head of Queue
// unchecked operation: front on an empty Queue is undefined
Type& front() { return head->item; }
const Type &front() const { return head->item; }
void push(const Type &);
void pop();
bool empty() const { // true if no elements in the Queue
return head == 0;
}
private:
QueueItem<Type> *head; // pointer to first element in Queue
QueueItem<Type> *tail; // pointer to last element in Queue
// utility functions used by copy constructor, assignment, and destructor
void destroy();
void copy_elems(const Queue&);
// version of copy to be used by assign to copy elements from iterator range
template <class Iter> void copy_elems(Iter, Iter);
};
// Inclusion Compilation Model: include member function definitions as well
#include "Queue.cpp"
#endif


//Queue.cpp
template <class Type>
std::ostream& operator<< (std::ostream &os, const Queue<Type> &q) {
os << "< ";
QueueItem<Type> *p;
for (p = q.head; p; p = p->next)
os << p->item << " ";
os <<">";
return os;
}
template <class Type> void Queue<Type>::destroy() {
while (!empty())
pop();
}
template <class Type> void Queue<Type>::pop() {
// pop is unchecked: Popping off an empty Queue is undefined
QueueItem<Type> *p = head; // keep pointer to head so we can delete it
head = head->next; // head now points to next element
delete p; // delete old head element
}
template <class Type> void Queue<Type>::push(const Type &val) {
// allocate a new QueueItem object
QueueItem<Type> *pt = new QueueItem<Type>(val);
// put item onto existing queue
if (empty())
head = tail = pt; // the queue now has only one element
else {
tail->next = pt; // add new element to end of the queue
tail = pt;
}
}
template <class Type> template <class It>
void Queue<Type>::copy_elems(It beg, It end) {
while (beg != end) {
push(*beg);
++beg;
}
}
template <class T> template <class Iter>
void Queue<T>::assign(Iter beg, Iter end) {
destroy(); // remove existing elements in this Queue
copy_elems(beg, end); // copy elements from the input range
}
template <class Type>
void Queue<Type>::copy_elems(const Queue &orig) {
// copy elements from orig into this Queue
// loop stops when pt == 0, which happens when we reach orig.tail
for (QueueItem<Type> *pt = orig.head; pt; pt = pt->next)
push(pt->item); // copy the element
}
template <class Type>
Queue<Type>& Queue<Type>::operator=(const Queue &rhs) {
destroy();
head=rhs.head;
tail=rhs.tail;
return *this;
}

//main.cpp
#include <iostream>
#include "Queue.h"
#include<vector>
int main()
{
short a[4] = { 0, 3, 6, 9 };
Queue<int> qi(a, a + 4); // copies elements from a into qi
vector<int> vi(a, a + 4);
qi.assign(vi.begin(), vi.end());
std::cout<<qi;
return 0;
}
原创粉丝点击