+3

How can I utilize classes with a person's inventory?

So let's say that there's a character with an inventory. You can also specify the amount of each item there is. We can do this with variables. int bread = 5; int gold = 10; But I also want to group these variables together for easy access. So a vector naturally comes to mind (a vector because we can add new items in the future). vector<int> Inventory { bread, gold }; This vector stores actual variables. But it seems to me that I can't change the value of `bread` or `gold` once set. So I have to come up with another solution. I was thinking maybe classes. With a class `Inventory`, I could create objects like `bread` and set a value to them. But this seems like too much work. I shouldn't have to create an entire new class, and then initialize objects just for some variables. Going back to using vectors, would references work? Are there any better ways?

10/21/2019 1:17:57 AM

Dynamic Squid

2 Answers

New Answer

+2

introducing map object. map is a collection of key and value, it's like an array but instead of indexing by 0123 you can chose whatever you want! including string intrigued ? check out here and try it yourself http://www.cplusplus.com/reference/map/map/

+1

I've heard about maps before but I really never used them. I'll be sure to cjekc that out, thanks!