# How can I store spell & items using a std::vector implementation?

I'm following along with a book from GameInstitute right now, and it's asking me to:

Allow the player to buy and carry healing potions and potions of fireball. You can add an Item array (after you define the item class) to the Player class for storing them, or use a std::vector to store them.

I think I would like to use the std::vector implementation, because that seems to confuse me less than making an item class, but I am unsure how to do so. I've heard from many people that vectors are great ways to store dynamic values (such as items, weapons, etc), but I've not seen it used.

• You'll need a Item class in either way. A array of items would be Item items[INVENTORY_SIZE]; A std::vector of items would be std::vector<Item> items; A std::vector is just a array which size can be changed dynamically. – API-Beast Nov 5 '12 at 16:32
• To store objects in a vector they must be objects of the same type. The way to do that is to create an Item vector (where Item is an interface for all items that can be picked up) and classes for each of your item types (healing potion and fireball potion). As long as the potion classes implement the Item interface you can store them in the vector (although as things become more complicated you might want to add more interfaces for things like consumable items, stackable items, or just potions themselves. But for simplicity just make one class per item and the Item interface) – Benjamin Danger Johnson Nov 5 '12 at 17:06

## 1 Answer

std::vector is a fine way to store "dynamic" (as you call them) things like items, but the real benefit of the vector is not that the thing you are storing can change, but that the number of items in the vector can change without minimal effort on your part. To illustrate, were you to have store your Item objects as an array, you'd have to fix the size of the array at compile-time (Item items[SIZE];), which means you have a fixed upper limit on items (among other issues, which aren't as relevant to this specific subject and so I will skip over them).

You could also dynamically-allocate the array at runtime (Item * items = new Item[SIZE];) which would let you resize the array later by allocating new storage, copying the items over, and deleting the old storage. This, however, is a lot more work for you to do.

Fortunately, that's what std::vector does for you -- it is a dynamically-sized array implementation, essentially, and handles the memory management for growing the array beyond its current capacity for you. It's definitely the option I'd suggest you pursue for now -- but note that you'll still want to create an Item class.

It's very easy to use:

// Create an item list and two item objects to add to it.
std::vector<Item> items;
Item fireballPotion("Potion of Fireball");
Item healingPotion("Potion of Healing");

// Add the items:
items.push_back(fireballPotion);
items.push_back(healingPotion);

// operator[] is supported for accessing items.
// This will print "Potion of Fireball" for example:
std::cout << items[0].GetName();


The MSDN documentation for the vector class is probably worth a read, and if you aren't familiar with templates -- which is what vector uses to allow it to store "anything" -- you should brush up on the basics there as well. Which also leads me to a final point: while it looks like a vector can store anything, it does have constraints for what is allowed in it and these sometimes throw beginners. In particular, the type you store in a vector must be copyable, because the vector will need to make a copy of the objects when resizing its internal storage, for example. C++'s rule of three is something to keep in mind here.

• Thank you, this was clear and concise. I'm reading over the documentation now- When I have 15 rep, I'll make sure to come back and vote you up! – Vladimir Marenus Nov 5 '12 at 17:00