Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C++
#include <iostream>
#include <vector>
using namespace std;

class item
{
    public:
        string name;
        string icon;
        //etc.
}

int main()
{
    vector<int> itemVec;
    itemVec.insert(itemVec.end, #) //# will be a variable containing the number of items created by user
    item itemVec[#] //# would be the latest vector item created
    //other content

    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:
C++
item my_item; // variable used to create items
vector<item> items; // will hold all added items
items.push_back(my_item); // add item 0
items.push_back(my_item); // add item 1
items.push_back(my_item); // add item 2

string item1name = item[1].name; // Example of accessing item #1

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.
Posted
Updated 17-Aug-16 11:56am
v7
Comments
jeron1 17-Aug-16 11:20am    
Do you want to have the user enter data, then create an object which contains that data, then add the object to a vector of these objects?
Anti-Antidote 17-Aug-16 11:58am    
I want the user to have an option to create a new 'item', which creates a new 'item#' object under the 'item' class. I need the ability to add an unknown amount of 'item#' objects, as the user may create many 'items', or perhaps only two or three.
Philippe Mori 17-Aug-16 11:22am    
I downvote your question since you don't provide code and as such it is very hard to figure out what you are trying to do...

I could guess that item[#] would be the item at that position in the vector but the what is the problem (or the question)?

You can add item to a vector by calling push_back member function.

We have no idea how you expect to use item name. Do you only need to get item by index or you need to be able to find item name of a given item?
Anti-Antidote 17-Aug-16 11:54am    
I provided code to help you understand, as well as a step-by-step process to further explain. If there's something I need to edit, please tell me! Thanks
Philippe Mori 17-Aug-16 12:32pm    
What is the purpose of itemVec? Obviously, if you don't remove items, the content of that vector will always be 0, 1, 2, 3, 4, 5... up to the number of items added minus one (or be empty if no items were added yet).

Thus, you probably want to use a vector of items.

item my_item; // variable used to create items
vector<item> items; // will hold all added items
items.push_back(my_item); // add item 0
items.push_back(my_item); // add item 1
items.push_back(my_item); // add item 2

string item1name = item[1].name; // Example of accessing item #1

1 solution

You most likely need a better vector than vector<int></int>, since you need to store details of the object rather than just its index. What information does the user provide on the request? If it is a class name then you can immediately create an object of that class and add that to the vector, with some additional information to identify exactly what class it is. Perhaps some other collection type would be better than a simple vector, or you could use the pair Structure[^] in the vector. I think your question needs a bit more detail as to exactly what these 'items' are.
 
Share this answer
 
Comments
Anti-Antidote 17-Aug-16 12:17pm    
I'm using the object's class details to store the information, I just need a way to sequentially create objects in the form 'item#' or similar, where # is the number of objects created prior to creating the object.
Richard MacCutchan 17-Aug-16 13:02pm    
Why are you making it more complex than it needs to be? You can store each new item in a vector (vector<item>), and can then read, modify, update, save them as necessary.
Anti-Antidote 17-Aug-16 13:40pm    
Do I not have to define the type as int?
Richard MacCutchan 17-Aug-16 14:40pm    
No, declare it as holding the class of objects that you are creating. you need to go and re-read the documentation on the vector class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900