OK, so this will take a bit of explaining, but I'm new to C++, so bear with me.
What I am trying to accomplish in my program is to have the user add an 'item' with certain attributes (these are my class variables). Every time the user adds another 'item', I want the program to assign another object in the format
item[#]
, where # starts from 0 and goes up from there. I have been trying to figure out vectors, and from what I have seen so far, I'll add a vector
vector<int> item;
. When the user adds another item, I want the program to add another variable
item#
(or similar, I am not quite sure) to the vector. The vector will then assign an object to the class 'item', where the user will go through the process of adding the attributes.
Again, I'm not sure exactly how well I explained this, so if I did a HORRIBLE job of this, please tell me why I did so instead of downvoting and saying nothing, as I need to be able to actually ask questions here :)
EDIT: Here is an example of what I'm trying to do
#include <iostream>
#include <vector>
using namespace std;
class item
{
public:
string name;
string icon;
}
int main()
{
vector<int> itemVec;
itemVec.insert(itemVec.end, #) item itemVec[#]
return 0;
}
There is a class 'item' with certain details.
When the user creates a new item, I wish for a new object to be created in the format 'item#', where # is the next number in the sequence of items (starting at 0)
I am considering using a vector to help in this process. If I didn't use it right, please let me know :)
ELI5, please.
What worked for me
OK, so after many an hour of heated discussion, these wonderful people finally drilled it into my think skull that the type in
std::vector<type> v
did not have to be
int
or
string
or anything. It can be used for a class. @Phillippe Mori was the one that finally got through to me that that was how it went:
item my_item; vector<item> items; items.push_back(my_item); items.push_back(my_item); items.push_back(my_item);
string item1name = item[1].name;
Thanks, all!
- Anti-Antidote
What I have tried:
I have been searching for an answer for this, but no where seems to have the answer. Arrays don't work, as I need it to be able to expand to meet the user's needs.