相关疑难解决方法(0)

如何在向量上使用 std::views::transform ?

我正在尝试将视图创建为转换类型的向量。从文档中我读到以下内容应该有效,但编译器输出非常混乱。我缺少什么?

#include <ranges>
#include <vector>

int main() {
    std::vector<int> v {1, 2, 3};
    auto view = v | std::views::transform([](int i){ return std::to_string(i); });
}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/ranges:43:
In file included from /opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/iterator:61:
In file included from /opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/stl_iterator_base_types.h:71:
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/iterator_concepts.h:980:13: error: no matching function for call to '__begin'
        = decltype(ranges::__cust_access::__begin(std::declval<_Tp&>()));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/ranges_base.h:586:5: note: in instantiation of template type alias '__range_iter_t' requested here
    using iterator_t = std::__detail::__range_iter_t<_Tp>;
    ^
/opt/compiler-explorer/gcc-11.2.0/lib/gcc/x86_64-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/ranges_util.h:98:43: note: in instantiation of template type …
Run Code Online (Sandbox Code Playgroud)

c++ c++20 std-ranges

9
推荐指数
1
解决办法
3399
查看次数

标签 统计

c++ ×1

c++20 ×1

std-ranges ×1