-
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?
-
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.
-
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
-
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.