迭代器中的转换错误,超过unordered_map的向量

发布于 2025-02-09 07:25:54 字数 2483 浏览 1 评论 0原文

我有一个简单的程序,我想在无序地图的向量上迭代。我是STL的新手,不知道为什么转换存在问题。代码如下所示,

#include<iostream>
#include<vector>
#include<unordered_map>

std::unordered_map<uintptr_t, uintptr_t> umap1{ { 1, 2 }, { 3, 4 }, { 5, 6 } };
std::unordered_map<uintptr_t, uintptr_t> umap2{ { 7, 8 }, { 9, 10 }, { 11, 12 } };
std::unordered_map<uintptr_t, uintptr_t> umap3{ { 13, 14 }, { 15, 16 }, { 17, 18 } };

std::vector<std::unordered_map<uintptr_t, uintptr_t>> crd{ umap1, umap2, umap3 };

constexpr uint8_t ZERO = 0;

std::vector<std::unordered_map<uintptr_t, uintptr_t>>::iterator begin()
{
    return crd[ZERO].begin();
}

std::vector<std::unordered_map<uintptr_t, uintptr_t>>::iterator end()
{
    return crd[ZERO].end();
}

int main()
{
    for (auto it = begin(); it != end(); ++it)
    {
        for (auto it1 = it->begin(); it1 != it->end(); ++it)
        {
            std::cout << it1->first << " " << it1->second << std::endl;
        }
    }
    return 0;
}

这是错误,

mytest.cpp(15): error C2440: 'return': cannot convert from 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' to 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>'
        with
        [
            _Ty=std::pair<const uintptr_t,uintptr_t>
        ]
        and
        [
            _Ty=std::unordered_map<uintptr_t,uintptr_t,std::hash<uintptr_t>,std::equal_to<uintptr_t>,std::allocator<std::pair<const uintptr_t,uintptr_t>>>
        ]
mytest.cpp(15): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
mytest.cpp(20): error C2440: 'return': cannot convert from 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' to 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>'
        with
        [
            _Ty=std::pair<const uintptr_t,uintptr_t>
        ]
        and
        [
            _Ty=std::unordered_map<uintptr_t,uintptr_t,std::hash<uintptr_t>,std::equal_to<uintptr_t>,std::allocator<std::pair<const uintptr_t,uintptr_t>>>
        ]
mytest.cpp(20): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

任何帮助都将不胜感激!提前致谢。

I have a simple program where I want to iterate over a vector of unordered maps. I'm new to STL and don't know why there is a problem in conversion. The code is as follows

#include<iostream>
#include<vector>
#include<unordered_map>

std::unordered_map<uintptr_t, uintptr_t> umap1{ { 1, 2 }, { 3, 4 }, { 5, 6 } };
std::unordered_map<uintptr_t, uintptr_t> umap2{ { 7, 8 }, { 9, 10 }, { 11, 12 } };
std::unordered_map<uintptr_t, uintptr_t> umap3{ { 13, 14 }, { 15, 16 }, { 17, 18 } };

std::vector<std::unordered_map<uintptr_t, uintptr_t>> crd{ umap1, umap2, umap3 };

constexpr uint8_t ZERO = 0;

std::vector<std::unordered_map<uintptr_t, uintptr_t>>::iterator begin()
{
    return crd[ZERO].begin();
}

std::vector<std::unordered_map<uintptr_t, uintptr_t>>::iterator end()
{
    return crd[ZERO].end();
}

int main()
{
    for (auto it = begin(); it != end(); ++it)
    {
        for (auto it1 = it->begin(); it1 != it->end(); ++it)
        {
            std::cout << it1->first << " " << it1->second << std::endl;
        }
    }
    return 0;
}

This is the error

mytest.cpp(15): error C2440: 'return': cannot convert from 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' to 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>'
        with
        [
            _Ty=std::pair<const uintptr_t,uintptr_t>
        ]
        and
        [
            _Ty=std::unordered_map<uintptr_t,uintptr_t,std::hash<uintptr_t>,std::equal_to<uintptr_t>,std::allocator<std::pair<const uintptr_t,uintptr_t>>>
        ]
mytest.cpp(15): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
mytest.cpp(20): error C2440: 'return': cannot convert from 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' to 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>'
        with
        [
            _Ty=std::pair<const uintptr_t,uintptr_t>
        ]
        and
        [
            _Ty=std::unordered_map<uintptr_t,uintptr_t,std::hash<uintptr_t>,std::equal_to<uintptr_t>,std::allocator<std::pair<const uintptr_t,uintptr_t>>>
        ]
mytest.cpp(20): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Any help would be greatly appreciated! Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文