Thread: Storing objects in Vector.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Storing objects in Vector.

    How would i go about storing an arbitrary number of objects within a vector.

    If say I have 20 created objects, how could I loop through them and 'push_back' to the vector without manually doing each one?

    Also, whilst I am coding I can create objects easily, is there a way that the person using the program can create objects, such as adding a book to a library?

    I don't yet know about pointers, so please no advice yet on that (it will probably just confuse me more)

    Thanks,

    Darren.

    NB....this isn't homework. Currently working on chapter 9 of Stroustrup PPP.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, given a vector<T> named v, you might write:
    Code:
    v.push_back(T(1, args));
    v.push_back(T(2, args));
    // etc
    v.push_back(T(20, args));
    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".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    Well, given a vector<T> named v, you might write:
    Code:
    v.push_back(T(1, args));
    v.push_back(T(2, args));
    // etc
    v.push_back(T(20, args));
    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?

    Thanks,

    Darren.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by darren78
    Thanks for the response. I am fairly new to C++ and not totally sure what you mean by args, if you could please elaborate.
    Whatever arguments you need to invoke the constructor.

    Quote Originally Posted by darren78
    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 the object is supposed to have a name, make the name a member variable, e.g., a book has a title.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    Whatever arguments you need to invoke the constructor.


    If the object is supposed to have a name, make the name a member variable, e.g., a book has a title.
    Thanks again. It's making a little more sense now. From my readings so far, I was under the impression that when creating objects they had to be given a name.

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

    Looking at the book so far, I have accessed them by using the notation object.member.

    Thanks.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by darren78
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks. I understand now.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Bubba View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism via a vector of objects
    By Bozebo in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2010, 06:46 AM
  2. Push_back not storing data to vector
    By csonx_p in forum C++ Programming
    Replies: 13
    Last Post: 07-27-2008, 04:38 AM
  3. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  4. Call to new to create objects that go into a vector
    By matth in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2006, 02:59 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM