vector

This is a discussion on vector within the C++ Programming forums, part of the General Programming Boards category; Hi, I got a class student. And a vector<student> listOfStudents. Wanted to ask,which is best way to store this vector. ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    vector

    Hi,
    I got a class student.

    And a vector<student> listOfStudents.

    Wanted to ask,which is best way to store this vector.

    Code:
    vector<student> listOfStudents;   //method A
    vector<student*> listOfStudents;   //method B
    Which of the ways is the best way?method a or method b?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    It depends on the class student.

    The first options is usually the best place to start, but if student is a base class, or if it is expensive or illegal to copy, then the second option might be better.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you're gonna store pointers, you might as well store smart pointers.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    There is no way that can be answered for the specific case without giving the definition of student.
    I mean it could be a typedef for short for all we know...

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    if student has no polymorphic methods and a default constructor, storing student by value may be a good option. else, store by pointer.

    if you have the option to do either, the best option depends on the usage context.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21