I've ordered QVector<QString>
by qSort
. I'd like to get index of specified QString
, but qBinarySearch
returns Java-style iterator allowing to move next/back or pointer to QString
(because QVector::iterator
is typedef T*
). How can I get index of element without iterate for each element and check. Is the only way make own binary search method (I know how, but why invent wheel again?)?
Asked
Active
Viewed 955 times
0

patrykbajos
- 384
- 3
- 17
1 Answers
1
According to http://doc.qt.io/qt-5/qtalgorithms-obsolete.html those algorithms are obsolete, and you are recommended to use std::binary_search
(or std::lower_bound
) instead. They return random access iterators, so converting them to an index is cheap and easy.

Alan Stokes
- 18,815
- 3
- 45
- 64
-
1The actual question is hidden in irrelevant detail. It is about getting an index from an iterator. – juanchopanza Jun 19 '16 at 18:03
-
@juanchopanza Well, it might be. If the OP is correct that `qBinarySearch` returns a non-standard Java-style iterator then the standard techniques wouldn't work. – Alan Stokes Jun 19 '16 at 18:05
-
I wasn't know that subtraction "begin - iterator" returns index. I've made that in my method. – patrykbajos Jun 19 '16 at 18:26
-
@patrykbajos you meant iterator-begin – Waslap Nov 06 '20 at 08:46