I need to cout and return a deque element's index that an iterator is pointing to. How do I get an int out of an iterator?
+3
A:
std::ptrdiff_t index = std::distance(myDeque.begin(), curIterator);
Matthew Flaschen
2010-06-01 21:42:18
+8
A:
You can use:
std::ptrdiff_t index(std::distance(my_container.begin(), my_iterator));
Be aware of the runtime costs of such a routine, however- it depends on the data structure you use.
fbrereto
2010-06-01 21:42:48
Or rather it depends on the class of iterator (random, forward, etc.)
Don Wakefield
2010-06-01 21:52:46
I'm assuming distance steps through each element of the container to find the number, so for a large deque the runtime cost would be big. However, my deque should be no bigger than 10 elements. And does distance return an int? What does the index() call do?
George
2010-06-01 21:54:58
If your deque shouldn't have more than 10 elements in it then I'd submit you should be using `vector` instead, or at least have a good rationale for sticking with deque. The index() "call" is just a variable called `index` being constructed with the return value from `std::distance`.
fbrereto
2010-06-01 22:27:42
deque uses random-access iterators, so the iteration will be faster than walking the deque from first to the index.
fbrereto
2010-06-01 22:29:23
George: technically, distance returns the `difference_type` of the iterators, which boils down to a `ptrdiff_t`. That's an integer, but not necessarily literally an `int` - eg. in MSVC++ 64-bit builds, `int` is 32 bits but `ptrdiff_t` is 64 bits.
Peter
2010-06-02 00:02:16
fbrereto: Come to think of it, there really isn't a good reason for using deques. I used it because I've been using deques for other parts of this project (namely, card decks). playerList is will be holding the players at a poker table in the order of position at the table. Do you see any reason why I should stick with deques?Peter: Thanks for the response
George
2010-06-02 01:14:08
@George: My personal heuristic for the STL containers is "use vector unless one of the other ones suits the problem better." In this case with such a small data set, vector should meet your needs with the smallest footprint possible.
fbrereto
2010-06-02 14:27:30
+4
A:
For random-access iterators you can just use subtraction:
size_t index = some_iterator - some_deque.begin()
Obviously this doesn't work for all iterators (eg. for std::list or whatever) but I'd submit that there's a certain elegance in that you can only use this technique when it'll take constant time. If your container doesn't have random access iterators then it very probably isn't sensible to try to find the index of them anyway.
Peter
2010-06-01 21:46:34
I think I'm going to vote this the best answer for its simplicity and elegance. Could I assign the result to an int to have it cast it to an int?
George
2010-06-02 01:15:59
You can assign it to an int, but you'll probably get a compiler warning at higher warning levels (either unsigned->signed conversion or actual shortening of type, depending on the compiler etc). It's a little annoying to have to explicitly cast to int, but that's not going to hide a problem unless you'll have more than 2 billion elements in your deque - which I guess you'll be pretty sure that you won't :)
Peter
2010-06-02 07:14:35
+2
A:
Out of the two presented methods:
std::ptrdiff_t index(std::distance(my_container.begin(), my_iterator));
and
std::ptrdiff_t index = some_iterator - some_deque.begin()
... the latter has the superiority of being only applicable to random access iterators -- hence when substituting for another container, you wont accidentaly get an expensive operation (O(n) for lists for example).
Kornel Kisielewicz
2010-06-01 22:04:52