返回迭代器范围(主列表的子集)
我想编写一个能够返回一系列迭代器来表示更大列表的子集的方法。
这样的“子集”不能由连续的对象组成吗?
例如,如果我有一个包含 n 个对象的 std::list 。
例如。
| object1 | object2 | object3 | object4 | ... | objectn
我可以返回一系列迭代器(一对开始/结束,我们可以称为 ItBegin 和 ItEnd),其中仅包含对象 1,3 和 4 吗?
例如。
| object1 | object2 | object3 | object4 | ... | objectN
^ ^ ^ ^
| | | |
ItBegin ++ItBegin ++ItBegin ItEnd
这是否可能,或者我是否需要将对象复制(或使用指针以避免复制)到新列表并返回该列表?
请注意,迭代器子集仅用于读取。 (一对 const_iterator 就可以完成这项工作)
谢谢! 贾科莫
I would like to code a method capable of returning a range of iterators to represent a subset of a bigger list.
Can such "subset" not have being made of contiguous objects ?
For example, if I have a std::list with n objects.
eg.
| object1 | object2 | object3 | object4 | ... | objectn
Can I return a range of iterators (a pair of begin / end we can call ItBegin and ItEnd) which would include only Object 1,3 and 4 ?
eg.
| object1 | object2 | object3 | object4 | ... | objectN
^ ^ ^ ^
| | | |
ItBegin ++ItBegin ++ItBegin ItEnd
Is that possible or do I need to copy the objects (or use pointers to avoid the copy) onto a new list and return that list ?
Please note that the iterator(s) subset would just be used for reading. (a pair of const_iterator would do the job)
Thanks!
Giacomo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你不介意使用Boost,你可以使用 filter_iterator ,例如
此外,如果您正在读取的内容可以制作成输出迭代器,您可以使用
std::remove_copy_if
。If you don't mind using Boost, you could use filter_iterator, e.g.
Also, if the thing you are reading to can be made into an output iterator, you could just use
std::remove_copy_if
.