question on C++ vectors

Started by
12 comments, last by deadimp 18 years, 11 months ago
Say I have a class named car. and I want to fill a vector of an unknown amount of cars, is this the right way to go about doing that?

#include <vector>
#include <time.h>

//seed the randome number generator
srand(time(0));

//create a random number for the amount of cars
int NUMOFCARS = rand( ) % 24;


std::vector<car> parkingLot;


//fill the vector with NUMOFCARS cars
for(int i = 0; i < NUMOFCARS; i++)
    {
      car tempcar;
      parkingLot.push_back(tempcar);
    }


/*
that doesn't seem right to me but I dont have much experience with vectors so I am asking.
*/

//or sould I do the following?

//fill the vector with NUMOFCARS cars
for(int i = 0; i < NUMOFCARS; i++)
    {
      parkingLot.push_back();
    }


thanks
These tears..leave scars...as they run down my face.
Advertisement
Seems fine to me.

Alternatively you could use:

std::vector<car> parkinglot(NUMOFCARS);

which will do the same thing (ie populate parkinglot with NUMOFCARS cars all default constructed).

Jim.
You might want to look into the vector::resize function. It allows you to specify how many objects you want in the vector and also allows you to specify the value for new elements. So your code could either look like this:
car tempcar;parkingLot.resize(NUMOFCARS,tempcar);

or
parkingLot.resize(NUMOFCARS);


Hope that helps.
cool thanks guys!
These tears..leave scars...as they run down my face.
ok now about iterators....


If I make a vecotr of cars
can I make an iterator like this...

vector<car>::iterator iter;

If that is how you do it then I must not be setting up the compier properly because I get the following error
.\gravSim.cpp(7) : error C2143: syntax error : missing ';' before '<'.\gravSim.cpp(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int.\gravSim.cpp(7) : error C2039: 'iterator' : is not a member of '`global namespace''


if I am doing this wrong could you point me in the right direction to find out?

thanks
These tears..leave scars...as they run down my face.
try

std::vector<car>::iterator iter;

instead of


vector<car>::iterator iter;


Cheers,

bob

[size="3"]Halfway down the trail to Hell...
try

std::vector<car>::iterator iter;

instead of


vector<car>::iterator iter;


Cheers,

bob

[size="3"]Halfway down the trail to Hell...
On the line previous to

vector<car>::iterator iter

Are you missing the semi-colon?

Or have you got another invalid statement such as an else if without a condidtion?
Gary.Goodbye, and thanks for all the fish.
damn std namespace!

I can sometimes be a rather large idiot
These tears..leave scars...as they run down my face.
Also, when you resize a vector you can pass in the value that all the new elements should have.

Like this:

std::vector <int> vect;
vect.resize("newsize", "value of new elemts");

You can find all the vector members at msdn (but you probarly knew)... but I link anyway :P

Correct me if Im wrong, Ive only used vectors in one game so far. :P Just tryin to help ya know. ;) Cheers!

This topic is closed to new replies.

Advertisement