类模板
来源:互联网 发布:如何优化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;
}
#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;
}
- 函数模板、类模板
- 模板 - 类模板
- 模板函数,模板类
- 模板的模板类
- 类模板(函数模板)
- 模板之---类模板
- C++模板:模板类
- 函数模板 类模板
- 函数模板、类模板
- C++模板学习(函数模板+类模板)
- 模板——函数模板、类模板
- 模板 Templates 模板函数 模板类
- C++模板(类模板、函数模板)
- 模板之类模板(数组类模板)
- 类模板
- 类模板
- 类模板
- 类模板
- C++ Primer 之队列类模板
- 商务签证的准备工作(2)——DS-160表格的填写
- Python学习---读过《深入Python3》有感2
- contacts里QuickContactBadge弹出窗口
- Firefox7来了,你的上传图片及时预览功能还能Hold住吗?
- 类模板
- Android 使用AsyncTask 后监听异步加载完毕的动作
- c陷进与缺陷笔记(4)
- jquery实现图片上传前的预览
- ASP.NET Web Service如何工作
- 如何配置JAVA环境变量
- VBA AutoExec 宏
- java三大框架整合做权限管理操作
- sql 修改sa密码的方法