c++ - 为什么在使用 unordered_map 的 emplace 方法时会出现编译错误?

标签 c++ hashtable unordered-map

#include <string>
#include <unordered_map>

using namespace std;

....
....
unordered_map<char, int> hashtable;
string str = "hello";
char lower = tolower(str[0]);
hashtable.emplace(lower, 1);
....

返回以下编译错误:

1   error C2780: 'std::pair<_Ty1,_Ty2> std::_Hash<_Traits>::emplace(_Valty &&)' : expects 1 arguments - 2 provided  
2   IntelliSense: no instance of function template "std::tr1::unordered_map<_Kty, _Ty, _Hasher, _Keyeq, _Alloc>::emplace [with _Kty=char, _Ty=int, _Hasher=std::hash<char>, _Keyeq=std::equal_to<char>, _Alloc=std::allocator<std::pair<const char, int>>]" matches the argument list

最佳答案

您使用的是旧版本的 Visual C++,它不能正确支持 emplace。可能是 Visual C++ 2010。

作为Visual C++ Team Blog曾经说过:

As required by C++11, we've implemented emplace()/emplace_front()/emplace_back()/emplace_hint()/emplace_after() in all containers for "arbitrary" numbers of arguments (see below).

(...)

VC10 supported emplacement from 1 argument, which was not especially useful.

最好的解决方案是升级到最新版本的编译器。

关于c++ - 为什么在使用 unordered_map 的 emplace 方法时会出现编译错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31882282/

相关文章:

java - 为什么 LinkedHashSet<E> 扩展 HashSet<e> 并实现 Set<E>

c++ - 据说 map.find() 和 map.end() 迭代器不兼容?

C++ 从模板函数中存储数据?

c++ - QTKit 没有找到任何附加的视频输入设备

c++ - LoadLibrary 与将二进制 dll 加载到进程中?

c - 独特的哈希函数,无任何冲突

c++ - c++中使用unordered_map实现哈希表并处理冲突

c++ - 将 32 位和 64 位应用程序(使用相同的代码)编译成一个 exe

c++ - 如何在循环中将元素添加到空 vector ?

C 编译器错误 "expected ... before ..."和无效类型参数