C ++迭代器和反向迭代器 [英] C++ iterator and reverse iterator

查看:294
本文介绍了C ++迭代器和反向迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为当前对象写了一个迭代器(实际上是 const_iterator 创建一个 reverse_const_iterator



我环顾四周,看看如何做到这一点,我偶然发现了


但是请注意当一个迭代器被反转时,反向版本
不指向该范围中的同一个元素,而是指向它之前的一个
,这是为了安排过去 - end
范围的元素:当反向时,指向
a范围中的过去端元素的迭代器将更改为指向最后一个元素(不是过去的
)范围(如果
相反,这将是范围的第一个元素)如果一个范围中的第一个元素的迭代器是
反转,反向迭代器指向第一个元素之前的元素
元素(如果
反转,则为范围的过去端元素)。


从用户的角度来看,或者当你取消引用 reverse_iterator ,它不会通过给你你认为的对象的值/引用来抽象这个 >它指向?这是否只是实现细节?



我的理解是:

  (i = obj.rbegin(); i!= obj.rend(); i ++)

相当于

  for(i = obj.begin(); i!= obj.end(); i ++)

因此,在第一种情况下, * i 将在容器中向后移动,在第二种情况下向前通过容器。我的直觉是否正确?

解决方案

你是对的,反向迭代器包含一个普通的迭代器,它指向在你取消引用它的对象之后的元素。然而,它不只是一个实现细节。 std :: reverse_iterator 适配器提供了一个成员函数调用 base ,它返回底层迭代器。



标准定义了 std :: reverse_iterator 作为迭代器适配器,与迭代器的适配关系如下:


反向迭代器及其相应迭代器i之间的基本关系由以下身份建立:& *(reverse_iterator(i))== & *(i - 1)


/ code>正在从容器中擦除一个元素,如下所示:

  it ++; 
lst.erase(it.base());

如果你想这样做而反过来反过来容器,你会这样做:

  it ++; 
std :: list< int> :: reverse_iterator(lst.erase(it.base()));


I'm writing an iterator (actually it is const_iterator for my current object, and I want also to create a reverse_const_iterator also.

I looked around, to see how to do this and I stumbled across this:

Notice however that when an iterator is reversed, the reversed version does not point to the same element in the range, but to the one preceding it. This is so, in order to arrange for the past-the-end element of a range: An iterator pointing to a past-the-end element in a range, when reversed, is changed to point to the last element (not past it) of the range (this would be the first element of the range if reversed). And if an iterator to the first element in a range is reversed, the reversed iterator points to the element before the first element (this would be the past-the-end element of the range if reversed).

Is this what happens from the users' perspective, or when you dereference a reverse_iterator does it not abstract this away by giving you the value/reference of the object you think it is pointing to? Is this just implementation detail?

My understanding was:

for(i = obj.rbegin(); i != obj.rend(); i++)

was equivalent to

for(i = obj.begin(); i != obj.end(); i++)

except in reverse. And so *i would go backward through the container in the first case, and go forward through the container in the second case. Is my instinct correct?

解决方案

You're right that it's an abstraction. The reverse iterator contains a normal iterator that points at the element after the object you would get if you dereferenced it. However, it's not merely an implementation detail. The std::reverse_iterator adapter provides a member function call base which returns the underlying iterator.

The standard defines std::reverse_iterator as an iterator adapter with the following relation to the iterator its adapting:

The fundamental relation between a reverse iterator and its corresponding iterator i is established by the identity: &*(reverse_iterator(i)) == &*(i - 1)

A common use for base is erasing an element from a container, which would be done like so:

it++;
lst.erase(it.base());

If you want to do this while iterating over the container in reverse, you would do:

it++;
std::list<int>::reverse_iterator(lst.erase(it.base()));

这篇关于C ++迭代器和反向迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