Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ iterator vector struct

I created a struct with two fields of type Point3f (which is inherited from another header).

struct C_Cell {

  Point3f min, max;

};

then I created a vector of C_Cell

std::vector< C_Cell> grid;

and I have filled through grid.push_back(c) where c is a C_Cell.

Now, when I try to iterate the vector, through

for (std::vector<C_Cell>::iterator it = grid.begin() ; it != grid.end(); ++it)

during debugging, the type of it isn't C_Cell but Point3f, which is the type of the field, moreover empty.

How can I iterate correctly, in such a way to obtain a single element of type C_Cell?

Thanks ( and sorry for my english! :) )

like image 563
Mau Avatar asked Dec 09 '22 05:12

Mau


1 Answers

You have to dereference the iterator and use it like

(*it).min

or

(*it).max

or, without de-referencing,

it->min

The type of the dereferenced iterator (*it) SHOULD be C_Cell, as that's how iterators work, their operator* returns a reference to an object of the underlying type of the container. Iterators behave very much like regular pointers (although they are not regular pointers, but proxy classes), you can increment/dereference etc.

like image 52
vsoftco Avatar answered Dec 19 '22 12:12

vsoftco