• Advertisement
Sign in to follow this  

question on C++ vectors

This topic is 4684 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
sorry for all the annoying questins but I have some more :)


If I have a vector filled with objects. how do I access the member functions of the objects through an iterator.


here is what I have tried and it does not seem to be working.

vector<chunk> chunkList(3);
vector<chunk>::iterator p;

p = chunkList.begin();
chunkProps props;
if (p != chunkList.end())
{
props.X = 500;
props.Y = 500;
props.mass = 5.9742e25;
props.radius = 10;

//create the chunk
*p.create(hwnd, props.X, props.Y, props.mass, props.radius);

ZeroMemory(&props, sizeof(props));
}



the compiler comes back with the following error.

error C2039: 'create' : is not a member of 'std::_Vector_iterator<_Ty,_Alloc>'
with
[
_Ty=chunk,
_Alloc=std::allocator<chunk>
]



create is a member function of the object in that slot of the vector


Share this post


Link to post
Share on other sites
You are almost right with that code. There are two things you could do to correct it:

(*p).create(hwnd, props.X, props.Y, props.mass, props.radius);

or

p->create(hwnd, props.X, props.Y, props.mass, props.radius);

Share this post


Link to post
Share on other sites
This is merely a comment for the first post:
#include <vector>
using std::vector; //No more std::vector!

vector<car> parkingLot;
...
for (int i=0;i<NUMOFCARS;i++) parkingLot.push_back(car());

Just in case you didn't know, calling the constructer of an object actually creates a new instance of that class.

You can also use linked lists if you're going to move the cars around (using pointers):
//Standard includes
vector<car*> parkingLot;
...
for (int i=0;i<NUMOFCARS;i++) parkingLot.push_back(new car);

If you don't know alot about pointers, there are plenty of articles here and on the internet for them.

Share this post


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

  • Advertisement