没有匹配的成员函数来调用 "insert" std::unordered_map
No matching member function for call to "insert" std::unordered_map
我正在尝试将string
散列为pointer to a void function which takes in a string
。当我试图将我的键值对插入到地图中时,我得到了以下错误:
"调用"insert"没有匹配的成员函数
我不知道如何解释这个错误。
我想我要么是为insert传递了错误的类型,要么是函数引用错误,要么是对函数指针进行了类型定义错误。
#include <string>
#include <unordered_map>
using namespace std;
void some_function(string arg)
{
//some code
}
int main(int argc, const char * argv[]) {
typedef void (*SwitchFunction)(string);
unordered_map<string, SwitchFunction> switch_map;
//trouble with this line
switch_map.insert("example arg", &some_function);
}
如有任何建议,我们将不胜感激。
如果您查看std::unordered_map::insert
的重载,您将看到以下内容:
std::pair<iterator,bool> insert( const value_type& value );
template< class P >
std::pair<iterator,bool> insert( P&& value );
std::pair<iterator,bool> insert( value_type&& value );
iterator insert( const_iterator hint, const value_type& value );
template< class P >
iterator insert( const_iterator hint, P&& value );
iterator insert( const_iterator hint, value_type&& value );
template< class InputIt >
void insert( InputIt first, InputIt last );
void insert( std::initializer_list<value_type> ilist );
没有insert(key_type, mapped_type)
,这就是你想要做的。你的意思是:
switch_map.insert(std::make_pair("example arg", &some_function));
如果您想在地图中放置一个新条目,而不需要自己实际创建新条目(也称为std::pair
),则使用以下两种形式之一:
switch_map.emplace("example.org", &some_function);
// OR:
switch_map["example.org"] = &some_function;
方法insert
仅用于将PAIRS添加到映射中
如果你需要使用插入,那么你必须制作一对,正如@Barry在他的回答中所示。
以下代码运行良好。
#include<iostream>
#include <string>
#include <unordered_map>
using namespace std;
void some_function(string arg)
{
return;
//some code
}
int main(int argc, const char * argv[]) {
typedef void (*SwitchFunction)(string);
unordered_map<string, SwitchFunction> switch_map;
//trouble with this line
switch_map.insert(std::make_pair("example arg", &some_function));
}
您必须使用std::make_pair来插入值。
相关文章:
- 为什么使用 <uint8_t>MSVC 2015 编译器的 std::vector::insert 比 std::copy 快 5 倍?
- 如果迭代器没有因插入而无效,则使用std::find和C::insert()是线程安全的
- 与C++std::map::insert行为相比,C#排序字典的效率
- std::list 的两个 insert() 方法签名之间的实现差异
- 为什么 std::set::erase 与 std::set::insert 不一致?
- std::map 在 [] 上调用默认构造函数,在 insert() 上调用复制构造函数
- std::map insert && hyperload 导致复制
- 为什么在以下示例中 std::map::insert 失败?
- 排序时在空 std::vector 上使用 insert()
- 为什么 std::queue 没有实现 insert() 而 std::d eque 实现了?
- 如何在 C++ 中使用 std::insert in 2D map
- std :: set :: insert()分段故障
- 当 std::map::insert 找到该元素时,它仍构造该对象的实例.我怎样才能阻止这种情况
- std::unordered_map:渐近{search,insert,remove}性能取决于密钥的大小和数据类型
- 重载分辨率如何适用于 std::vector:<int>:insert
- 为什么 std::map 没有 insert(key &k, val &v) 类型的插入函数
- C 将HEAP对象插入std ::用insert()插入映射,而另一个则存在删除新的对象
- std ::矢量引用将在.insert()上复制
- 为什么 gcc-4.9.2 不能支持 std::string.insert(迭代器,范围)返回迭代器
- std::map::insert exception