Thread: std::list and iterators

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    std::list and iterators

    Assuming you have an std::list (or other container) iterator and want it to point at 'nothing' you assign it List.end(). Ok, now if you don't have a specific list available (these cases occur) how can you nullify it? Would NULL be acceptable? (have my doubts about this...).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how can you nullify it?
    You don't. There's really no reason for an iterator that isn't paired with a container, so such an assignment would be nonsensical.

    >Would NULL be acceptable?
    No.
    My best code is written with the delete key.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Iterators are objects that can iterate over elements of a sequence so they're not pointers. NULL can be assigned to pointer. Iterator is an object and can act like a "smart pointer", but it's still and object!!!
    Iterators are objects that can iterate over elements of a sequence so they're not pointers. NULL can be assigned only to a pointer. Iterator is an object and can act like a "smart pointer", but it's still and object!!!Remember: each container type supplies its own kind of iterator. In fact, each container class defines its iterator type as a nested class. As a result, iterators share the same interface but have different types.
    NULL is in C++ defined as 0. Do you understand now why NULL is not an option here?
    Last edited by Micko; 04-04-2005 at 09:14 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. List copy semantics efficiency
    By dudeomanodude in forum C++ Programming
    Replies: 31
    Last Post: 05-24-2008, 05:05 PM
  2. std::list question
    By izuael in forum C++ Programming
    Replies: 16
    Last Post: 10-18-2006, 03:52 AM