C++ 无序地图之间的差异<;常数T,int>;和地图<;常数T,int>;
基于C++ 无序地图之间的差异<;常数T,int>;和地图<;常数T,int>;,c++,stl,C++,Stl,基于 编译源代码。。。。 $g++-std=c++11 main.cpp-o demo-lm-pthread-lgmpxx-lgmp-lreadline 2>&1 在/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0中包含的文件中, 从/usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_-map:47, 来自main.cpp:3: /usr/local/gcc-4.8.1/incl
编译源代码。。。。
$g++-std=c++11 main.cpp-o demo-lm-pthread-lgmpxx-lgmp-lreadline 2>&1
在/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0中包含的文件中,
从/usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_-map:47,
来自main.cpp:3:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:在“struct std::_detail::_Hash_code_base”的实例化中:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1402:10:从“struct std::u detail:u hashtable_base”中需要
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:174:11:从“class std::u hashtable”中需要
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/unordered_-map.h:100:18:从'class std::unordered_-map'中需要
main.cpp:11:38:从这里开始需要
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1070:12:错误:无效使用不完整的类型“struct std::hash”
从21.6中,我们了解到该语言提供了四个与字符串相关的哈希函数:
struct HashConstString
{
long operator()(const string& str) const {
return hash<string>()(str);
}
};
class Solution {
public:
private:
unordered_map<const string, int, HashConstString> mapStrInt; // Case 2: Now it works
};
您可以使用std::string
作为键,也可以将哈希函数指定为hash
,而不依赖默认的模板参数。。编译器会告诉你的。它可能会尝试使用const字符串无法完成的操作。比如,你知道,分配它们我想知道为什么它不合法,我不想依赖编译器。@sehe:我的一个编译器接受它,另一个抱怨没有定义hash
。这还不足以告诉我它是否有效。@MikeSeymour谢谢你提供了一个有趣的数据点问题的主体现在相当冗长。我觉得以前好多了。我们能把它拿回来吗?也许只是你在上面的评论中提到的错误。有时,所有这些问题都是有用的,但我不认为这是其中的一个,而且当它是一般性的、切中要害的时候,这个问题是相当好的创建散列
专门化和使用散列
散列函数的一个示例。+1:Spot on and not the所有我造成的混乱。:)@AdamRosenfield:intmain(void)
yack!)@LightnessRacesinOrbit:Meh,习惯的力量来自于与C代码的兼容性。@AdamRosenfield:我理解。有时候我希望我没有。
template < class Key, // unordered_map::key_type
class T, // unordered_map::mapped_type
class Hash = hash<Key>, // unordered_map::hasher
class Pred = equal_to<Key>, // unordered_map::key_equal
class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type
> class unordered_map;
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47,
from main.cpp:3:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h: In instantiation of 'struct std::__detail::_Hash_code_base<const std::basic_string<char>, std::pair<const std::basic_string<char>, int>, std::__detail::_Select1st, std::hash<const std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>':
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1402:10: required from 'struct std::__detail::_Hashtable_base<const std::basic_string<char>, std::pair<const std::basic_string<char>, int>, std::__detail::_Select1st, std::equal_to<const std::basic_string<char> >, std::hash<const std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >'
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:174:11: required from 'class std::_Hashtable<const std::basic_string<char>, std::pair<const std::basic_string<char>, int>, std::allocator<std::pair<const std::basic_string<char>, int> >, std::__detail::_Select1st, std::equal_to<const std::basic_string<char> >, std::hash<const std::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >'
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/unordered_map.h:100:18: required from 'class std::unordered_map<const std::basic_string<char>, int>'
main.cpp:11:38: required from here
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1070:12: error: invalid use of incomplete type 'struct std::hash<const std::basic_string<char> >'
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.h:3033:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:52,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<const std::basic_string<char> >'
struct hash;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47,
from main.cpp:3:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1070:12: error: invalid use of incomplete type 'struct std::hash<const std::basic_string<char> >'
struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.h:3033:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:52,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<const std::basic_string<char> >'
struct hash;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47,
from main.cpp:3:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type 'struct std::hash<const std::basic_string<char> >'
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.h:3033:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:52,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<const std::basic_string<char> >'
struct hash;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable.h:35:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/unordered_map:47,
from main.cpp:3:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type 'struct std::hash<const std::basic_string<char> >'
using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
^
In file included from /usr/local/gcc-4.8.1/include/c++/4.8.1/bits/basic_string.h:3033:0,
from /usr/local/gcc-4.8.1/include/c++/4.8.1/string:52,
from main.cpp:1:
/usr/local/gcc-4.8.1/include/c++/4.8.1/bits/functional_hash.h:58:12: error: declaration of 'struct std::hash<const std::basic_string<char> >'
struct hash;
^
struct HashConstString
{
long operator()(const string& str) const {
return hash<string>()(str);
}
};
class Solution {
public:
private:
unordered_map<const string, int, HashConstString> mapStrInt; // Case 2: Now it works
};
template <> struct hash<string>;
template <> struct hash<u16string>;
template <> struct hash<u32string>;
template <> struct hash<wstring>;
template <class Key,
class T,
class Hash = hash<Key>,
class Pred = std::equal_to<Key>,
class Alloc = std::allocator<std::pair<const Key, T> > >
class unordered_map;