C++中能不能用结构体模板
推荐于2016-05-15
#ifndef QUEUE_H_#define QUEUE_H_#include <iostream>
template<class T> class Queue;
template<class T> std::ostream &operator<<(std::ostream &, const Queue<T> &);
template<class Type> class QueueItem{ friend class Queue<Type>; friend std::ostream & operator<< <Type>(std::ostream &, const Queue<Type> &); QueueItem(const Type &it): item(it), next(0) { } Type item; QueueItem *next;};
template<class T>class Queue{ friend std::ostream & operator<< <T>(std::ostream &, const Queue<T> &);public: Queue(): head(0), tail(0) { } Queue(const Queue &c); Queue &operator=(const Queue &rhs); template<class Iter> Queue(Iter beg, Iter end): head(0), tail(0) { copy_elements(beg, end); } ~Queue(){ destroy(); }public: template<class Iter> void assign(Iter beg, Iter end); void push(const T &); void pop(); T &front() { return head->item; } const T &front() const { return head->item; } bool empty() const { return head == 0; }private: QueueItem<T> *head; QueueItem<T> *tail; //utility functions used by copy constructor, assignment and destructor void copy_elements(const Queue &); void destroy();private: template<class Iter> void copy_elements(Iter, Iter);};
template<> voidQueue<const char *>::push(const char *const &s){ char *new_item = new char[strlen(s) + 1]; strncpy(new_item, s, strlen(s) + 1); QueueItem<const char *> *p = new QueueItem<const char *>(new_item); if(empty()) head = tail = p; else{ tail->next = p; tail = p; }}
template<> void Queue<const char *>::pop(){ QueueItem<const char *> *p = head; head = head->next; delete []p->item; delete p;}
template<class T>void Queue<T>::push(const T &v){ QueueItem<T> *p = new QueueItem<T>(v); if(empty()){ head = tail = p; }else{ tail->next = p; tail = p; }}
template<class T> void Queue<T>::pop(){ QueueItem<T> *p; p = head; head = head->next; delete p;}
template<class T>Queue<T>::Queue(const Queue &c): head(0), tail(0){ copy_elements(c);}
template<class T>void Queue<T>::copy_elements(const Queue &c){ for(QueueItem<T> *p = c.head; p; p = p->next) push(p->item);}
template<class T> template<class Iter>void Queue<T>::copy_elements(Iter beg, Iter end){ for(; beg != end; ++beg) push(*beg);}
template<class T> template<class Iter>void Queue<T>::assign(Iter beg, Iter end){ destroy(); copy_elements(beg, end);}
template<class T> void Queue<T>::destroy(){ while(!empty()) pop();}
template<class T>Queue<T> &Queue<T>::operator=(const Queue &rhs){ destroy(); copy_elements(rhs); return *this;}
template<class T> inline std::ostream &operator<<(std::ostream &os, const Queue<T> &q){ if(q.empty()) return os; QueueItem<T> *p; os << "< "; for(p = q.head; p; p = p->next) os << p->item << " "; os << ">"; return os;}
template<> inline std::ostream &operator<<(std::ostream &os, const Queue<const char *> &q){ if(q.empty()) return os; QueueItem<const char *> *p; os << "< "; for(p = q.head; p!= q.tail; p = p->next) os << "\"" << p->item << "\"" << ", "; os << "\"" << p->item << "\"" << " >"; return os;}
#endif
2023-08-15 广告
注意只有在定义结构体时那个模板名不需要加模板参数列表<T>,之后应用该模板都应该加<T>
如这里的node<T>
#include <iostream>
#include <stdlib.h>
using namespace std;
template <class T>
struct node{
T data;
node<T>* next;
};
template <class T>
node<T>* CreateList(int len, T a[])
{
node<T>* head = new node<T>;
node<T>* tmp = head;
for (int i = 0; i < len; i++)
{
tmp->data = a[i];
if (i == len - 1)
{
tmp->next = NULL;
continue;
}
node<T> * newNode = new node<T>;
tmp->next = newNode;
tmp = newNode;
}
return head;
}
template <class T>
void Show(node<T>* head)
{
for (node<T> * tmp = head; tmp!= NULL; tmp = tmp->next)
cout << tmp->data << " ";
cout << endl;
}
int main()
{
int a[3] = {1,2,3};
node<int>* head = CreateList(3, a);
Show(head);
return 0;
}
2013-04-21
template <typename T>
struct Node
{
T data;
Node* next;
};