简体   繁体   English

该数据的哪种数据结构?

[英]Which data structure for this data?

I'm writing a project for a basic OOP C++ course. 我正在为基础OOP C ++课程编写一个项目。 I have to implement sets of objects of a type Media (and derivates Book , Movie , Album ). 我必须实现Media类型的对象集(并派生BookMovieAlbum )。 The operations on those sets are: adding an element, removing a certain element (not necessarily the first or last), search through the set (the search could return multiple results). 这些集合上的操作是:添加一个元素,删除某个元素(不一定是第一个或最后一个),搜索集合(搜索可能返回多个结果)。 Sorting is not required but I thought it would be a good addition. 排序不是必需的,但我认为这将是一个很好的补充。

So I was wondering, which would be the best data structure? 所以我想知道,哪种数据结构最好? Simple array, vector or list? 简单数组,向量还是列表? (Please notice that I must write the implementation, I can't use std classes.) I'm not actually concerned for efficiency or memory consumption since I'm not dealing with large sets of data, but I should still be able to explain why I chose one particular data structure. (请注意,我必须编写实现,不能使用std类。)由于我不处理大量数据,因此我实际上并不关心效率或内存消耗,但是我仍然应该能够解释为什么选择一个特定的数据结构。

I thought that a List would be preferable for removing and adding items, but the vector has the indexing operator [] that could be useful for the search function (which could return an array of indexes). 我认为最好使用List来删除和添加项目,但是向量具有索引运算符[],该运算符可能对搜索功能有用(它可以返回索引数组)。

If you dont care anything, Why don't you simply use a two dimensional array or a linked list ?? 如果您什么都不关心,为什么不简单地使用two dimensional arraylinked list I think its the best possible in this situation. 我认为在这种情况下是最好的。

I would do it with a list, Implemented as a binary-tree. 我会用一个列表实现,实现为二叉树。
For the search function, you can return an array of pointers. 对于搜索功能,您可以返回一个指针数组。

If you don't care about efficiency or memory consumption then you should pick the thing with the simplest implementation which is an array. 如果您不关心效率或内存消耗,则应该选择最简单的实现方法即数组。 Insert new items at the end, delete items by moving the final thing in the array into the gap. 在末尾插入新项目,通过将数组中的最后一个项目移到间隙中来删除项目。 Search by iterating over the entire array. 通过遍历整个数组进行搜索。

If you did care about efficiency, you might implement a hash table or a balanced tree. 如果您确实关心效率,则可以实现哈希表或平衡树。

I think that in your case the best container is a map. 我认为,就您而言,最好的容器是地图。 Each media has an id which is a key of this media (like book ISBN) so you cannot keep two books with the same id what exactly a map does. 每种媒体都有一个ID,它是该媒体的密钥(例如书籍ISBN),因此您不能保留两张具有相同ID的书籍,而这正是地图的作用。

As a home work, I suggest to use a simple linked list at first. 作为家庭作业,我建议首先使用一个简单的链表。 For searching, you can return an pointer or iterator(if your List class is compatible with STL container) of the item you have found. 为了进行搜索,您可以返回找到的项目的指针或迭代器(如果List类与STL容器兼容)。 And in your case, you have multiple sub-classes, you may need to put the the pointer of the base class - Media in the container. 而且,如果您有多个子类,则可能需要将基类的指针-Media放入容器中。 If yes, you need to consider how to manage the memory. 如果是,则需要考虑如何管理内存。 eg Free the memory when an element is removed. 例如,删除元素后释放内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM