返回迭代器范围(主列表的子集)

发布于 2025-01-07 20:04:25 字数 665 浏览 2 评论 0原文

我想编写一个能够返回一系列迭代器来表示更大列表的子集的方法。

这样的“子集”不能由连续的对象组成吗?

例如,如果我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女的英雄梦 2025-01-14 20:04:25

如果你不介意使用Boost,你可以使用 filter_iterator ,例如

struct ShouldIncludeChecker
{
    bool operator()(const Object& obj) const
    {
       return obj == object1 || obj == object3 || obj == object4;
       // ^ Customize this to fit your need.
    }
};

typedef boost::filter_iterator<ShouldIncludeChecker, std::list<Object>::iterator>
        filter_iterator;

ShouldIncludeChecker checker;
std::list<Object>::iterator old_begin = the_list.begin();
std::list<Object>::iterator old_end = the_list.end();
filter_iterator new_begin (checker, old_begin, old_end);
filter_iterator new_end (checker, old_end, old_end);

for (filter_iterator it = new_begin; it != new_end; ++ it)
{
   // read *it
}

此外,如果您正在读取的内容可以制作成输出迭代器,您可以使用std::remove_copy_if

If you don't mind using Boost, you could use filter_iterator, e.g.

struct ShouldIncludeChecker
{
    bool operator()(const Object& obj) const
    {
       return obj == object1 || obj == object3 || obj == object4;
       // ^ Customize this to fit your need.
    }
};

typedef boost::filter_iterator<ShouldIncludeChecker, std::list<Object>::iterator>
        filter_iterator;

ShouldIncludeChecker checker;
std::list<Object>::iterator old_begin = the_list.begin();
std::list<Object>::iterator old_end = the_list.end();
filter_iterator new_begin (checker, old_begin, old_end);
filter_iterator new_end (checker, old_end, old_end);

for (filter_iterator it = new_begin; it != new_end; ++ it)
{
   // read *it
}

Also, if the thing you are reading to can be made into an output iterator, you could just use std::remove_copy_if.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文