我正在准备编码采访,所以我一直在一个网站上写一些c ++代码,这些代码有很多样本问题。当试图在网站上编译我的代码时(不确定网站运行的是什么编译器版本)我得到了一个非常特殊的unordered_map错误。我之前从未遇到过这个问题,我做了一些研究,发现错误是由于我如何初始化我的unordered_map而发生的。在初始化我的unordered_map时,我使用c ++字符串作为我的键和值,由于某种原因,编译器不喜欢它。编译器一直说“basic_string”没有哈希函数,但我没有使用basic_string,我使用的是c ++字符串
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
void typeahead(const vector<string> &usernames, const vector<string> &queries) {
//error occurs right here at this initialization
//of HistoryHash
unordered_map<string, string> HistoryHash;
unordered_map<string, string>::iterator it;
string cur_query, cur_usrnm;
locale loc;
//------------------------------------
//rest of code irrelevant
以下是完整/完整错误:
In file included from /usr/include/c++/4.7/unordered_map:45:0,
from user_file.cpp:6:
/usr/include/c++/4.7/bits/hashtable.h: In instantiation of 'void std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::insert(_InputIterator, _InputIterator) [with _InputIterator = std::basic_string<char>; _Key = std::basic_string<char>; _Value = std::pair<const std::basic_string<char>, std::basic_string<char> >; _Allocator = std::allocator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _ExtractKey = std::_Select1st<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _Equal = std::equal_to<std::basic_string<char> >; _H1 = std::hash<std::basic_string<char> >; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; bool
__cache_hash_code = true; bool __constant_iterators = false; bool __unique_keys = true]':
user_file.cpp:56:52: required from here
/usr/include/c++/4.7/bits/hashtable.h:1410:61: error: no matching function for call to '__distance_fw(std::basic_string<char>&, std::basic_string<char>&)'
/usr/include/c++/4.7/bits/hashtable.h:1410:61: note: candidates are:
In file included from /usr/include/c++/4.7/bits/hashtable.h:36:0,
from /usr/include/c++/4.7/unordered_map:45,
from user_file.cpp:6:
/usr/include/c++/4.7/bits/hashtable_policy.h:44:5: note: template<class _Iterator> typename std::iterator_traits::difference_type std::__detail::__distance_fw(_Iterator, _Iterator, std::input_iterator_tag)
/usr/include/c++/4.7/bits/hashtable_policy.h:44:5: note:
template argument deduction/substitution failed:
In file included from /usr/include/c++/4.7/unordered_map:45:0,
from user_file.cpp:6:
/usr/include/c++/4.7/bits/hashtable.h:1410:61: note: candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/4.7/bits/hashtable.h:36:0,
from /usr/include/c++/4.7/unordered_map:45,
from user_file.cpp:6:
/usr/include/c++/4.7/bits/hashtable_policy.h:50:5: note: template<class _Iterator> typename std::iterator_traits::difference_type std::__detail::__distance_fw(_Iterator, _Iterator, std::forward_iterator_tag)
/usr/include/c++/4.7/bits/hashtable_policy.h:50:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.7/unordered_map:45:0,
from user_file.cpp:6:
/usr/include/c++/4.7/bits/hashtable.h:1410:61: note: candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/4.7/bits/hashtable.h:36:0,
from /usr/include/c++/4.7/unordered_map:45,
from user_file.cpp:6:
/usr/include/c++/4.7/bits/hashtable_policy.h:56:5: note: template<class _Iterator> typename std::iterator_traits::difference_type std::__detail::__distance_fw(_Iterator, _Iterator)
/usr/include/c++/4.7/bits/hashtable_policy.h:56:5: note: template argument deduction/substitution failed:
/usr/include/c++/4.7/bits/hashtable_policy.h: In substitution of 'template<class _Iterator> typename std::iterator_traits::difference_type std::__detail::__distance_fw(_Iterator, _Iterator) [with _Iterator = std::basic_string<char>]':
/usr/include/c++/4.7/bits/hashtable.h:1410:61: required from 'void std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::insert(_InputIterator, _InputIterator) [with _InputIterator = std::basic_string<char>; _Key = std::basic_string<char>; _Value = std::pair<const std::basic_string<char>, std::basic_string<char> >; _Allocator = std::allocator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _ExtractKey = std::_Select1st<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _Equal = std::equal_to<std::basic_string<char> >; _H1 = std::hash<std::basic_string<char> >; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; bool __cache_hash_code = true; bool __constant_iterators = false; bool __unique_keys = true]'
user_file.cpp:56:52: required from here
/usr/include/c++/4.7/bits/hashtable_policy.h:56:5: error: no type named 'difference_type' in 'struct std::iterator_traits<std::basic_string<char> >'
In file included from /usr/include/c++/4.7/unordered_map:45:0,
from user_file.cpp:6:
/usr/include/c++/4.7/bits/hashtable.h: In instantiation of 'void std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::insert(_InputIterator, _InputIterator) [with _InputIterator = std::basic_string<char>; _Key = std::basic_string<char>; _Value = std::pair<const std::basic_string<char>, std::basic_string<char> >; _Allocator = std::allocator<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _ExtractKey = std::_Select1st<std::pair<const std::basic_string<char>, std::basic_string<char> > >; _Equal = std::equal_to<std::basic_string<char> >; _H1 = std::hash<std::basic_string<char> >; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; bool __cache_hash_code = true; bool __constant_iterators = false; bool __unique_keys = true]':
user_file.cpp:56:52: required from here
/usr/include/c++/4.7/bits/hashtable.h:1418:2: error: no match for 'operator++' in '++__first'
/usr/include/c++/4.7/bits/hashtable.h:1419:4: error: no match for 'operator*' in '*__first'"
答案 0 :(得分:1)
这一行看起来不对:
HistoryHash.insert(cur_query, local_min);
没有std::unordered_map<std::string, std::string>::insert
的重载需要两个字符串。大概你想要
HistoryHash.insert(std::make_pair(cur_query, local_min));