Jump to content
  • Advertisement
Sign in to follow this  
donjonson

question on C++ vectors

This topic is 4927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!