Results 1 to 10 of 10

Thread: Map, from iterator to index and viceversa.

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Map, from iterator to index and viceversa.

    If I have a stl map with the next content :
    "a",1
    "h",4
    "s",8
    "t",1
    "v" 2
    ,....
    how to convert from iterator to index ?
    If my iterator is pointing to "s", how can I obtain that is the 'index' 2 of Mymap?
    And .... MyMap["h"] to iterator.begin ? or iterator.end ?
    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Map, from iterator to index and viceversa.

    You can use the function std::distance

    however remember that maps aren't sequential containers
    Last edited by mcosta; 19th April 2011 at 16:39. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Map, from iterator to index and viceversa.

    Thanks Mcosta.
    And, there is no way to know the contenct of the Map with the index 3 (not the key 3)
    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Map, from iterator to index and viceversa.

    You can use std::advance for this purpose
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Map, from iterator to index and viceversa.

    Internally, maps aren't stored sequentially, so the notion of an item having an "index" in the sense you're using is simply the wrong way of thinking about the data structure. The proper index of a particular item in a map is the index you've chosen - in your example, a given letter, although any data type for which a 'less than' operator is defined can be used. This is required because maps are most frequently implemented as binary search trees, so searching for items is quite fast, on the order of log2(n). But asking for a sequential, array-style "index" into a map is basically an unanswerable request - the ordering nodes are visited in depends on the algorithm used, and the nth item encountered when one algorithm is used will almost certainly not be the nth item when a different algorithm is employed.

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Map, from iterator to index and viceversa.

    In a previous post I said
    remember that maps aren't sequential containers
    However map can be traversed "like" sequential containers using iterators
    Qt Code:
    1. for (it = myMap.begin(); it != myMap.end(); ++it) {
    2. ...
    3. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Map, from iterator to index and viceversa.

    Yes but the notion of "index" or "order" simply doesn't exist for a map. You can "order" a map any way you like if you reorder the keys any way possible as all orders are equally appropriate. So to answer all the questions from this thread asked so far -- fetch a list of keys, order it the way you like and then use index-based approach on the list of keys. Since iterator-based access for the map is not really faster than key-based approach there is no practical reason for using iterators with maps other than running some algorithms that require iterators.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Map, from iterator to index and viceversa.

    Quote Originally Posted by mcosta View Post
    In a previous post I said


    However map can be traversed "like" sequential containers using iterators
    Qt Code:
    1. for (it = myMap.begin(); it != myMap.end(); ++it) {
    2. ...
    3. }
    To copy to clipboard, switch view to plain text mode 
    That's true, but the order in which items are visited depends on the underlying data structure (often a binary tree) and the algorithm used to traverse it (prefix, postfix, depth or breadth first, etc.) It may also depend on the order in which items are added to the structure, so even with the same implementation it is possible to get two entirely different results when iterating.

  9. #9
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Map, from iterator to index and viceversa.

    I agree with both of you (wysota and SixDegrees).
    A camel can go 14 days without drink,
    I can't!!!

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Map, from iterator to index and viceversa.

    To put things short - an iterator of a map assures you that if you start from begin() and advance the iterator until end(), you will have traversed all items in the map. It doesn't guarantee anything else and certainly nothing about the order of traversal.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. pointer and iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 3rd February 2008, 22:24
  2. QLinkedList iterator
    By ^NyAw^ in forum Qt Programming
    Replies: 8
    Last Post: 18th October 2007, 16:15
  3. iterator
    By mickey in forum General Programming
    Replies: 6
    Last Post: 23rd May 2006, 10:28
  4. ::iterator
    By mickey in forum General Programming
    Replies: 2
    Last Post: 20th March 2006, 22:05
  5. convert iterator
    By mickey in forum General Programming
    Replies: 8
    Last Post: 20th March 2006, 21:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.