Advertisement Jump to content
Sign in to follow this  

vector member assignation

This topic is 3828 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

struct POINT { int x,y,z; } vector<POINT> myvector; myvector.pushback(x); How do I assign the values of a vector of type POINT?, needless to say I'm assigning and allocating with pushback.

Share this post

Link to post
Share on other sites
The correct term is assignment, but I do like assignation now that you've coined it ;)

The way you've written it at the moment you'll have to create one, and then copy it into the struct like this:
//create a point
POINT temp;
temp.x = 42;
temp.y = 73;
temp.z = 12;
myvector.push_back( temp );
//modify it
myvector.back().x = 43;
myvector.back().y = 74;
myvector.back().z = 13;

The alternative is to create a constructor:
struct POINT {
POINT( int x, int y, int z ) : x(x), y(y), z(z) {}
int x,y,z;
//create a point
myvector.push_back( POINT(42, 73, 12) );
//edit it
myvector.back() = POINT(43, 74, 13);

Share this post

Link to post
Share on other sites
Well, the vector gives you random access (access to any random member using square brackets []) and iterator access (access through an iterator). Neither seems to be significantly more efficient at accessing data.

You can push_back a ton of POINTs and use one of the two about to assign them.
ex: size_t i;                    vector[i++] = POINT(1,2,3);
ex: vector< POINT>::iterator it; (*it)++ = POINT(1,2,3);

you can learn more here:

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!