简体   繁体   中英

Which data structure for this data?

I'm writing a project for a basic OOP C++ course. I have to implement sets of objects of a type Media (and derivates Book , Movie , Album ). 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.

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).

If you dont care anything, Why don't you simply use a two dimensional array or a linked 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.

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. 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. If yes, you need to consider how to manage the memory. eg Free the memory when an element is removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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