Thread: stl vector class

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    stl vector class

    let's say i have an array of apple pointers:
    Code:
    apple* apples[100];
    or i use vector classes to do this:
    Code:
    vector<apple*> apples[100];
    let's say i wanted to access an item of the apple class
    Code:
    apples[0]=new apple;
    cout << apples[0]->flavor; //prints "delicious"
    does c++ allow the last snippet of code if vector classes are used? what would need to be changed?

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Your declaration of the vector is wrong, to create a vector of apple pointers with an initial size of 100 elements you wuold do this:
    Code:
    vector<apple*> apples(100);
    The rest of your code should work.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    forgot that with vector arrays i don't have to specify its size ahead of time. with a little poking and proding the program should work. thanks

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    You dont have to specify the size of a vector initially, but you must also consider how many times you are going to call push_back on the vector since this can be a very time/memory consuming operation if it is called often.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM