I am currently working through examples from the book Expert C++.
In chapter 7, they offer the following code for mapping a function to a matrix:
#include <vector>
#include <ranges>
#include <iostream>
using IntMatrix = std::vector<std::vector<int>>;
int count_evens(const std::vector<int>& number_line) {
return std::count_if(number_line.begin(),
number_line.end(), [](int num){return num % 2 == 0;});
}
std::vector<int> count_all_evens(const IntMatrix& numbers)
{
return numbers | std::ranges::views::transform(count_evens); // ERROR APPEARS HERE AT |
}
int main()
{
IntMatrix m{{1, 2, 3}, {4, 5, 6}};
for (auto item : count_all_evens(m)) {
std::cout << item << " ";
}
std::cout << std::endl;
return 0;
}
I am getting an error on line 14 that says:
could not convert 'std::ranges::views::__adaptor::operator|<const std::vector<std::vector<int> >&>((* & numbers), std::ranges::views::transform.std::ranges::views::__adaptor::_RangeAdaptor<std::ranges::views::<lambda(_Range&&, _Fp&&)> >::operator()<int (&)(const std::vector<int, std::allocator<int> >&)>(count_evens))' from 'std::ranges::transform_view<std::ranges::ref_view<const std::vector<std::vector<int> > >, int (*)(const std::vector<int>&)>' to 'std::vector<int>'
Does anyone else have this issue? I am using the g++10 compiler.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…