如何将列表类转换为模板,使其能够处理任何数据
How to convert list class to template so that it work with any data?
如何将列表类转换为模板链接类,使其能够处理任何数据。下面是一个使用整数的类。我想把它转换成模板类,这样它就可以与任何数据类型的一起工作
#ifndef _INTLIST_H_
#define _INTLIST_H_
#include <iostream>
class IntListNode
{
protected:
int value;
public:
IntListNode* nxt;
IntListNode(int = 0, IntListNode* = NULL);
IntListNode(const IntListNode&);
IntListNode& operator=(const IntListNode&);
int val() const;
void val(int);
IntListNode* next() const;
};
class IntList
{
protected:
IntListNode* nhead;
IntListNode* tail;
int csize;
public:
IntList();
IntList(const IntList&);
IntList& operator=(const IntList&);
int size() const;
IntListNode* head() const;
void push(int);
void pop();
void clear();
~IntList();
};
#endif
这就是我所做的
#ifndef _INTLIST_H_
#define _INTLIST_H_
#include <iostream>
template <class T>
class IntListNode
{
protected:
T value;
public:
IntListNode* nxt;
IntListNode(int = 0, IntListNode* = NULL);
IntListNode(const IntListNode&);
IntListNode& operator=(const IntListNode&);
T val() const;
void val(T);
IntListNode* next() const;
};
template <class T>
class IntList
{
protected:
IntListNode<T> nhead;
IntListNode<T>tail;
int csize;
public:
IntList();
IntList(const IntList&);
IntList& operator=(const IntList&);
int size() const;
IntListNode* head() const;
void push(T);
void pop();
void clear();
~IntList();
};
#endif
- 将
IntLisNode
更改为ListNode
,并使其成为类模板 - 将
IntLis
更改为List
,并使其成为类模板。在List
中使用ListNode<T>
而不是IntListNode
- 在某些地方用
T
代替int
,在函数签名中用T const&
代替
这是一个快速的改造。
#ifndef _LIST_H_
#define _LIST_H_
#include <iostream>
template <typename T>
class ListNode
{
protected:
T value;
public:
ListNode* nxt;
ListNode(T const& in, ListNode* = NULL);
ListNode(const ListNode&);
ListNode& operator=(const ListNode&);
T const& val() const;
void val(T const&);
ListNode* next() const;
};
template <typename T>
class List
{
protected:
ListNode<T>* nhead;
ListNode<T>* tail;
int csize;
public:
List();
List(const List&);
List& operator=(const List&);
int size() const;
ListNode<T>* head() const;
void push(T const& val);
void pop();
void clear();
~List();
};
相关文章:
- 使用流处理接收到的数据
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 我可以使用哪种数据结构来处理这种方式
- OpenCV(C++)处理来自JavaScript / Web Assembly的图像数据
- 在线程函数中处理数据向量时进行线程竞速
- 而(getline(fin,str)){}即使在到达eof之后仍在处理数据
- 如何以低于相机每秒帧数的能力处理数据?
- 处理数据包的设计模式
- C 图像处理数据块进入数组 /指针
- 如何使用 C++ 驱动程序在 MongoDB 中使用 UserException 处理数据类型错误
- 实时绘图的信号处理数据(c++中的FFT和GNUPLOT)
- 处理数据的方法,速度是我处理速度的两倍
- 如何使用STL迭代器和reverse_iterator处理数据
- MFC树视图控件:寻找一种万无一失的方法来处理数据
- 从套接字递归处理数据
- boost::p roperty_:tree - 解析和处理数据
- 这段代码是否在作为参数传递时处理数据有点困惑
- 神经网络似乎工作得很好,直到用于处理数据(所有的结果几乎是相同的)
- 在托管c#应用程序的非托管c++ Dll调用中,如何处理数据类型之间的冲突