简体   繁体   English

std :: reverse_iterator的缺点是什么?

[英]What are the shortcomings of std::reverse_iterator?

The documentation for boost's specialized iterator adaptors states that boost::reverse_iterator "Corrects many of the shortcomings of C++98's std::reverse_iterator." boost的专用迭代器适配器的文档声明boost::reverse_iterator “纠正了C ++ 98的std :: reverse_iterator的许多缺点。”

What are these shortcomings? 这些缺点是什么? I can't seem to find a description of these shortcomings. 我似乎无法找到这些缺点的描述。

FOLLOW-UP QUESTION: 后续问题:

How does boost::reverse_iterator correct these shortcomings? boost :: reverse_iterator如何纠正这些缺点?

Well, the big problem is that they're not forward iterators, and there's stuff that pretty much expect forward iterators. 好吧,最大的问题是它们不是前向迭代器,并且有些东西非常期待前向迭代器。 So, you have to do some funny conversions to get things to work. 所以,你必须做一些有趣的转换才能让事情发挥作用。 To name some issues 列举一些问题

  1. Some versions of erase() and insert() require iterators rather than reverse iterators. 某些版本的erase()insert()需要迭代器而不是反转迭代器。 That means that if you're using a reverse iterators and you want to insert() or erase() , you're going to have to use the reverse iterator's base() function to get your hands on a forward iterator. 这意味着如果你正在使用反向迭代器并且想要insert()erase() ,那么你将不得不使用反向迭代器的base()函数来获得前向迭代器。 There is no automatic conversion. 没有自动转换。

  2. base() returns the forward iterator equivalent to the reverse iterator in terms of insertion. base()在插入方面返回等价于反向迭代器的前向迭代器。 That is, insert inserts in front of the current element. 也就是说,在当前元素的前面插入插入。 The element that the reverse iterator is pointing at, therefore, would be the wrong element to be pointing at if base() gave you an iterator that pointed at the same element. 因此,如果base()给你一个指向同一元素的迭代器,那么反向迭代器指向的元素将是指向的错误元素。 So, it points one forward, and you can use it for insertion. 因此,它指向一个前进,您可以使用它进行插入。

  3. Because base() returns an iterator pointing at a different element, it's the wrong element to use for erase() . 因为base()返回指向不同元素的迭代器,所以它是用于erase()的错误元素。 If you called erase() on the iterator from base() , you'd erase one element forward in the container from the element that the reverse iterator points at, so you have to increment the reverse iterator before calling base() in order to get the correct forward iterator to use for erase() . 如果从base()调用迭代器上的erase() ,则从反向迭代器指向的元素中删除容器中的一个元素,因此在调用base()之前必须递增反向迭代器以便获取正确的前向迭代器以用于erase()

  4. Whether you can even use base() with erase() to correctly erase an element depends entirely on your implementation. 是否甚至可以使用带有erase() base()来正确擦除元素完全取决于您的实现。 It works with gcc, but with Visual Studio they're really just wrapping a forward iterator in a manner that makes it so that it doesn't work to use erase() when dealing with reverse iterators and Visual Studio. 它适用于gcc,但是使用Visual Studio时,它们实际上只是以一种方式包装一个前向迭代器,以便在处理反向迭代器和Visual Studio时不能使用erase() I don't recall whether insert() has the same problem, but reverse iterators don't work the same between different implementations of C++ (according to the Visual Studio guys, the standard wasn't clear enough), so it can be kind of hairy to use them for anything other than simply iterating over a container. 我不记得insert()是否有相同的问题,但反向迭代器在C ++的不同实现之间不起作用(根据Visual Studio人员,标准不够清晰),所以它可以是友好的毛茸茸的用于除了简单地迭代容器之外的任何东西。

There are probably other issues as well, but dealing with any type of iterator other than a non-const, forward iterator in C++ when doing anything other than simply iterating over a container can get a bit hairy - if you can even do it all - because so many functions require non-const forward iterators rather than any other kind of iterator. 可能还有其他问题,但是除了简单地迭代容器之外,在C ++中处理除了非const,forward迭代器之外的任何类型的迭代器都会有点毛茸茸 - 如果你甚至可以做到这一点 - 因为这么多函数需要非const前向迭代器而不是任何其他类型的迭代器。

If you really want to know the differences between the various iterator types and the issues associated with them, I recommend reading Scott Meyer's Effective STL . 如果您真的想知道各种迭代器类型之间的差异以及与它们相关的问题,我建议您阅读Scott Meyer的Effective STL It has a great chapter on iterators. 它有一个关于迭代器的伟大章节。

EDIT: As for how Boost's reverse iterator corrects those shortcomings, I'm afraid that I don't have a clue. 编辑:至于Boost的反向迭代器如何纠正这些缺点,我担心我没有线索。 I'm aware of some of the standard reverse iterator's shortcomings and have been bitten by them in the past, but I've never used Boost much, so I'm not familiar with their reverse iterators at all. 我知道一些标准的反向迭代器的缺点,过去曾被它们咬过,但我从未使用过很多Boost,所以我根本不熟悉它们的反向迭代器。 Sorry. 抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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