Storing objects in Vector.

This is a discussion on Storing objects in Vector. within the C++ Programming forums, part of the General Programming Boards category; How would i go about storing an arbitrary number of objects within a vector.
If say I have 20 created ...

and thus populate the vector with those 20 objects. If they are all (initially) the same as you create the vector, you could just write:

Code:

vector<T> v(20, T(whatever, args));

As for your users creating objects: when your user performs some action to add a book to the library, your program then correspondingly uses push_back on the vector. The user himself/herself should be thinking "adding a book to the library", not "adding a book object to a vector".

and thus populate the vector with those 20 objects. If they are all (initially) the same as you create the vector, you could just write:

Code:

vector<T> v(20, T(whatever, args));

As for your users creating objects: when your user performs some action to add a book to the library, your program then correspondingly uses push_back on the vector. The user himself/herself should be thinking "adding a book to the library", not "adding a book object to a vector".

Thanks for the response. I am fairly new to C++ and not totally sure what you mean by args, if you could please elaborate.

Also, the problem I am having with user creating objects, is how to give the created object a name? How do you set that initial object name?

If I create an object without a name, how do I access the data relating to a specific object?

In the case of a vector, you could refer to the object by index. Perhaps you could search for the object in the vector by name, id, etc. Or perhaps instead of using a vector, you use a map, mapping the name, id, or some other key to the object.

If you intend on removing books and need to access the books later based on an ID or a key then a map is the best choice. Vector is great until you realize that when you remove an object it invalidates all iterators beyond the point of removal. This means that if you are using the index in the vector as the book ID the moment you remove all of the stored IDs for books that lie beyond the point of removal those IDs are all incorrect. You can remedy this via a listener/observer pattern but the best choice is to use the right container for the job.

If you intend on removing books and need to access the books later based on an ID or a key then a map is the best choice. Vector is great until you realize that when you remove an object it invalidates all iterators beyond the point of removal. This means that if you are using the index in the vector as the book ID the moment you remove all of the stored IDs for books that lie beyond the point of removal those IDs are all incorrect. You can remedy this via a listener/observer pattern but the best choice is to use the right container for the job.

Thanks Bubba. I will take a look at maps. The reason I have chosen vectors is that I am currently working through a book that has not yet reached maps, but I will take a look at them for this task.