简体   繁体   中英

Do C++ standard library algorithms use std::advance

I have not been able to find a decent source for this. Do all algorithms in the <algorithm> header use std::advance to increment (and possibly decrement) iterators?

Also another related follow up - I saw the cppreference page for the RandomAccessIterator concept ( http://en.cppreference.com/w/cpp/concept/RandomAccessIterator ) , but it did not address the requirement for having member alias for an iterator category. If an iterator class does not have a member typedef named iterator_category that is aliased to random_access_iterator_tag but supports the operations that are mentioned in the cppreference page ( http://en.cppreference.com/w/cpp/concept/RandomAccessIterator ) for RandomAccessIterator, does the c++ standard library assume that the iterator is a random access iterator?

Note I meant to ask this question with reference to the C++ standard. ie "What does the standard say about this?"

The standard does not specify the implementation of C++ std algorithms explicitly. It specifies behavior, and sometimes how many times particular operations are done. This can leave the implementer with little practical choice, but it is not explicitly specified.

std::advance is never called out in the standard as being called by another algorithm to the best of my knowledge. Which means it could be used, or not used, in a particular implementation of a particular algorithm.

The effects of std::advance are specified. Insofar as other algorithms specify their operation counts, in order to call std::advance it must not break their guarantees.

In short, using it is permitted and not required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM