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
Re: Map, from iterator to index and viceversa.
You can use the function std::distance
however remember that maps aren't sequential containers
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
Re: Map, from iterator to index and viceversa.
You can use std::advance for this purpose
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.
Re: Map, from iterator to index and viceversa.
In a previous post I said
Quote:
remember that maps aren't sequential containers
However map can be traversed "like" sequential containers using iterators
Code:
for (it = myMap.begin(); it != myMap.end(); ++it) {
...
}
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.
Re: Map, from iterator to index and viceversa.
Quote:
Originally Posted by
mcosta
In a previous post I said
However map can be traversed "like" sequential containers using iterators
Code:
for (it = myMap.begin(); it != myMap.end(); ++it) {
...
}
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.
Re: Map, from iterator to index and viceversa.
I agree with both of you (wysota and SixDegrees).
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.