【问题标题】:unordered_map, no match function call errorunordered_map,没有匹配函数调用错误
【发布时间】:2017-01-18 06:58:58
【问题描述】:

unordered_map 的示例程序当我使用c++11 or c++17 在 Linux 终端中运行时,它运行良好。我在eclipse Neon.1a Release(4.6.1) 中使用c++11 or c++17 尝试了相同的程序,给出了许多错误消息,包括

没有匹配函数调用‘boost::unordered::unordered_map >::insert(int, std::pair)’ refmap.insert(1, std::make_pair(2,5));

#include <iostream>
#include <boost/unordered_map.hpp>
#include <utility>
typedef boost::unordered_map<int, std::pair<int, int> > reference_map;
reference_map refmap;
int main(){

        refmap.insert(1, std::make_pair(2,5));
        return 0;
    }

【问题讨论】:

    标签: c++ boost unordered-map


    【解决方案1】:

    insert 接受一个参数,而不是两个——std::pair&lt;K const, V&gt;(又名std::unordered_map&lt;K,V&gt;::value_type):

    int main() {
        refmap.insert(std::make_pair(1, std::make_pair(2, 5)));
    }
    

    为 key 和 value 单独参数的函数命名为 emplace,在大多数情况下应该是首选:

    int main() {
        refmap.emplace(1, std::make_pair(2, 5));
    }
    

    Online Demo

    【讨论】:

    • 谢谢,说得对,但是为什么它可以在 linux 终端中工作
    猜你喜欢
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 2012-12-28
    • 2016-01-18
    • 2015-03-31
    • 1970-01-01
    相关资源
    最近更新 更多