Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否可以覆盖散列的std::string的==逻辑?_C++_Hash_Unordered Map - Fatal编程技术网

C++ 是否可以覆盖散列的std::string的==逻辑?

C++ 是否可以覆盖散列的std::string的==逻辑?,c++,hash,unordered-map,C++,Hash,Unordered Map,我要做的是使用prime为anagram创建哈希;但是,由于使用了=操作符,必须创建一个额外的结构键有点不方便。是否有一种方法可以重载std::string的现有== #include <string> #include <unordered_map> #include <cstddef> #include <iostream> using namespace std; int F[26] = {2, 3, 5, 7, 11, 13, 17,

我要做的是使用prime为anagram创建哈希;但是,由于使用了
=
操作符,必须创建一个额外的
结构键有点不方便。是否有一种方法可以重载std::string的现有
==

#include <string>
#include <unordered_map>
#include <cstddef>
#include <iostream>


using namespace std;

int F[26] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,  
             31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
             73, 79, 83, 89, 97, 101}; 

size_t f(const string &s) {
  size_t r = 1;
  for (auto c : s) {
    r *= F[c - 'a'] % 9999997;
  }
  return r;
}

// this struct seemed redundant!
struct key {
  const string s;

  key(const string s)
    :s(s) {}

  bool operator ==(const key &k) const {
    return f(s) == f(k.s);
  }
};

struct hasher {
  size_t operator()(const key &k) const {
    return f(k.s); 
  }
};

int main() {
  unordered_map<key, int, hasher> cnt;
  cnt[key{"ab"}]++;
  cnt[key{"ba"}]++;
  cout << cnt[key{"ab"}] << endl;
}
#包括
#包括
#包括
#包括
使用名称空间std;
intf[26]={2,3,5,7,11,13,17,19,23,29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101}; 
大小\u t f(常数字符串和s){
尺寸r=1;
用于(自动c:s){
r*=F[c-'a']%999997;
}
返回r;
}
//这个结构似乎是多余的!
结构键{
常量字符串s;
键(常量字符串s)
:s(s){}
布尔运算符==(常量键和k)常量{
返回f(s)==f(k.s);
}
};
结构哈希器{
大小\u t运算符()(常数键和k)常数{
返回f(k.s);
}
};
int main(){
无序映射cnt;
cnt[键{“ab}]++;
cnt[键{“ba}]+;

无法创建您自己的派生自std::string的类,并将重载运算符添加到该新类中创建您自己的派生自std::string的类,并将重载运算符添加到该新类中无序映射
声明为

template<
    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator<std::pair<const Key, T>>
> class unordered_map;
模板<
类密钥,
T类,
类Hash=std::Hash,
类KeyEqual=std::等于,
类分配器=std::分配器
>类无序图;

因此,您可以将自己的相等谓词替换为第四个模板参数。

unordered\u map
声明为

template<
    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator<std::pair<const Key, T>>
> class unordered_map;
模板<
类密钥,
T类,
类Hash=std::Hash,
类KeyEqual=std::等于,
类分配器=std::分配器
>类无序图;

因此,您可以将自己的相等谓词替换为第四个模板参数。

我在包含列表中看到了
,不再阅读。>@bipll我清理了问题中的包含。我在包含列表中看到了
,不再阅读。>@bipll我清理了问题中的包含。这很危险;base类没有虚拟函数,因此,如果随后将其传递给任何接受
std::string
的接口,则将使用错误的等式这是危险的;基类没有虚拟函数,因此如果随后将其传递给任何接受
std::string
的接口,则将使用错误的等式used@RainbowTom:D“不”;“代码> STD::String 不是设计为基类,而是作为一个值类。C++将阻止您继承<代码> int <代码>,并阻止您继承<代码> STD::String 。@ MsAlter每天都要聪明,@ RunbtoTo:不要;<代码> STD::String < /C> >不是作为基类设计的,而是作为一个值类。C++将阻止您从
int
继承,并且应该阻止您从
std::string
@MSalters继承,谢谢!