Thread: Using an iterator with a vector of objects

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Guest
    Guest

    Using an iterator with a vector of objects

    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:
    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;
    }
    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.

    Thanks.
    Last edited by Guest; 01-20-2013 at 11:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector of objects and Iterator
    By Ivan Novák in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2013, 01:39 PM
  2. vector and iterator problem
    By BeBu in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2009, 07:38 AM
  3. Vector Iterator Help
    By (TNT) in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2007, 02:53 PM
  4. stl vector + iterator operation
    By sujeet1 in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2007, 04:10 PM
  5. vector/iterator program
    By Drake in forum Game Programming
    Replies: 3
    Last Post: 02-06-2006, 01:55 PM

Tags for this Thread