user3085497
user3085497

Reputation: 121

Vector Object Invetory, Object that can store other Object types?

I'm trying to create a Inventory system that can hold any object

for example

struct Ore{
    string name;
    int Size;
};

struct Wood{
    string name;
    int size;
    int color;
};

my idea is to create a struct with 2 vectors, one for Numeric numbers, like items with Attack, Deffense and stuff, and the other vector for Name,description or other text. With multiple constructors for different item types.

the problem I have with it is ive heard vectors can take up more memory, and i expect this program to create hundreds or thousands of items.

So i was looking for any suggestion for bettery memory storage.

 struct Invetory{
 vector<float> Number;
 vector<string> Word;

   Invetory(string n,float a) 
   {Word.push_back(s); Number.push_back(a)}

   Invetory(string n,float a, float b)
   {Word.push_back(s); Number.push_back(a); Number.push_back(b);}
 };

 vector<Invetory>Bag_Space;

Upvotes: 0

Views: 45

Answers (1)

Spundun
Spundun

Reputation: 4034

You are trying to optimize too early.

Go with whatever is the cleanest thing to use. vectors are not an insane choice. (Using arrays or std::vectors in C++, what's the performance gap?)

Deal with a performance issue if/when it arises.

Checkout the following discussions on premature optimizations.


BTW I stumbled upon this interesting discussion on potential performance issues with vectors. In summary, it's saying that if your vectors are shrinking, then the memory footprint won't shrink with the vector size unless you call swap function. And if you are making a lot of vectors and don't need to initialize it's elements to 0s, then instead of

vector<int> bigarray(N);

try

vector<int> bigarray;
bigarray.reserve(N);

Upvotes: 1

Related Questions