Why do I have to dereference my vector iterator? What does it mean?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Why do I have to dereference my vector iterator? What does it mean?

  1. #1
    Join Date
    Jun 2012
    Posts
    6

    Unhappy Why do I have to dereference my vector iterator? What does it mean?

    I wrote this code to test:

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct Person {
    string name;
    int age;
    };
    
    int main()
    {
    // Populate some people and put them into a vector.
    
    Person p1, p2, p3;
    vector<Person> ListOfPeople;
    
    p1.name = "Richard";
    p2.name = "Mike";
    p3.name = "Cheryl";
    
    p1.age = 12;
    p2.age = 13;
    p3.age = 14;
    
    ListOfPeople.push_back(p1);
    ListOfPeople.push_back(p2);
    ListOfPeople.push_back(p3);
    
    vector<Person>::const_iterator citer = ListOfPeople.begin();
    
    while ( citer != ListOfPeople.end() )
    {
    cout << (*citer).name << " is " << citer->age << " years old." << endl;
    ++citer;
    }
    
    return 0;
    }
    A clean version can be found at http://codepad.org/hAumqzNB with the associated output.

    I know this has to do with pointers. I taught myself C++ once before using online tutorials but this book hasnt gone into pointers though it has covered references. This is one thing I never really understood about the language.

    My questions:

    Why do I have to deference this to make it work?
    What exactly does dereferencing normally do?
    Why on other sites do they say pointers are a type of iterator?
    Lastly, object->member just a synonym of (*object).member and nothing more?

    Edit: the book says derefencing gives an lvalue. Which i understand is a nontemporary object. So when you use an iterator does it act as a reference to the vector? Because I know you need lvalues to access data because of scoping issues. So does this essentially make a copy of the object in memory for the purpose of printing to the screen? aka an lvalue?

    Sorry if I don't understand properly, its why im here :/

    Thanks in advance for all your help!

    Senjai

  2. #2
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Why do I have to dereference my vector iterator? What does it mean?

    Quote Originally Posted by Senjai View Post
    I wrote this code to test:
    You should learn to properly indent your code. It is horribly formatted (everything is flushed to the left margin).
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct Person {
        string name;
        int age;
    };
    
    int main()
    {
        // Populate some people and put them into a vector.
    
        Person p1, p2, p3;
        vector<Person> ListOfPeople;
    
        p1.name = "Richard";
        p2.name = "Mike";
        p3.name = "Cheryl";
    
        p1.age = 12;
        p2.age = 13;
        p3.age = 14;
    
        ListOfPeople.push_back(p1);
        ListOfPeople.push_back(p2);
        ListOfPeople.push_back(p3);
    
        vector<Person>::const_iterator citer = ListOfPeople.begin();
    
        while ( citer != ListOfPeople.end() )
        {
             cout << (*citer).name << " is " << citer->age << " years old." << endl;
             ++citer;
        }
        return 0;
    }
    That is properly indented code, so that it becomes readable to you, me, and anyone else looking at your code.
    I know this has to do with pointers. I taught myself C++ once before using online tutorials
    Which for most "online tutorials" means you were not taught correctly or sufficiently. Online tutorials fall way short of actually learning a language such as C++.
    Why do I have to deference this to make it work?
    If you didn't dereference the iterator, what happens?
    What exactly does dereferencing normally do?
    Returns to you the data residing at where the iterator is "pointing" to (I put "pointing" in quotes to indicate that iterators act like pointers).
    Why on other sites do they say pointers are a type of iterator?
    This has nothing to do with "other sites". C++ doesn't work by whatever the web author of a site happens to say.

    This is a fact -- a pointer is a type of iterator, most notably a random access iterator. An iterator is a concept -- that concept being that if you increment, decrement, add to or subtract from an iterator, that iterator will now point to a certain value. A pointer satisfies this concept, therefore it is an iterator.
    Lastly, object->member just a synonym of (*object).member and nothing more?
    Yes. This is a basic concept.
    Edit: the book says derefencing gives an lvalue. Which i understand is a nontemporary object. So when you use an iterator does it act as a reference to the vector?
    The iterator allows you to access elements within the vector.
    Because I know you need lvalues to access data because of scoping issues. So does this essentially make a copy of the object in memory for the purpose of printing to the screen? aka an lvalue?
    I have no idea what you're trying to say here.

    C++ is a language, it has rules. What does printing to the screen have to do with the rules of the language?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jun 2012
    Posts
    6

    Re: Why do I have to dereference my vector iterator? What does it mean?

    Thank you paul for taking the time to respond to my post.

    For the formatting issue, I copy and pasted from MSVS into the browser, other boards automatically copy the indentation. Thats why I included http://codepad.org/hAumqzNB .

    Sorry I'll make sure to manually indent in the future.

    You answered my question. container<container-type>::iterator is a type of pointer. I had not got to pointers in my book so I stated I was unsure exactly what I was dereferencing.

    I agree that online tutorials in no way to justice to learning a language properly, that's why I'm here.

    Thank you for clearing up my points. C++ has rules I'm just trying to make sure I learn them, the right way.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,353

    Re: Why do I have to dereference my vector iterator? What does it mean?

    Quote Originally Posted by Senjai
    You answered my question. container<container-type>::iterator is a type of pointer. I had not got to pointers in my book so I stated I was unsure exactly what I was dereferencing.
    Actually, this is not generally true. Rather, a pointer is a type of iterator because the concept of an iterator in C++ was generalised from the concept of a pointer. However, a given iterator type could actually be a class type, not a pointer type.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2012
    Posts
    6

    Re: Why do I have to dereference my vector iterator? What does it mean?

    Quote Originally Posted by laserlight View Post
    Actually, this is not generally true. Rather, a pointer is a type of iterator because the concept of an iterator in C++ was generalised from the concept of a pointer. However, a given iterator type could actually be a class type, not a pointer type.
    You lost me.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,353

    Re: Why do I have to dereference my vector iterator? What does it mean?

    Consider: English is a language, but not all languages are English. Likewise, a pointer is an iterator, but not all iterators are pointers.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Why do I have to dereference my vector iterator? What does it mean?

    Quote Originally Posted by Senjai View Post
    You lost me.
    Look at these operations:
    Code:
    x++;
    ++x;
    x += n;
    x -= n;
    *x;
    Assume n is an integer. If x is a pointer, are those operations you see above possible? Yes.

    In C++, you can overload operators. This means that ++, +=, -=, and * can mean whatever you want them to mean. So let's say that x is an object (not a true pointer) that "points" to a certain item. This object has all of those operators overloaded to go to the next, previous, jump to any item, and dereference. So is this object an iterator? Yes.

    Forget about vector, look at std::list. A std::list has iterators, and the elements in a std::list are not next to each other in memory. So how would a simple pointer be used as an iterator, when to get to next item in a linked list is more complex then taking a pointer and adding 1? The answer is that a list iterator is not a simple pointer -- it is an entire class that has the operators overloaded that describe how to get to the next or previous element.

    Maybe that's what's your missing -- the "how an iterator does what it does" is customized due to classes that use operator overloading. However, the interface that you use to manipulate an iterator looks exactly the same as a simple pointer.

    Look through the <list> header file. If you understand intermediate to advanced C++, you will see exactly what I'm talking about.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 14th, 2012 at 11:08 PM.

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,050

    Re: Why do I have to dereference my vector iterator? What does it mean?

    Quote Originally Posted by Senjai View Post
    Why do I have to deference this to make it work?
    Dereferencing is the way you access an object by pointer. Pointer itself is just an address to some memory region.

    What exactly does dereferencing normally do?
    Dereferencing instructs compiler to use the memory the pointer points at as an object of the type deduced from the pointer type.

    Why on other sites do they say pointers are a type of iterator?
    Because pointer lets you iterate (see Pointer Arithmetic). More interesting question behind this is "what is iterator?"

    Lastly, object->member just a synonym of (*object).member and nothing more?
    For pointers, yes, unconditionally. In fact, for STL iterators it's same true, but this was intentionally done to make iterators look like pointers as much as possible.

    Edit: the book says derefencing gives an lvalue. Which i understand is a nontemporary object.
    Nontemporary, this is about a scope. While actually lvalue is an expression that can be on the left side of equation, i.e. an object which a value could be assigned to.
    So when you use an iterator does it act as a reference to the vector?
    Vector element, to be accurate with terms.
    Last edited by Igor Vartanov; June 15th, 2012 at 03:13 AM.
    Best regards,
    Igor

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,061

    Re: Why do I have to dereference my vector iterator? What does it mean?

    The real reason behind it all is because it made sense to the people that designed the STL classes.

    What you need to realise is that the iterators are NOT pointers to container elements. Neither are they references or indices or actual items. they are what they are... iterators...

    You may look at one specific implementation ans see that that specific implementation solved the iterator 'problem' by using pointers (most do), but this isn't a guarantee or a requirement of the iterators.

    An iterator is a object that allows you to iterate through a container. Nothing more... nothing less.

    now, you need a way to obtain the vector item the iterator 'points' to.
    The STL designers could have done this in many ways...

    it could have been a .get() function. or it could have been an () operator. Apparently the designers chose the * and -> operators.


    And you aren't "dereferencing" the iterator... since the iterator is not a pointer, not all iterators are implemented with pointers, but you always (by convention) get at the item an iterator points to by calling it's * operator.

    In C++, if it looks like a duck, and quacks like a duck... it could be a duck, or it could be anything else that has a duck operator.

    The iterator is just an object that has been designed to look like a pointer. it could be a pointer, but it doesn't have to be, it just has to more or less behave like one.
    Last edited by OReubens; June 15th, 2012 at 03:45 AM.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,353

    Re: Why do I have to dereference my vector iterator? What does it mean?

    Quote Originally Posted by OReubens
    And you aren't "dereferencing" the iterator... since the iterator is not a pointer, not all iterators are implemented with pointers, but you always (by convention) get at the item an iterator points to by calling it's * operator.
    That said, because the concept of iterators in C++ are a generalisation of the concept of pointers, it is correct to say "dereference the iterator", and in fact that language is used in the C++ standard.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center