gpt4 book ai didi

c++ - C++ unordered_map 的问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:47 25 4
gpt4 key购买 nike

我正在为编码面试做准备,所以我一直在一个网站上编写一些 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'"

最佳答案

这一行看起来不对:

 HistoryHash.insert(cur_query, local_min);

std::unordered_map<std::string, std::string>::insert 没有重载这需要两个字符串。想必你想要

HistoryHash.insert(std::make_pair(cur_query, local_min));

关于c++ - C++ unordered_map 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20779887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com