Thread: Using an iterator with a vector of objects

  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.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your code is close to being correct:
    Code:
    for (std::vector<Helix>::const_iterator it = helix.begin(); it != helix.end(); ++it)
    {
        for (std::vector<Point>::const_iterator it2 = it->p_begin(); it2 != it->p_end(); ++it2)
        {
            std::cout << "x = " << it2->x << ", y = " << it2->y << ", z = " << it2->z << '\n';
        }
    }
    p_begin() and p_end() should be const member functions as well, otherwise const correctness will bite you in the ass:
    Code:
    cnst_iter p_begin() const { return point_coords.begin(); }
    cnst_iter p_end() const { return point_coords.end(); }
    My best code is written with the delete key.

  3. #3
    Guest
    Guest
    Very helpful, this forum is almost like a chat room. I indented my for-loop like that because of the narrow post form, i'll see if i can undo that to make it more readable

    So helix.begin() works without any extra code because helix is a vector defined in main() and the begin() function is built-in; makes sense.

    I saw some an online tutorial where the operators (e.g. ++, !=) used in the loop had to be defined in the class, which was difficult to understand. Might this be code prior to C++11, or a different usage scenario? Unfortunately I can't find the link right now.

    I'm glad your example gets the job done, as it's very straight forward and understandable for a beginner.


    Quote Originally Posted by Prelude
    p_begin() and p_end() should be const member functions as well, otherwise const correctness will bite you in the ass (...)
    Ah yes, i wanted to ask about that too and forgot.

    So since
    Code:
    typedef xyz;
    xyz myFunction() { return xyz; }
    Is comparable to
    Code:
    int myFunction() { return int; }
    I can just add a const as with functions of regular types. Now i get it.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Exactly! They are built in. More about vectors. Nice examples can be found there.

    In C++, you can overload functions as you may know. Moreover, we can overload operators too! (it is pretty common to overload the >> and the << operators for example).
    That is what you saw I guess, an operator's overload!

    As for you next question.
    When we typedef something, we give a synonymous to this something.
    Example
    Code:
    #include <stdio.h>
    
    typedef int aType;
    
    int main(void)
    {
            aType g;
            int t;
            t = 9;
            g = 9;
            printf("g = %d and t = %d\n", g, t);
            return 0;
    }
    The code is in C, but I think you get the point. After the typedef, even if I write int or aType, for the compiler it's the same! What he actually does, is to replace aType with the word int every time he meets the word aType.
    typedef is useful when we have structs, etc. typedef'ed words are usually followed by a _t.
    So, I should write aType_t;
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Guest
    Guest
    Great, thanks!

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Bitte
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

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