简体   繁体   中英

std::equal with reverse_iterator

Is it legal to use a reverse_iterator with std::equal ?

For example, are any of these legal?

std::equal(v.begin(), v.end(), w.rbegin())

std::equal(v.rbegin(), v.rend(), w.begin())

std::equal(v.rbegin(), v.rend(), w.rbegin())

All are valid, because reverse iterators are , in fact, forward iterators .

"Reverse iterator" is not an iterator category. Remember some iterator categories:

  • An iterator that can be dereferenced ( * ) and incremented ( ++ ) is a forward iterator.
  • A forward iterator that can also be decremented is a bidirectional iterator.
  • A random access iterator is a biderectional iterator that also has + and - operators.

On the other hand, a reverse iterator is a bidirectional iterator or a random access iterator that looks at a collection in reverse. Look at

http://www.cplusplus.com/reference/std/iterator/reverse_iterator/

... especially what it says about iterator_category under the "Member types" heading.

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