std::reverse_iterator
STD:反向[医]迭代器
Defined in header | | |
---|---|---|
template< class Iterator > class reverse_iterator : public std::iterator< typename std::iterator_traits<Iterator>::iterator_category, typename std::iterator_traits<Iterator>::value_type, typename std::iterator_traits<Iterator>::difference_type, typename std::iterator_traits<Iterator>::pointer, typename std::iterator_traits<Iterator>::reference > | | (until C++17) |
template< class Iterator > class reverse_iterator; | | (since C++17) |
std::reverse_iterator
是一个迭代器适配器,它反转给定迭代器的方向。换句话说,当提供双向迭代器时,std::reverse_iterator
生成一个新的迭代器,该迭代器从底层双向迭代器定义的序列的末尾移动到开始。
用于反向迭代器r
由迭代器构造的i
,关系&*r == &*(i-1)
总是正确的%28,只要r
是可撤销的%29;因此,反向迭代器是由一次过结束迭代器对序列中的最后一个元素的反推。
这是成员函数返回的迭代器。rbegin()
和rend()
标准库容器。
成员类型
Member type | Definition |
---|---|
value_type | std::iterator_traits<Iterator>::value_type |
difference_type | std::iterator_traits<Iterator>::difference_type |
pointer | std::iterator_traits<Iterator>::pointer |
reference | std::iterator_traits<Iterator>::reference |
iterator_category | std::iterator_traits<Iterator>::iterator_category |
注意:在C++17之前,这些成员类型必须通过从std::iterator<std::iterator_traits<Iterator>::iterator_category
,std::iterator_traits<Iterator>::value_type
,std::iterator_traits<Iterator>::difference_type
,std::iterator_traits<Iterator>::pointer
,std::iterator_traits<Iterator>::reference
>...
Member type | Definition |
---|---|
iterator_type | Iterator |
成员函数
(constructor) | constructs a new iterator adaptor (public member function) |
---|---|
operator= | assigns another iterator (public member function) |
base | accesses the underlying iterator (public member function) |
operator*operator-> | accesses the pointed-to element (public member function) |
operator[] | accesses an element by index (public member function) |
operator++operator++(int)operator+=operator+operator--operator--(int)operator-=operator- | advances or decrements the iterator (public member function) |
成员对象
Member name | Definition |
---|---|
current (protected) | a copy of the base() iterator |
非会员职能
operator==operator!=operator | compares the underlying iterators (function template) |
---|---|
operator+ | advances the iterator (function template) |
operator- | computes the distance between two iterator adaptors (function template) |
例
二次
#include <iostream>
#include <string>
#include <iterator>
int main()
{
std::string s = "Hello, world";
std::reverse_iterator<std::string::iterator> r = s.rbegin(
r[7] = 'O'; // replaces 'o' with 'O'
r += 7; // iterator now points at 'O'
std::string rev(r, s.rend()
std::cout << rev << '\n';
}
二次
产出:
二次
OlleH
二次
另见
iterator (deprecated in C++17) | base class to ease the definition of required types for simple iterators (class template) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。