STL iterator "de-referencing" question

This is a discussion on STL iterator "de-referencing" question within the C++ Programming forums, part of the General Programming Boards category; Hey, I was writing something and made the statement: Code: return *(vector_name.end()); And it threw a memory fault, changed it ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196

    STL iterator "de-referencing" question

    Hey,

    I was writing something and made the statement:
    Code:
    return *(vector_name.end());
    And it threw a memory fault, changed it to
    Code:
    return vector_name[vector_name.size() - 1];
    And it worked fine. I realize what the latter does, but my question is why the first one doesn't work? If I "dereference" the iterator, wouldn't it be the same as:
    Code:
    object_name a;
    return a;
    Or is there something different about iterators? I know you can use them by just dereferencing them as if they were pointers...but I've heard from a few people that they're not "really" pointers..but they act it, can anybody give me some insight here? I'd hate to make the same mistake again .

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    vectorname.end() points the past-the-end element of the vector. So basically, you should get undefined behavior. It's have the same behavior as: int v[5], then use v[5].

    The correct one is:
    Code:
    return *(vector_name.end()-1);
    Or is there something different about iterators? I know you can use them by just dereferencing them as if they were pointers...but I've heard from a few people that they're not "really" pointers..but they act it, can anybody give me some insight here? I'd hate to make the same mistake again .
    It depends on what kind of iterators you're using. Random Access Iterators, which vector class use, is just like the usual pointer. But if you have like input iterator or bidirectional iterator, then they're not really the same because for those, they can't do some pointer arithmatics with them.
    Last edited by nimitzhunter; 01-11-2011 at 07:51 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    A quick look at what map::end() returned would have answered this question.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    It would be easier to write:
    Code:
    return vector_name.back();
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Wow, I've used STL iterators many times...I can't believe I didn't see that.

    <-- Feels like an idiot now o.o

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. STL preformance question
    By l2u in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2006, 04:36 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. STL algorithm question
    By Reggie in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2003, 09:04 AM
  5. question about STL
    By free2run in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2002, 11:12 AM

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