【问题标题】:template argument deduction/substitution failed erro, when iterating two maps simultaneously模板参数扣除/替换失败错误,同时迭代两个映射时
【发布时间】: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


    【解决方案1】:

    你的代码中有几个错别字:

    std::unordered_map<std::string, std::string> intVec = {"key", "name"};
    

    应该是:

    std::unordered_map<std::string, std::string> intVec = {{"key", "name"}};
    //                    notice additional curly brackets ^             ^
    

    之前您尝试使用一对const char[]s 来初始化您的地图,但现在您使用单个键值对来初始化它。


    typedef std::unordered_map<std::string, bool>::iterator dubIter;
    

    应该是:

    typedef std::unordered_map<std::string, int>::iterator dubIter;
    

    以前,dubIter 是一个 typedef 到 string-bool 条目的无序映射的迭代器,但您的 dubVec 中有 string-int 条目。


    cout << i.first.first << "\n" << i.first.second << "\n" << i.second.second;
    

    应该是:

    cout << i.first->first << "\n" << i.first->second << "\n" << i.second->second;
    // notice here ^^                        ^^                          ^^
    

    istd::pair 的迭代器。它的firstsecond 成员是迭代器。键值对的迭代器。要访问这些对的元素,您必须先取消引用迭代器,或者使用-&gt; 语法,而不是.

    【讨论】:

      猜你喜欢
      • 2013-06-20
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      相关资源
      最近更新 更多