迭代器中的转换错误,超过unordered_map的向量
我有一个简单的程序,我想在无序地图的向量上迭代。我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论