简体   繁体   English

c ++迭代器的向量

[英]c++ vector of iterators

我知道我可以在一个vector<int*> onev_point_2_all等上使用for循环指向向量std::vector<int> vector<int*> onev_point_2_all 。但是我如何使用迭代器来实现这一点是否有一种创建迭代器向量的方法指针矢量?

You can have a vector of iterators, not necessary iterators of a vector and not necessarily iterators of the same collection, but they must all be of the same type. 你可以有一个迭代器的向量,不是向量的必要迭代器,也不一定是同一个集合的迭代器,但它们必须都是相同的类型。

You would not need the underlying collection to be able to dereference them, so if you know they are all valid iterators and that's all you want to do, you would have no problem doing that. 你不需要底层集合能够取消引用它们,所以如果你知道它们都是有效的迭代器而且你想要做的就是这样,那么你就没有问题了。 If they are non-const iterators you can also set their values (if not set iterators). 如果它们是非const迭代器,您还可以设置它们的值(如果没有设置迭代器)。

If you need to check them to see if they are "end" iterators, or remove them from their collection then you will need to know the underlying collection behind the iterator. 如果你需要检查它们是否是“结束”迭代器,或者从它们的集合中删除它们,那么你需要知道迭代器后面的底层集合。 If they all come from the same collection that is straightforward, but if they come from different collections you would need to store a pointer to their underlying collection with them. 如果它们都来自同一个简单的集合,但如果它们来自不同的集合,则需要使用它们存储指向其底层集合的指针。 Note that it would have to be a pointer to the underlying collection, not a reference, as if they are vector items they must be assignable. 请注意,它必须是指向底层集合的指针,而不是引用,就像它们是矢量项一样,它们必须是可分配的。

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

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