【发布时间】:2019-12-09 09:05:57
【问题描述】:
我正在尝试使用一对两个迭代器同时迭代两个无序映射。 如果我们遍历两个向量,这个方法就可以正常工作;
#include <iostream>
#include<unordered_map>
using namespace std;
int main()
{
std::unordered_map<std::string,double> mypantry = {{"flour",1.5}};
std::unordered_map<std::string, int> dubVec = {{"key", 5}};
std::unordered_map<std::string, std::string> intVec = {"key", "name"};
double result = 0;
typedef std::unordered_map<std::string, std::string>::iterator intIter;
typedef std::unordered_map<std::string, bool>::iterator dubIter;
for (std::pair<intIter, dubIter> i(intVec.begin(), dubVec.begin());
i.first != intVec.end() && i.second != dubVec.end();
++i.first, ++i.second)
{
cout << i.first.first << "\n" << i.first.second << "\n" << i.second.second;
}
return 0;
}
prog.cpp:在函数“int main()”中:prog.cpp:18:70:错误:没有匹配 调用函数 'std::pair, std::__cxx11::basic_string >, 假,真>,std::__detail::_Node_iterator,布尔>,假,真>
::pair(std::unordered_map, std::__cxx11::basic_string >::iterator, std::unordered_map, int>::iterator)' for (std::pair i(intVec.begin(), dubVec.begin()); ^ 在 /usr/include/c++/5/bits/stl_algobase.h:64:0 包含的文件中, 来自 /usr/include/c++/5/bits/char_traits.h:39, 从 /usr/include/c++/5/ios:40, 来自 /usr/include/c++/5/ostream:38, 来自 /usr/include/c++/5/iostream:39, 来自 prog.cpp:3: /usr/include/c++/5/bits/stl_pair.h:206:9: 注意:候选人: 模板 std::pair<_t1 _t2>::pair(std::tuple<_args1 ...>&, std::tuple<_args2 ...>&, std::_Index_tuple<_indexes1 ...>, std ::_Index_tuple<_indexes2 ...>) 对(元组<_args1...>&,元组<_args2...>&, ^ /usr/include/c++/5/bits/stl_pair.h:206:9:注意:模板参数推导/替换失败:prog.cpp:18:70:注意:
'std::unordered_map, std::__cxx11::basic_string >::iterator {aka std::__detail::__Node_iterator, std::__cxx11::ba
【问题讨论】:
标签: c++11 stl iterator containers unordered-map