vector

This is a discussion on vector within the Game Programming forums, part of the General Programming Boards category; This is going to be difficult to explain so I will ask you after this example code...
Code:
vector<string> inventory(10, ...

vector

This is going to be difficult to explain so I will ask you after this example code...

Code:

vector<string> inventory(10, "empty slot\n");
//pseudocode
if item 10 of inventory is "empty slot" and user buys an item
then place an item in the vector over the first available "empty slot"
else
display to user that they have made an error

well, I have got all of that to work except the if item 10 of inventory is "empty slot" . I have made a program that shows that you have 10 empty slots and when you go to the store and buy an item, the first available empty slot gets taken up by the purchased item. The only problem I seem to have is I can't get the program to check whether or not item 10 is an empty slot, and if not, does not try to put an item in the already full inventory.

When using vectors there is no such thing as an empty slot. Vectors are sizable arrays. When you put something into the vector class allocates the needed memory for it and throws it into the vector container. Just as when you delete something, the vector class deallocates the memory, meaning it is not there any more. So all you need to do is have a int and increment it every time you add an item.

I really sorry, I should have been more specific when I said "empty slot". I put 10 strings in the vector that were all "empty slot". I am other strings in place of the empty slots. I'm using empty slots to help display the inventory to the user, looks rather nice in my opinion.

ok then here is the deal. using a vector to hold a string of a certian number of slots is bad design unless you are going to need lots and lots of arrays of that certian number. even in this case it is still bad practice because a vector is supposed to be thought of as a safe array. putting an array into an abstraction for arrays is completly defeating the purpose of a vector because it is making it unsafe, which is what the vector is supposed to counter. if you do the implementation i talked about you should be fine.