Per*_*-lk 5 c++ algorithm range c++20
有什么方法可以编译该代码,或者我必须创建自己的 lambda 作为 的第四个参数std::accumulate?
#include <iostream>
#include <ranges>
#include <unordered_map>
#include <numeric>
namespace rv = std::ranges::views;
int main()
{
std::unordered_map<unsigned, unsigned> m = {{5, 3}};
auto values = m | rv::values;
std::cout << std::accumulate(values.begin(), values.end(), 0) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
gcc抛出的错误,如果我理解正确的话,基本上就是说begin并且end产生不同的类型并且std::accumulate不能推断出唯一的InputIterator类型。完整的编译器输出是:
main.cpp:15:65: error: no matching function for call to 'accumulate(std::ranges::elements_view<std::ranges::ref_view<std::unordered_map<unsigned int, unsigned int> >, 1>::_Iterator<true>, std::__detail::_Node_iterator<std::pair<const unsigned int, unsigned int>, false, false>, int)'
15 | std::cout << std::accumulate(values.begin(), values.end(), 0) << std::endl;
| ^
In file included from /usr/local/include/c++/10.2.0/numeric:62,
from main.cpp:5:
/usr/local/include/c++/10.2.0/bits/stl_numeric.h:134:5: note: candidate: 'template<class _InputIterator, class _Tp> constexpr _Tp std::accumulate(_InputIterator, _InputIterator, _Tp)'
134 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
| ^~~~~~~~~~
/usr/local/include/c++/10.2.0/bits/stl_numeric.h:134:5: note: template argument deduction/substitution failed:
main.cpp:15:65: note: deduced conflicting types for parameter '_InputIterator' ('std::ranges::elements_view<std::ranges::ref_view<std::unordered_map<unsigned int, unsigned int> >, 1>::_Iterator<true>' and 'std::__detail::_Node_iterator<std::pair<const unsigned int, unsigned int>, false, false>')
15 | std::cout << std::accumulate(values.begin(), values.end(), 0) << std::endl;
| ^
In file included from /usr/local/include/c++/10.2.0/numeric:62,
from main.cpp:5:
/usr/local/include/c++/10.2.0/bits/stl_numeric.h:161:5: note: candidate: 'template<class _InputIterator, class _Tp, class _BinaryOperation> constexpr _Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation)'
161 | accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
| ^~~~~~~~~~
/usr/local/include/c++/10.2.0/bits/stl_numeric.h:161:5: note: template argument deduction/substitution failed:
main.cpp:15:65: note: deduced conflicting types for parameter '_InputIterator' ('std::ranges::elements_view<std::ranges::ref_view<std::unordered_map<unsigned int, unsigned int> >, 1>::_Iterator<true>' and 'std::__detail::_Node_iterator<std::pair<const unsigned int, unsigned int>, false, false>')
15 | std::cout << std::accumulate(values.begin(), values.end(), 0) << std::endl;
| ^
Run Code Online (Sandbox Code Playgroud)
gcc 抛出的错误,如果我理解正确的话,基本上就是这样说
begin并end产生不同的类型
的确。在范围术语中,elements_view'siterator和sentinel是不同的类型。在 C++20 之前,这些类型必须是相同的,并且大量代码是在假设的情况下编写的。不幸的是,我们还没有ranges::accumulate哪个可以为您正确处理这个问题。
在那之前,还有另一个范围适配器,它强制范围具有相同的类型iterator和sentinel(如果已经是这种情况,则为空操作)views::common::
auto values = m | rv::values | rv::common;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,这将创建一个common_iterators的视图,它基本上是一个variant<iterator, sentinel>包装起来的行为就像一个迭代器。