Thread: STL iterator "de-referencing" question

  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
    463
    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 08:51 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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
    28,413
    It would be easier to write:
    Code:
    return vector_name.back();
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05: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, 12:12 PM