在.NET GetHashCode方法中,很多地方都使用.NET 方法.特别是在快速查找集合中的项目或确定相等性时.是否有关于如何GetHashCode为我的自定义类实现覆盖的标准算法/最佳实践,因此我不会降低性能?
C++ 0x添加hash<...>(...).
我找不到一个hash_combine函数,如boost中所示.实现这样的事最简洁的方法是什么?也许,使用C++ 0x xor_combine?
我正在尝试unordered_map使用整数创建一个映射对:
#include <unordered_map>
using namespace std;
using Vote = pair<string, string>;
using Unordered_map = unordered_map<Vote, int>;
Run Code Online (Sandbox Code Playgroud)
我有一个班级,我已宣布Unordered_map为私人会员.
但是,当我尝试编译它时,我收到以下错误:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/type_traits:948:38:未定义模板的隐式实例化'std :: __ 1 :: hash,std :: __ 1: :basic_string >>'
如果我使用常规地图map<pair<string, string>, int>而不是使用,我没有收到此错误unordered_map.
是否无法pair在无序地图中使用密钥?
我有一个unordered_map成员的以下类,并定义了一个哈希函数pair<int,int>
class abc
{public :
unordered_map < pair<int,int> , int > rules ;
unsigned nodes;
unsigned packet ;
};
namespace std {
template <>
class hash < std::pair< int,int> >{
public :
size_t operator()(const pair< int, int> &x ) const
{
size_t h = std::hash<int>()(x.first) ^ std::hash<int>()(x.second);
return h ;
}
};
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
error: invalid use of incomplete type ‘struct std::hash<std::pair<int, int> >
error: declaration of ‘struct std::hash<std::pair<int, int> >
error: type ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<std::pair<int, int> >, …Run Code Online (Sandbox Code Playgroud) 为什么不std::unordered_map<tuple<int, int>, string>开箱即用?必须为tuple<int, int>例如定义散列函数是繁琐的
template<> struct do_hash<tuple<int, int>>
{ size_t operator()(std::tuple<int, int> const& tt) const {...} };
Run Code Online (Sandbox Code Playgroud)
构建一个以元组为键的无序映射(Matthieu M.)展示了如何自动执行此操作boost::tuple.有没有为c ++ 0x元组执行此操作而不使用可变参数模板?
当然这应该在标准:(
问题
对于用户定义类型的std :: unordered_map或std :: unordered_set的第三个模板参数,std :: hash有什么好的特殊性,所有成员数据类型都已经具有良好的std :: hash特性?
对于这个问题,我将"好"定义为易于实现和理解,合理有效,并且不太可能产生哈希表冲突.商品的定义不包括任何有关安全性的陈述.
什么是谷歌的状态
目前,两个StackOverflow问题是Google搜索"std hash specialization"的第一个问题.
第一个,如何在无序容器中为用户定义的类型专门化std :: hash :: operator()?,解决了打开std命名空间和添加模板特化是否合法的问题.
第二个,如何专门化来自其他库的类型的std :: hash,基本上解决了同样的问题.
这留下了当前的问题.鉴于C++标准库的实现为标准库中的基本类型和类型定义了散列函数,为用户定义的类型专门化std :: hash的简单有效方法是什么?有没有一种很好的方法来组合标准库实现提供的哈希函数?
(编辑感谢dyp.)StackOverflow的另一个问题是如何组合一对哈希函数.
谷歌的其他结果没有任何帮助.
这篇 Dobbs博士的文章指出,两个令人满意的哈希的XOR将产生一个新的令人满意的哈希值.
这篇文章似乎是从知识中说出并暗示了很多东西,但却注重细节.它与第一个例子中的简短评论中的Dr. Dobbs文章相矛盾,称使用XOR组合散列函数会产生一个弱的结果散列函数.
因为XOR应用于任何两个相等的值导致0,我可以看出为什么XOR本身很弱.
元问题
一个很好的理由回答解释为什么这个问题无效且一般无法回答也是受欢迎的.
我的问题与集合操作中的用户定义函数有关,但我认为我可以切入问题的核心:
如何选择特定的散列函数?例如,如果我想进行基于值的匹配而不是引用匹配,并且我想查看某个元组是否存在(或者只是删除它):
my %data := SetHash.new: (1, 2), (3, 4);
%data{$(1, 2)}:delete; # False
Run Code Online (Sandbox Code Playgroud)
在C++或C#中,我可以为构造函数提供自定义哈希/比较函数.在C#中,如果我的数据类型是a struct(值类型而不是引用类型),则会自动按值进行散列.Perl 6在某种程度上进行了值类型哈希Pair(如果Pair不包含任何容器),但我不知道如何使它适用于任何其他复杂类型.
一方面,我明白为什么这不是最安全的操作 - 很容易定义哈希代码插入后可以更改的对象.但这并没有阻止.NET和C++ STL允许自定义散列.
一种可能的API使用(与由激发了链式散列逻辑此,最初来自升压)将是:
class MyHasher does Hasher of Array[Int] {
method get-hash-value(Int @array) {
reduce
-> $a, $b {$a +^ ($b + 0x9e3779b97f4a7c16 + ($a +< 6) + ($a +> 2))},
0,
|@array;
}
method equals(Int @a, Int @b) { @a eqv @b; }
}
my %data := SetHash.new(
my Int @=[1, 2], my …Run Code Online (Sandbox Code Playgroud) 我想将HashSet用作的键HashMap。这可能吗?
use std::collections::{HashMap, HashSet};
fn main() {
let hmap: HashMap<HashSet<usize>, String> = HashMap::new();
}
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
use std::collections::{HashMap, HashSet};
fn main() {
let hmap: HashMap<HashSet<usize>, String> = HashMap::new();
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个struct/ class具有任意数量的属性,我想用它作为std::unordered_map例如:
struct Foo {
int i;
double d;
char c;
bool b;
};
Run Code Online (Sandbox Code Playgroud)
我知道我必须为它定义一个hasher-functor,例如:
struct FooHasher {
std::size_t operator()(Foo const &foo) const;
};
Run Code Online (Sandbox Code Playgroud)
然后将我定义std::unordered_map为:
std::unordered_map<Foo, MyValueType, FooHasher> myMap;
Run Code Online (Sandbox Code Playgroud)
令我烦恼的是,如何定义呼叫运算符FooHasher.一种方法,我也倾向于喜欢,是std::hash.但是,有很多变化,例如:
std::size_t operator()(Foo const &foo) const {
return std::hash<int>()(foo.i) ^
std::hash<double>()(foo.d) ^
std::hash<char>()(foo.c) ^
std::hash<bool>()(foo.b);
}
Run Code Online (Sandbox Code Playgroud)
我也看到了以下方案:
std::size_t operator()(Foo const &foo) const {
return std::hash<int>()(foo.i) ^
(std::hash<double>()(foo.d) << 1) ^
(std::hash<char>()(foo.c) >> 1) ^
(std::hash<bool>()(foo.b) << 1);
}
Run Code Online (Sandbox Code Playgroud)
我也看到一些人加入黄金比例: …
我正在使用CodeIgniter构建一个快速站点,目前我正在使用CI'加密'库.通过这里阅读听起来像我只是快速:
$this->encrypt->encode($secret_data);
Run Code Online (Sandbox Code Playgroud)
并在您想要使用它时执行此操作:
$this->encrypt->decode($encripted_string);
Run Code Online (Sandbox Code Playgroud)
然后CI和Mcrypt的魔力完成其余的工作.
好吧,我不确定在不知道后台发生了什么的情况下我可以睡觉.所以我有两个问题......
这是怎么回事?或者是否有一个很好的资源向我解释这是如何工作的,我可以阅读它?
这通常被认为是加密数据的安全方式吗?如果不是我应该在哪里看.
php encryption cryptography codeigniter encryption-symmetric
c++ ×6
c++11 ×5
hash ×3
.net ×1
algorithm ×1
boost ×1
codeigniter ×1
cryptography ×1
dictionary ×1
encryption ×1
gethashcode ×1
hashcode ×1
hashmap ×1
hashset ×1
keyvaluepair ×1
overloading ×1
perl6 ×1
php ×1
rust ×1
std ×1
stdhash ×1
stl ×1
tuples ×1