Archived

This topic is now archived and is closed to further replies.

Muzlack

Vectors -> Inventory

Recommended Posts

I''m going to make this short and sweet. I''ve recently started using vectors. I love them! I use them for almost every important resizing array now. But, for inventory, I''d like it for when you use an inventory item, for that item to be removed from the list and for the rest to move up. This vector is a "vector inv; " So, I''d like it to remove one integer from the list, and for the rest to move up the line. Thanks.

Share this post


Link to post
Share on other sites
Well, as "they" say: When all you have is a hammer, everything looks like a nail.
While vector''s are very decent for many kinds of resizable collections of items, they are not your best choice for all scenarios. In your case I''d suggesting using list''s instead. If you have followed good coding-practices all that needs to be done to implement this change is to change your inventory typedef''s from vector to list, and include <list>.

-Neophyte

Share this post


Link to post
Share on other sites
Now, I''m NOT a genious, but won''t that make it so I can''t get single element access? So if the user wanted to use a specific item, I''d have to scroll through the rest of the items? I don''t think this is the best solution. But like I said, I''m not a genious.

Share this post


Link to post
Share on other sites
Well, you are partly correct. You will not get the handy operator[] with a list, and will need to iterator through it to get a specific element. I would, however, assume that under most circumstances you''ll need to look at all the elements in the inventory anyway (showing the full inventory).

After seeing your objection though, I reread the vector interface and found the function erase. I haven''t tested wether it moves the remaining elements to take the erased element''s place, but it seems likely and it should be trivial to test.

-Neophyte

Share this post


Link to post
Share on other sites
You might want to consider how large these inventories might become. If they are quite small, use a vector or a list.
With the vector, the remove operation will be expensive, with the list, a find/search operation will be expensive, but neither will really be a problem with small amounts of objects.

If the inventories can become big, you might instead want to use a hashset(not really part of the STL, but most implementations have it anyway), where both remove/insert and search are "fast".
If you want to stay within the "standard" STL, you can use a set i believe(i think it uses a RB-tree implementation).

As always i would suggest to take a look at some datastructure material( for example "Introduction to algorithms" by Cormen, or just some internet tutorials).

Share this post


Link to post
Share on other sites
Just occurred to me that you probably will access objects in the inventory by their inventory slot number or something similar.
In that case you can use a a map or better yet a hash_map that maps a integer(inventory slot number) to an inventory object.

Share this post


Link to post
Share on other sites
If you don''t need to maintain relative ordering, you can copy the last element to the current element, and reduce the size by 1. It''s situation-dependant.

Share this post


Link to post
Share on other sites
I guess my statement was way to over-general. How do I use the erase function to remove a specific slot? It seems the only acceptable arguments are inventory.start() and inventory.end(), other than that, I can''t use integers to remove a specific slot

Share this post


Link to post
Share on other sites