list iterator question

This is a discussion on list iterator question within the C++ Programming forums, part of the General Programming Boards category; Using <list> Dose a list iterator keep an index of what number it is in the list? About to go ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    list iterator question

    Using <list>

    Dose a list iterator keep an index of what number it is in the list?

    About to go look through MSDN, not finding anything with google.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    lists have no real knowledge of "placement" in a list. IE a particular node only knows who is directly infront of it and directly behind it, it does not know that its the 5th node. The iterator only has the knowledge of the node it points to (and of course it knows what the node knows )

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    I found the function Distance that takes two iterators and returns the distance between them, so I could use that to find the position in the list, but I decided to just keep track of it outside of the list.

    My list is a list of menu choices, and I need to know which one is the current selection, not just from the iterator, but the index from the start too.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Dose a list iterator keep an index of what number it is in the list?
    No, but you can acquire that information fairly easily by subtracting the first iterator in the list from the current one.

    [edit]
    My mistake. I was thinking of random access iterators rather than bidirectional iterators. For anything but a random access iterator the standard library has a function called distance in <iterator>.
    [/edit]
    Last edited by Prelude; 06-12-2004 at 10:39 AM.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Follow up question.


    How can you loop an iterator around to make a list a circular list, what should be below...

    Code:
    list<object>::iterator selection;
    
    void NextObject() {
        selection++;
        if(--something--) {
            selection = objectList.begin();
        }
    }

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    Code:
    if (selection == objectList.end()) {
        selection = objectList.begin();
    gg

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Thats not going to work for the other direction is it?

    Code:
    if (selection == objectList.begin()) {
        selection = objectList.end();

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    It can work - you just have to increment/decrement your iterator at the right time depending on direction.

    end() can be thought of as "one past the last element", where begin() returns the first element. If the container is empty, then begin() == end().

    gg

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    got it now.

    Code:
    		if(selection == objectList->begin()) {
    			selection = objectList->end();
    			selection--;
    		}
    		else selection--;
    thanks guys.

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