“强”迭代器指针/引用 [英] A "strong" iterator pointer/reference

查看:137
本文介绍了“强”迭代器指针/引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这样的事情作为一个强迭代器。我的意思是一个迭代器坚持参考的值,而不是它的地址,这样,如果值被交换到一个不同的地址,迭代器将继续指向它在这个新的地址;不管它在数据结构中移动到哪里?

Is there such thing as a "strong" iterator. What I mean is an iterator that sticks to the value it is referencing rather than the address it is in, such that if the value gets swapped to a different address, the iterator will continue pointing to it in this new address; regardless of where it gets moved to in the data structure?

推荐答案

是和否。但为什么?你想要什么? std :: iter_swap(strong_a,strong_b);

Yes and no. But why? And what would you want std::iter_swap(strong_a, strong_b); to do?

正如Karoly Horvath点out,你可以实现自己的容器,它有自己的迭代器(Iterator只是一个概念,而不是任何一个类型)。

As Karoly Horvath points out, you could implement your own container that has its own iterators (Iterator is just a concept, not any one type).

将很好地与标准库一起工作,这取决于你想到的标准库的一部分。你不能去添加东西到容器(除非你能找出一种方法来实现你想要的分配器或东西)。但是,如果你创建自己的某种方式,你将有一定程度的兼容性与标准的算法。

As for a way to "implement one that would work with the Standard Library nicely", it depends what part of the standard library you have in mind. You can't go adding things to the containers (unless you can figure out a way to implement what you want with Allocators or something). But if you created your own somehow, you you'd have some level of compatibility with the standard algorithms.

最后,你可能实现这样的特殊包装类,它将允许您执行 std :: vector< strong_pointable< int>> 其中 strong_pointable< int> 通常会像int类型,但让我们提供一个方法来获得一个特殊的强指针或任何你想要的,它将跟踪的东西,因为他们得到移动构建。

Finally, you could probably implement something like this with a special wrapper class, that would let you do something like std::vector<strong_pointable<int>> where strong_pointable<int> would generally act like int but let provide a way to get a special "strong pointer" or whatever you want to call it that would keep track of things as they get move-constructed.

但这是一个奇怪的事情要求。我怀疑你做错了,如果你想要这个功能。

But this is an odd thing to ask for. I suspect you are Doing It Wrong if you want this functionality.

这篇关于“强”迭代器指针/引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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