"NULL" iterator?

This is a discussion on "NULL" iterator? within the C++ Programming forums, part of the General Programming Boards category; I have some code which is using std::map iterators in a manner similar to pointers. I have a wrapper class ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    752

    "NULL" iterator?

    I have some code which is using std::map iterators in a manner similar to pointers. I have a wrapper class around the iterator which facilitates pulling elements out of them. The problem is, when I compile, I get the error "no appropriate default constructor available".

    Code:
    typedef std::map<int, int> foo;
    
    class FooElem
    {
      foo::const_iterator ci;
      
      public:
        FooElem (foo::const_iterator const & iter) : ci(iter) { }
    
        std::string getId() { return ci->first; }
        int getAge() { return ci->second; }
    };


    So, what I want to do is add a constructor like this...
    Code:
        FooElem() : ci(NULL?) { }
    But... that clearly won't work. What can I do to get this effect? The best I can think of is to use a pointer to the iterator, but it seems like there should be a way to just make a zero-iterator or something.
    Callou collei we'll code the way
    Of prime numbers and pings!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    What does getId() and getAge() mean when the iterator cannot be dereferenced? It sounds to me that you can just make it a pre-condition that the iterator can be dereferenced, so passing map.end() to the constructor explicitly results in undefined behaviour.

    Actually, it may be better to just provide getId() and getAge() as free functions instead of member functions.
    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

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If it makes sense for a FooItem to "not exist" (i.e., not have an actual item referenced), then you wouldn't want a foo::const_iterator, but maybe something like a pair<int, int> *, I guess. (Somebody will be along soon to tell me how wrong I am, but that's what I say.)

    If it doesn't make sense for a FooItem to "not exist", then you need to look at the line with the error and figure out what you want to do there.

    (Also: how does return ci->first give you a std::string if ci iterates over <int, int>?)

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is somewhat strange to have an iterator with non-local scope. Normally they are used for iterating (or obtaining the result from an algorithm) and not kept around longer.

    There is no NULL iterator if I'm not mistaken (the closest would be the return value of end() of the respective container), so perhaps a pointer might be more appropriate. (Or perhaps a redesign of the system.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by QuestionC View Post
    I have some code which is using std::map iterators in a manner similar to pointers. I have a wrapper class around the iterator which facilitates pulling elements out of them. The problem is, when I compile, I get the error "no appropriate default constructor available".
    I'm not sure "facilitates" is the right term. This wrapper class seems superfluous. What is the difficulty in simply accessing iter->first or iter->second?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Syntactic candy? Code readability (OK, so it->first and it->second aren't so bad, but the wrapper makes it better)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Iterator
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-19-2008, 11:16 PM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 12:36 PM

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