什么是的std :: reverse_iterator的的缺点? [英] What are the shortcomings of std::reverse_iterator?

查看:189
本文介绍了什么是的std :: reverse_iterator的的缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升压的文档<一个href=\"http://www.boost.org/doc/libs/release/libs/iterator/doc/index.html#specialized-adaptors\">specialized迭代器适配器指出的boost :: reverse_iterator的更正许多C ++ 98的的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."

这些是什么缺点?我似乎无法找到这些缺点的描述。

What are these shortcomings? I can't seem to find a description of these shortcomings.

后续问题:

如何提高:: reverse_iterator的改正这些缺点?

How does boost::reverse_iterator correct these shortcomings?

推荐答案

好了,最大的问题是,他们没有前向迭代器,这里面的东西,pretty太多期望前向迭代器。所以,你必须做一些有趣的转换得到的东西的工作。要命名一些问题

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. 擦除()和一些版本的插入()要求迭代器,而不是反向迭代器。这意味着,如果你使用一个反向迭代器,你想插入()擦除(),你'重新将不得不使用反向迭代器的基地()函数来得到你的手向前迭代器。没有自动转换功能。

  1. Some versions of erase() and insert() require iterators rather than reverse iterators. 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. There is no automatic conversion.

基地()返回前向迭代等同的反向迭代器在插入的条款。也就是说,在当前元素的前面插入的插入。该反向迭代器所指向的元素,因此,将是错误的元素在,如果基地()给你,指着同一个元素的迭代被人指指点点。因此,它指向一个正向的,你可以用它进行插入。

base() returns the forward iterator equivalent to the reverse iterator in terms of insertion. 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. So, it points one forward, and you can use it for insertion.

由于基地()返回在不同的元素指向的迭代器,这是错误的元素用于擦除()。如果你叫擦除()()迭代器底座,你会删除一个元素向前容器不同于反向迭代点,所以你必须调用基地(),以获得正确的前向迭代器用于<$ C之前递增反向迭代元素$ C>擦除()。

Because base() returns an iterator pointing at a different element, it's the wrong element to use for 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().

无论您甚至可以使用基地()擦除()来正确地删除元素取决于完全在您的实现。它与GCC,但与Visual Studio他们真的只是包装的方式,它使一个前向迭代器,使其不能正常工作使用擦除()时处理反向迭代器和Visual Studio。我不记得是否插入()有同样的问题,但是反向的迭代器不工作的C不同的实现之间的相同++(根据Visual Studio的家伙,在标准是不够清晰),因此它可以是一种毛茸茸使用它们比简单地在一个容器迭代的任何其他。

Whether you can even use base() with erase() to correctly erase an element depends entirely on your implementation. 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. 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.

有可能是其他一些问题,但处理的不是一个非const其他任何类型的迭代器,在C ++做的比简单地在一个容器迭代任何其他时候能有点毛茸茸的前向迭代器 - 如果你甚至可以做到这一切 - 因为这么多的功能需要非const前向迭代器,而不是任何其他种类的迭代器

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.

如果你真的想知道与之相关的各种迭代器类型和问题之间的区别,我建议你阅读斯科特·梅耶的的Effective STL 的。它有一个迭代器伟大的篇章。

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. It has a great chapter on iterators.

编辑:至于Boost的反向迭代如何纠正这些缺点,恐怕我没有头绪。我所知道的一些标准的反向迭代器的缺点,并已被他们在过去咬伤,但我从来没有使用升压多,所以我不熟悉,在他们所有的反向迭代器。对不起。

As for how Boost's reverse iterator corrects those shortcomings, I'm afraid that I don't have a clue. 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. Sorry.

这篇关于什么是的std :: reverse_iterator的的缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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