I am preparing for coding interviews so I have been writing some c++ code on a website that has a nice grab bag of sample problems. When trying to compile my code on the website (not really sure what compiler version the website runs under the hood) I get a very peculiar unordered_map error. I have never run into this issue before, I did some research and find out the error was occurring due to how I was initializing my unordered_map. When initializing my unordered_map I use a c++ string as my key and value, for some reason the compiler is not liking that. The compiler keeps saying there is no hash function for "basic_string" but I am not using a basic_string, I am using a c++ string

#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

Here is the complete/full error:

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'"
有帮助吗?

解决方案

This line looks wrong:

 HistoryHash.insert(cur_query, local_min);

There is no overload of std::unordered_map<std::string, std::string>::insert which takes two strings. Presumably you want

HistoryHash.insert(std::make_pair(cur_query, local_min));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top