#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/