문제

Is there a way to determine the position of an iterator inside it's container? The "position" I'm looking would take the form of an integer value, that describes how far from the beginning of the container the iterator is.

For example,vector.front() would be 0, and vector.back() would be vector.size() - 1

도움이 되었습니까?

해결책

std::distance:

size_t index = std::distance( vector.begin(), it );

What it does behind the scenes is just it - v.begin() (for random access iterators, such as vector's). Otherwise, it just increments first argument until it reaches the second (which isn't particularly efficient).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top