首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将一对迭代器转换为视图?

如何将一对迭代器转换为视图?
EN

Stack Overflow用户
提问于 2021-04-08 16:11:24
回答 3查看 765关注 0票数 3

我有一对迭代器,我想在上面使用ranges::views::filter(some_predicate) (与管道操作符一起)。AFAIU我应该首先将我的对迭代器转换成视图。我试着用ranges::subrange(first, last)来做这件事,但是我收到了可怕的错误消息。

Note1:我使用的是C++14和range-v3版本0.9.1 (最后一个版本兼容gcc-5.5)。如果在使用C++17/20和/或使用C++20 std::range时,解决方案不同,我也想知道更改了什么。

Note2:我发现range-v3的文档严重缺乏,所以我使用cppreference.com。如果你知道更好的文档,我很感兴趣。

编辑:

在我的实际代码中,我包装了一个java风格的遗留迭代器(它有一个next()方法而不是operator++/operator* )。我正在用一个C++兼容的包装包包装它们。然后,我尝试将包装器转换为视图,并最终对其进行过滤。我在哥德波特上复制了一个最小的例子。这按照建议使用iterator_range,但仍然没有编译(请参阅下面的第二个编辑)。

代码语言:javascript
复制
#include "range/v3/all.hpp"
#include "range/v3/iterator_range.hpp"

class LegacyIteratorWrapper {
public:
    using value_type = int;
    using difference_type = std::ptrdiff_t;
    using pointer = value_type*;
    using reference = value_type&;
    using iterator_category = std::input_iterator_tag;

    // The type isn’t default-constructible, the error comes from here
    LegacyIteratorWrapper() = delete;

    static LegacyIteratorWrapper create();
    
    reference operator*() const;
    pointer operator->();
    LegacyIteratorWrapper& operator++();
    LegacyIteratorWrapper operator++(int);
    friend bool operator==(const LegacyIteratorWrapper& a, const LegacyIteratorWrapper& b);
    friend bool operator!=(const LegacyIteratorWrapper& a, const LegacyIteratorWrapper& b);
};

void foo()
{
    LegacyIteratorWrapper begin { LegacyIteratorWrapper::create() };
    LegacyIteratorWrapper end { LegacyIteratorWrapper::create() };
    ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper> rng {begin, end};
    auto _ = rng
        | ranges::views::filter(
            [&](auto _) { return true; }
          )
        ;
}
代码语言:javascript
复制
In file included from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/reference_wrapper.hpp:24:0,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/detail/variant.hpp:33,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/iterator/common_iterator.hpp:26,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/interface.hpp:24,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/ref.hpp:25,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:29,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
                 from <source>:1:
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp: In instantiation of 'constexpr auto ranges::operator|(Arg&&, Pipe) [with Arg = ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper>&; Pipe = ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_>; bool CPP_false_ = false; typename concepts::detail::identity<typename std::enable_if<(static_cast<bool>(((! is_pipeable_v<Arg>) && is_pipeable_v<Pipe>)) || CPP_false_), void>::type>::invoke<int> <anonymous> = 0]':
<source>:33:11:   required from here
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp:63:53: error: no matching function for call to 'ranges::pipeable_access::impl<ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_> >::pipe(ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper>&, ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_>&)'
             return pipeable_access::impl<Pipe>::pipe(static_cast<Arg &&>(arg), pipe);
                                                     ^
In file included from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/range_fwd.hpp:22:0,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:21,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
                 from <source>:1:
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35: note: candidate: template<class Rng, class Vw> static constexpr auto ranges::views::view<View>::pipe(Rng&&, Vw&&, concepts::detail::enable_if_t<concepts::detail::Nil, (static_cast<bool>((viewable_range<Rng> && invocable<View&, Rng>)) || concepts::detail::CPP_false(concepts::detail::Nil{}))>) [with Rng = Rng; Vw = Vw; View = ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_]
             static constexpr auto CPP_fun(pipe)(Rng && rng, Vw && v)( //
                                   ^
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35: note:   template argument deduction/substitution failed:
<source>: In function 'void foo()':
<source>:33:11: error: 'void _' has incomplete type
           )
           ^
