iterator for empty list

This is a discussion on iterator for empty list within the C++ Programming forums, part of the General Programming Boards category; If I have a linked list which is empty can I do something like this? Code: std::list<int> l; iter = ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    iterator for empty list

    If I have a linked list which is empty can I do something like this?

    Code:
    std::list<int> l;
    iter = l.end();
    so I can test iter for l.end() later on?
    it seems to work but it feels like its not good

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,791
    Yes, you can, but following the principle of declaring variables near first use, I would try and only declare later on when I actually want to test for the end.
    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

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,215
    If the list is modified, is it guaranteed that end() won't change? I thought it's defined to point to one past the end of the container.

    Doesn't work for vectors -
    Code:
    #include <iostream>
    #include <vector>
    
    int main() {
            std::vector<int> a;
            std::vector<int>::iterator end_it = a.end();
            a.push_back(5);
            std::cout << (a.end() == end_it) << std::endl;
    }
    For OP: why not just compare what you need to compare against l.end()?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,791
    Quote Originally Posted by cyberfish
    If the list is modified, is it guaranteed that end() won't change?
    Insertion to a std::list does not invalidate iterators to elements of the list, and deletion from a std::list only invalidates iterators to the deleted elements.
    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
    Jan 2007
    Posts
    330
    Quote Originally Posted by laserlight View Post
    Yes, you can, but following the principle of declaring variables near first use, I would try and only declare later on when I actually want to test for the end.
    thanks

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,215
    Ah I see. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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