As the title says, I'm trying to work with iterators in a class object. So far I haven't done anything beyond conventional for-loops and I'm a little uncertain on how to do this properly.
Some reduced code to show you what I'm hoping to accomplish:
I hope my code makes a bit of sense. I know it's wrong and I didn't try to compile it as is. I'm trying to get an idea of what key concepts and ingredients are missing.Code:struct Point { double x, y, z; }; class Helix { public: /* here i have a constructor and some other functions which all work, so I'll leave it out */ ... ... /* here I need help adding iterator functions, operator overloading, for the 'outside' */ typedef std::vector<Point>::const_iterator cnst_iter; cnst_iter p_begin() { return point_coords.begin(); } cnst_iter p_end() { return point_coords.end(); } private: std::vector<Point> point_coords; }; int main() { short helices = 5; // example std::vector<Helix> helix; for(short level = 0; level < helices; level++) { helix.push_back(Helix(level, scale, density)); // works } /* here I need help: 1) I would like to iterate over the vector of <Helix> objects. Do i need to define iterators inside the Helix class for this purpose as well, or is that only needed for private class members? 2) I also want to iterate over the vector of <Point> structs inside the (vector of) Helix objects. I will be reading data only. */ for(std::vector<Helix>::const_iterator it = helix.begin(); it != helix.end(); ++it;) { for(std::vector<Point>::const_iterator it2 = helix.p_begin(); it2 != helix.p_end(); ++it2) { /* here i need to retrieve data from the point_coords vector; */ } } return 0; }
Thanks.



2Likes
LinkBack URL
About LinkBacks



