Jump to content
  • Advertisement
Sign in to follow this  
VprMatrix89

vector member assignation

This topic is 3674 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
Advertisement
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: http://www.cplusplus.com/reference/stl/vector/

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!