1

我尝试使用 Range-V3 库(用于 MSVC),但由于缺乏文档,我不明白如何做一件事。

std::map<int, std::wstring> ss = { {1,L"1"}, {2,L"2"}, {3,L"3"} };
auto rng = ss | ranges::view::reverse | ranges::view::values;
auto it = ranges::find_if(rng, [](auto&&x) {return x == L"2"; });
if (it != rng.end()) {
    assert(it.base()->first == 2); // this does not compile
}

我从中得到find_if什么?这是迭代器吗?我想获得一个基本元素的迭代器,即ss映射中的值。

这是我得到的错误:

1>d:\sources\ranges_test\ranges_test.cpp(11): error C2819: type 'ranges::v3::basic_iterator<ranges::v3::adaptor_cursor<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,ranges::v3::reverse_view<ranges::v3::sized_range<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>>>::adaptor<true>>,ranges::v3::adaptor_cursor<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,ranges::v3::reverse_view<ranges::v3::sized_range<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>>>::adaptor<true>>>' does not have an overloaded member 'operator ->'
1>        with
1>        [
1>            _Kty=int,
1>            _Ty=std::wstring
1>        ]
1>d:\sources\fingrad\dev.fingrad\src\vc\lib\range\v3\utility\basic_iterator.hpp(656): note: see declaration of 'ranges::v3::basic_iterator<ranges::v3::adaptor_cursor<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,ranges::v3::reverse_view<ranges::v3::sized_range<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>>>::adaptor<true>>,ranges::v3::adaptor_cursor<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,ranges::v3::reverse_view<ranges::v3::sized_range<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>>>::adaptor<true>>>'
1>        with
1>        [
1>            _Kty=int,
1>            _Ty=std::wstring
1>        ]
1>d:\sources\ranges_test\ranges_test.cpp(11): note: did you intend to use '.' instead?
1>d:\sources\ranges_test\ranges_test.cpp(11): error C2039: 'first': is not a member of 'ranges::v3::basic_iterator<ranges::v3::adaptor_cursor<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,ranges::v3::reverse_view<ranges::v3::sized_range<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>>>::adaptor<true>>,ranges::v3::adaptor_cursor<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,ranges::v3::reverse_view<ranges::v3::sized_range<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>>>::adaptor<true>>>'
1>        with
1>        [
1>            _Kty=int,
1>            _Ty=std::wstring
1>        ]
1>d:\sources\fingrad\dev.fingrad\src\vc\lib\range\v3\utility\basic_iterator.hpp(656): note: see declaration of 'ranges::v3::basic_iterator<ranges::v3::adaptor_cursor<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,ranges::v3::reverse_view<ranges::v3::sized_range<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>>>::adaptor<true>>,ranges::v3::adaptor_cursor<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,ranges::v3::reverse_view<ranges::v3::sized_range<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>>>::adaptor<true>>>'
1>        with
1>        [
1>            _Kty=int,
1>            _Ty=std::wstring
1>        ]
4

1 回答 1

3

这看起来像是范围 V3的MSVC 实现中的错误1 (或者更确切地说是缺少的功能)。一个简单的解决方法是将您的代码替换为:

assert((*it.base()).first == 2);

这应该可以工作,因为虽然不是operator*超载。basic_iteratoroperator->


1如果您查看 MSVC 源代码basic_iterator,您会注意到 for 没有重载operator->,而原始 range-v3 中有一个basic_iterator

于 2018-01-19T10:10:11.517 回答