This is a small section of code I have below, which is basically how to create an inventory program for beginners, which I am.
In this programs case, I think, the vector would be used to keep track of the amount of currency/gold a user has and then after the user is finished the elements of the inventory would be displayed.
From what I have gathered the functions necessary for the utilization of a vector in a program like this would be push_back.
what is the vector for? Each customer?
some rough ideas..
add a 'is deleted' bool to the data structure. then you can 'delete' items without the mess on the fly and clean up a bunch of them at once every now and then.
add an 'is dirty' type field as well. (this is above the vector, not inside it)
use push back to add a new one. any additions triggers is-dirty.
each entry needs a key.
sort the vector by the key before doing any binary search on the keys to find an entity, if is-dirty. If not, its still sorted from last time.
from time to time sort off is-deleted, remove the deleted ones off the back end using vector's tools
resort on key.
if resorting all the time is a mess you can track which portion is sorted and binary search that part and linear search the recent additions until you get enough additions to justify the sort.
if you want to get extra fancy you can do stuff like custom comparison for the sort that throws the deleted to the end, track where those start, and overwrite them (instead of push-back) with new additions. This is more complex but it eliminates the need to remove the deleted stuff.
if its not obvious yet, you need this format for the things I am saying:
class external
{
bool is_dirty;
other stuff;
vector<inner_datastore_class> v;
external my_push_back(inner_datastore_class & x) {v.push_ back(x); v.sort(..); etc;}
};
the external wrapper may look like the vector -- it won't do much more than provide access to the vector, but it will have just a little extra logic and a couple of extra variables to help do the things you may want to do.
not going to chase down links but amount of gold does not need a vector. The only use I see of having a vector is to keep a history of their money as they earn more and buy stuff etc. You could use it for that... is the assignment just to add a vector for the sake of adding a vector? Or is it the inventory, so you can see that you have 10 tents and 3 swords or whatever?
If its the inventory, you have 2 approaches.
1) make a vector of integers (init to zero), 1 for each product. looks like
enum {tent, sword, potion, emax} ; vector<int> inventory(emax); inventory[tent]++; //bought a tent.
2) a vector of the thingys, so you have {tent, potion, potion, tent, sword, ...} for all the purchases.
I am a big fan of approach 1 for small problems like this.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
struct Item // used to store a single item from the store.
{
std::string name;
int price;
};
class Store // used to handle everything to do with the store
{
public:
Store(const std::vector<Item>& itemList, int n);
~Store() {}
public:
std::string BuyItem(int item);
std::string viewInventory();
std::string ListItems();
int getMoney()
{
return std::max(money, 0);
}
private:
std::vector<Item> inventory;
std::vector<Item> forSale;
int money;
};
int main()
{
std::vector<Item> f(3);
f[0].name = "Clown";
f[0].price = 2;
f[1].name = "Cracker Jack";
f[1].price = 6;
f[2].name = "Camel";
f[2].price = 9;
Store s = Store(f, 3);
bool quit = false;
int input;
while (!quit)
{
do
{
std::cout << "Welcome to the store.\n"
<< "You have " << s.getMoney()
<< " dollars.\n"
<< "\nWhat would you like to do?\n"
<< s.ListItems()
<< "[4]View your inventory\n"
<< "[5]Leave\n";
std::cin >> input;
} while (input < 1 || input > 5);
switch (input)
{
case 4:
std::cout << s.viewInventory();
break;
case 5:
quit = true;
break;
default:
std::cout << s.BuyItem(input);
}
}
std::cout << "\nSee ya!\n";
}
Store::Store(const std::vector<Item>& itemList, int n)
{
for (int i = 0; i < n; i++)
{
forSale.push_back(itemList[i]);
}
money = 20;
}
std::string Store::BuyItem(int item)
{
money -= forSale[item - 1].price;
if (money < 0)
{
return"\nSorry, you don't have enough money.\n\n";
}
inventory.push_back(forSale[item - 1]);
return"You bought a " + forSale[item - 1].name + '\n';
}
std::string Store::ListItems()
{
std::string s;
for (unsignedint i = 0; i < forSale.size(); i++)
{
s += "[";
s += i + 49;
s += "] Buy a ";
s += forSale[i].name;
s += " ($";
s += forSale[i].price + 48;
s += ")\n";
}
return s;
}
std::string Store::viewInventory()
{
std::string s = "\n";
for (unsignedint i = 0; i < inventory.size(); i++)
{
s += inventory[i].name + '\n';
}
return s + '\n';
}
Welcome to the store.
You have 20 dollars.
What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
3
You bought a Camel
Welcome to the store.
You have 11 dollars.
What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
2
You bought a Cracker Jack
Welcome to the store.
You have 5 dollars.
What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
1
You bought a Clown
Welcome to the store.
You have 3 dollars.
What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
1
You bought a Clown
Welcome to the store.
You have 1 dollars.
What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
4
Camel
Cracker Jack
Clown
Clown
Welcome to the store.
You have 1 dollars.
What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
5
See ya!