Click here to Skip to main content
6,633,937 members and growing! (22,217 online)
Email Password   helpLost your password?
Web Development » Trace and Logs » General     Intermediate

CList Iterator

By Craig Henderson

A simple iteration class for MFC's CList linked list
VC6Win2K, Visual Studio, MFC, Dev
Posted:12 Oct 2000
Views:84,168
Bookmarked:17 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 3.42 Rating: 3.07 out of 5
2 votes, 50.0%
1

2
2 votes, 50.0%
3

4

5
  • Download source files - 1 Kb
  • Introduction

    MFC's CList provides a template linked list implementation and works perfectly adequately. However, iterating through the list can be a little combersome. Code like that below is common place for processing list elements, but the POSITION variable is meaningless and confusing as it always points to the next node rather than the current one.

    POSITION        pos;
    CList<int, int> list;
    
    // add nodes to the list
    
    
    pos = list.GetHeadPosition();
    while (pos)
    {
        int nElement;
    
        nElement = list.GetNext(pos);
        // do something with nElement
    
    }
    

    TLListIter class

    The iterator class I present here simplifies the use slightly, making the code easier to read and maintain. It originates from a larger project I wrote where I have a list of objects of type A, each with their own list of objects of type B, and I needed to iterate all objects of type B in the system. This class simplified the task immensly.

    So using this class, the above code becomes

    CList<int, int>      list;		// linked list
    
    TLListIter<int, int> iter(list);	// linked list iterator
    
    // add nodes to the list
    
    
    iter.begin();
    while (iter)
    {
        int nElement;
    
        nElement = iter++;
        // do something with nElement
    
    }
    

    Public Members

    Construction/Destruction

    TLListIter(TLList<TYPE, ARG_TYPE> &__list);
    Constructor to initialise the iterator with the the linked list to be iterated.

    TLListIter(TLListIter<TYPE, ARG_TYPE> &__iter);
    Copy constructor to create a new iterator for the same linked list with the same node position.

    virtual ~TLListIter();
    Virtual class destructor.

    Operators

    operator bool ();
    Boolean operator will return true if there is a current node, or false if not.

    operator ARG_TYPE ();
    Linked List argument operator will returns the element at the current node.

    Navigation

    TLListIter<TYPE, ARG_TYPE> operator++(int);
    Post increment operator returns the current node and moves the internal position to the next node.

    TLListIter<TYPE, ARG_TYPE> operator--(int);
    Post decrement operator returns the current node and moves the internal position to the previous node.

    // these functions will return false if there are no elements in the list

    bool begin(void);
    Moves to the first node in the list. This function returns false if the linked list is empty.

    bool end(void);
    Moves to the last node in the list. This function returns false if the linked list is empty.

    MFC Compatibilty

    POSITION GetPosition(void) const;

    Returns the internal POSITION variable.

    Limitations

    • As the class stands, it cannot be used to process a list of bool. This is because of the operator bool() used to determine the end of the list.
    • For some reason unknown to me (please let me know any solutions) a compiler error is generated if the class is used while (nNumber == 7 && iter). Instead, this must be coded while (nNumber == 7 && iter == true) and then all is fine. Wierd.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Craig Henderson


    Member
    Craig graduated with a B.SC. Honours in Computing for Real Time Systems from the University of the West of England, Bristol in 1995 after completing an HND in Computing in 1993.

    Craig was a successful professional software engineer for over 15 years, and now works for JDA Software as the Development Manager for the Revenue Management business in EMEA.
    Occupation: Other
    Company: JDA Software
    Location: United Kingdom United Kingdom

    Other popular Trace and Logs articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
    GeneralI'm translating the article Pinmemberjulianico5:14 20 Dec '05  
    GeneralMy diff List PinsussAnonymous23:25 30 Dec '03  
    GeneralMy diff List PinsussAnonymous23:23 30 Dec '03  
    GeneralIterator PinmemberAndre Pham9:28 29 Dec '03  
    GeneralPOSITION PinmemberRAyRAy16:34 26 May '02  
    Generalcode source seems not ok PinmemberLuc Vandal12:03 20 Nov '00  
    GeneralRe: code source seems not ok PinmemberCraig Henderson5:19 1 Dec '00  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 12 Oct 2000
    Editor: Chris Maunder
    Copyright 2000 by Craig Henderson
    Everything else Copyright © CodeProject, 1999-2009
    Web19 | Advertise on the Code Project