Like Tree2Likes
  • 1 Post By Prelude
  • 1 Post By std10093

Using an iterator with a vector of objects

This is a discussion on Using an iterator with a vector of objects within the C++ Programming forums, part of the General Programming Boards category; As the title says, I'm trying to work with iterators in a class object. So far I haven't done anything ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    20

    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 -Adrian; 01-20-2013 at 10:03 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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(); }
    -Adrian likes this.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    20
    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,675
    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;
    -Adrian likes this.
    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
    Registered User
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    20
    Great, thanks!

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    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, 12: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, 01: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, 12:55 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21