# vector member assignation

## 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 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 pointPOINT temp;temp.x = 42;temp.y = 73;temp.z = 12;myvector.push_back( temp );//modify itmyvector.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 pointmyvector.push_back( POINT(42, 73, 12) );//edit itmyvector.back() = POINT(43, 74, 13);

##### 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);