为什么const_iterator不提供类似reverse_iterator的基础? [英] Why const_iterator does not provide a base like reverse_iterator?

查看:76
本文介绍了为什么const_iterator不提供类似reverse_iterator的基础?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么const_iterator不提供const_iterator::base()函数来获得与reverse_iterator一样的相应非常量iterator?

Why does const_iterator not provide a const_iterator::base() function, to obtain corresponding non-const iterator like reverse_iterator does?

考虑以下伪代码(例如几何算法):

Considering following pseudocode (say, geometric algorithm):

std::container< point > universe;
auto it = std::cbegin(universe);
std::list< decltype(it) > interesting_subset = sieve(it, std::cend(universe));
auto structure = algorithm(interesting_subset);

其中universe是所有输入点.在sieve() -ing之后,interesting_subset包含迭代器,该迭代器用于universe成员的子集.紧跟在algorithm()之后的是从interesting_subset构造出的structure,它由对universe成员的引用(迭代器)组成.

where universe is all the input points. After sieve()-ing the interesting_subset contains iterators to subset of universe's members. Following algorithm() constructs a resulting structure from interesting_subset, which consists of references (iterators) to members of the universe.

最后,我想将point更改为包含的结果structure(例如,将它们移位).但同样地,我想在algorithm动作期间保护它们免受修改,因此与std::begin/std::end相反,我使用了std::cbegin/std::cend.最后,我只有const_iterator对源point的引用.

At the end, I want to change the points, containing into resulting structure (say, shift them). But equally I want to protect them from modyfining during algorithm action, and therefore I used std::cbegin/std::cend as opposite to std::begin/std::end. Finally I have only const_iterator references to source points.

这是我想在STL容器中显示的iterator std::container< T >::const_iterator::base() const成员函数的一个非常用例.

This is a very use case for iterator std::container< T >::const_iterator::base() const member function I want to be present into STL containers.

推荐答案

为什么const_iterator不提供const_iterator :: base()函数来获取与reverse_iterator一样的对应非const迭代器?

Why does const_iterator not provide a const_iterator::base() function, to obtain corresponding non-const iterator like reverse_iterator does?

保持const安全.如此处已详细讨论的那样,提供这种功能将非常危险.

To maintain const-safety. Providing such function would be very dangerous as already discussed here at length.

最后,我想更改点,将其包含到结果结构中(例如,将它们移位).但是同样,我想在算法操作期间保护它们免受修改,因此我使用了std :: cbegin/std :: cend来代替std :: begin/std :: end.最后,我只有const_iterator对源点的引用.

At the end, I want to change the points, containing into resulting structure (say, shift them). But equally I want to protect them from modyfining during algorithm action, and therefore I used std::cbegin/std::cend as opposite to std::begin/std::end. Finally I have only const_iterator references to source points.

好吧,您要与base成员一起提出错误的要求.当然可以解决您的问题,但是如上所述,这太危险了.让我为您准备一个问题:

Well, you're asking for the wrong thing with the base-member. Sure it would solve your problem, but as said, it's just too dangerous. Let me rephrease a question for you:

如果我有一个对象的const_iterator并且可以对容器进行非常量访问,我如何有效地(在恒定时间内)将一个iterator传递给所引用的对象?

If I have a const_iterator to an object and non-const access to the container, how do I efficiently (in constant time) get an iterator to the referred object?

这里有个花哨的技巧可以做到这一点:

Here's a fancy trick to do exactly that:

template <typename Container, typename ConstIterator>
typename Container::iterator remove_constness(Container& c, ConstIterator it)
{
    return c.erase(it, it);
}

我不相信这个窍门.我从 https://stackoverflow.com/a/10669041/2079303 找到了它们,他们将其归功于

I don't take any credit for the trick. I found it from https://stackoverflow.com/a/10669041/2079303 and they credit Howard Hinnant and Jon Kalb

如该答案的注释中所述,此技巧适用于所有标准容器,但不一定适用于所有可能的符合标准的第三方容器,因为它们不需要提供erase.

As discussed in the comments of that answer, this trick works with all standard containers, but not necessarily with all possible standard-compliant third-party containers because they're not required to provide erase.

就个人而言,我更喜欢标准容器具有一个非常量成员函数,该函数会将给定的const_iterator转换为iterator,但是它们没有,因此您需要解决它.

Personally, I would prefer that standard containers had a non-const member function that would convert a given const_iterator to an iterator, but they don't, so you need to work around it.

这篇关于为什么const_iterator不提供类似reverse_iterator的基础?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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