ASM generation compiler returned: 1
In file included from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/reference_wrapper.hpp:24:0,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/detail/variant.hpp:33,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/iterator/common_iterator.hpp:26,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/interface.hpp:24,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/ref.hpp:25,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:29,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
                 from <source>:1:
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp: In instantiation of 'constexpr auto ranges::operator|(Arg&&, Pipe) [with Arg = ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper>&; Pipe = ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_>; bool CPP_false_ = false; typename concepts::detail::identity<typename std::enable_if<(static_cast<bool>(((! is_pipeable_v<Arg>) && is_pipeable_v<Pipe>)) || CPP_false_), void>::type>::invoke<int> <anonymous> = 0]':
<source>:33:11:   required from here
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp:63:53: error: no matching function for call to 'ranges::pipeable_access::impl<ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_> >::pipe(ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper>&, ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_>&)'
             return pipeable_access::impl<Pipe>::pipe(static_cast<Arg &&>(arg), pipe);
                                                     ^
In file included from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/range_fwd.hpp:22:0,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:21,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
                 from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
                 from <source>:1:
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35: note: candidate: template<class Rng, class Vw> static constexpr auto ranges::views::view<View>::pipe(Rng&&, Vw&&, concepts::detail::enable_if_t<concepts::detail::Nil, (static_cast<bool>((viewable_range<Rng> && invocable<View&, Rng>)) || concepts::detail::CPP_false(concepts::detail::Nil{}))>) [with Rng = Rng; Vw = Vw; View = ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_]
             static constexpr auto CPP_fun(pipe)(Rng && rng, Vw && v)( //
                                   ^
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35: note:   template argument deduction/substitution failed:
<source>: In function 'void foo()':
<source>:33:11: error: 'void _' has incomplete type
           )
           ^
Execution build compiler returned: 1

EDIT2 (已解决):我得到了一个错误,因为LegacyIteratorWrapper不是默认的可构造结构。这是必需的,以满足常规特征(由范围-v3模拟),这是C++迭代器所要求的,包括默认的可构造性。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-04-08 16:24:17

在range -v3中,有iterator_range,您可以使用它将迭代器包装到range对象中。

在C++20中,可以使用std::span将这些迭代器封装到范围对象中。

票数 -2
EN

Stack Overflow用户

发布于 2021-04-08 19:49:56

我有一对迭代器,我想在上面使用ranges::views::filter(some_predicate) (与管道操作符一起)。AFAIU我应该首先将我的对迭代器转换成视图。我试着用ranges::subrange(first, last)来做这件事,但是我收到了可怕的错误消息。

由于您还没有给出一个代码来备份您收到的可怕的错误消息,下面的代码定义了一个向量上的两个迭代器,使用了ranges::subrange,并过滤了结果范围。

代码语言:javascript
复制
#include <iostream>
#include <range/v3/view/filter.hpp>
#include <range/v3/view/subrange.hpp>
#include <vector>

namespace r = ranges;
namespace rv = ranges::views;

int main() {
    std::vector<int> v{1,2,3,4,5};
    auto even = [](auto x){ return x % 2 == 0; };
    auto i1 = v.begin() + 1;
    auto ie = v.end() - 1;
    auto w = r::subrange(i1, ie) | rv::filter(even);
    for (auto i : w) {
        std::cout << i << std::endl;
    }
}

如果您澄清了您的示例代码是什么以及您所得到的错误,我或其他人可能会给您一个更好的答案。

票数 2
EN

Stack Overflow用户

发布于 2021-06-15 11:41:51

您可以使用以下方法将每对相等的迭代器转换为std::ranges::subrange

代码语言:javascript
复制
auto my_range = std::ranges::subrange{begi,endi};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67007908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档