i have a vector<items*> ListofItems, but i need to add one of those items to another vector, vector<myitems> ListofmyItems, using a pointer of an item in the ListofItems vector, and then the quantity of that item. How would i do that?
You want to add the items pointer to the other list? This would only create a shallow copy. (They hold the same pointer to the exact same data. Changing the data of one pointer changes the other's. This can be very dangerous and have unexpected consequences)
You could create an assignment operator/copy constructor to create a new items object and pass the pointer of that to ListofmyItems.
Are you sure you need to even use a pointer? This is exactly the sort of thing vectors are supposed to prevent. You can just have a vector<items> and then say something like:
Not the whole list, just add 1 item at a time with the users input, like say i have 5 items in the ListofItems vector, but i only want to add 3 of those to ListofmyItems vector and then i can just print out my list to the screen instead of all 5. Basically like a shopping cart, u have a list of items to buy, and then the user buys some and has his own list
You can use an enum to represent different indexes into a vector, or you could even use std::map and use strings as keys to the objects. Or you could just have a function/switch to add the right items:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
enum selection{item1,item2,item3};
void selectItem(std::vector<items> &myItems, selection s)
{
switch(s)
{
case item1: myItems.push_back(items(DATA)); break;
case item2: ...
//And so on
default: std::cout <<"Item does not exist";
}
}
//Note: If you need to use vectors, you could replicate basically this exact
//same function, but instead of a hard coded switch you could pass in a
//vector of possibilities. You'd just need to do some size checks.
//Also, drop the whole pointers thing. You don't need them.