无法理解未排序列表和通用数据类型

Trouble understanding Unsorted Lists and generic data types?

本文关键字:数据类型 列表 排序      更新时间:2023-10-16

因此,我们在今天的Comp Sci 2课上了解了未排序列表-老师提到,使用未排序列表,您可以创建模拟任何数据的通用数据类型-但对该数据的操作被定义为特定的。这本书给出的例子如下:

#include “ItemType.h”
class UnsortedType // declares a class data type
{
public : 
// 8 public member functions
     void UnsortedType ( );
     bool IsFull ( ) const; 
     int GetLength ( ) const ; // returns length of list
     ItemType GetItem ( ItemType item, bool& found);
     void PutItem ( ItemType item ); 
     void DeleteItem ( ItemType item ); 
     void ResetList ( );
     ItemType GetNextItem (); 
private :
// 3 private data members
     int length; 
     ItemType info[MAX_ITEMS]; 
     int currentPos;
};

我的问题是:当我试图重新创建这样的东西时,我是否必须制作一些专门称为ItemType的类?还是ItemType是要存储在未排序列表中的任何对象的占位符?(例如,您是否可以将ItemType替换为:phonebookContacts,其中phonebookContact类是一个包含电话簿中人员的联系信息的对象?)

是的,您可以用phonebookContact替换ItemType。

只需将每个ItemType替换为phonebookContact,并#包含一个类似phonebookContact.h的标头,而不是ItemType.h。标头phonebookContact.h需要包含phonebook联系人类的定义,因为ItemType.h可能包含ItemType类的定义。

阅读c++模板。模板化的类。以下是一个示例http://www.cplusplus.com/doc/tutorial/templates/