0

我正在尝试使用范围比较 2 个数组并想打印它。

#include <algorithm>
#include <iostream>
#include <iterator>
#include <array>
#include <ranges>

int main()
{
  std::array<int, 9> test{0, 2, 3, 8, 0, 5, 4, 0, 1};
  std::array<int, 9> ref{1, 2, 3, 4, 5, 6, 7, 8, 9};

  //auto found = std::views::take_while([ ref, test ](auto *it) -> auto { it = std::ranges::find_first_of(ref, test); return it; });

  auto found1 = std::views::take_while([ref, test]() { return std::ranges::find_first_of(ref, test); });
  int *i{0};
  while (*i != std::ranges::end(found1)){
    std::cout << i << " ";
    i++;
  }
}

但未能编译错误我不明白

$ g++ main.cpp -std=c++20 -o main
main.cpp: In function ‘int main()’:
main.cpp:16:39: error: no match for call to ‘(const std::ranges::__cust_access::_End) (std::ranges::views::__adaptor::_RangeAdaptorClosure<std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{main()::<lambda()>}>::<lambda(_Range&&)> >&)’
   16 |   while (*i != std::ranges::end(found1)){
      |                                       ^

有没有办法打印 found1 ?

4

1 回答 1

0

您可以使用 fmt 库来输出范围,如下所示:

    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <array>
    #include <ranges>
    #include <fmt/format.h>
    #include <fmt/ranges.h>
    
    int main()
    {
      std::array<int, 9> test{0, 2, 3, 8, 0, 5, 4, 0, 1};
      auto v= test|std::ranges::views::take(5);
      fmt::print("{}\n",v);
}
于 2021-04-17T18:31:10.483 回答