我正在尝试建立一个unordered map在n维空间中包含点的位置。我了解std::vector满足成为key的所有要求std::map,但是此代码无法编译。我收到一长串错误消息,但这似乎是最有问题的:
error: no match for call to ‘(const std::hash<std::vector<int> >) (const std::vector<int>&)'.
有谁知道为什么g ++似乎不认为它std::vector<int>是可哈希的?
#include <vector>
#include <unordered_map>
#include <boost/functional/hash.hpp>
using namespace std;
typedef vector<int> point;
int main()
{
unordered_map<point, int>jugSpace;
vector<int> origin(3, 0);
jugSpace.insert( pair<point,int>(origin, 0) );
}
Run Code Online (Sandbox Code Playgroud) 在我的项目的一个头文件中,以下行包含在inline方法中
typedef boost::archive::iterators::transform_width<boost::archive::iterators::binary_from_base64< boost::archive::iterators::remove_whitespace<std::string::const_iterator>>, 8, 6> Base64ToBin;
Run Code Online (Sandbox Code Playgroud)
当我使用gcc 4.8.2编译它时,我收到以下错误:
错误:'boost :: archive :: iterators :: remove_whitespace <__ gnu_cxx :: __ normal_iterator >>'有一个字段'boost :: archive :: iterators :: remove_whitespace <__ gnu_cxx :: __ normal_iterator >>> ::'其类型使用匿名命名空间[-Werror]
我真的很努力,但无法解决这个问题,同样来自link1和link2,看起来这是一个较低版本的gcc的问题.有人可以建议如何使这个警告保持沉默或者克服这个警告.我正在使用-Werror标志编译.
我想为 std::unordered_map 编写自己的 Hash_function 而不是使用默认函数。我可以在许多网站上找到 unordered_map::hash_function()。但是使用这个我只能得到生成的哈希值,使用这样的东西:
/*Sample map of strings*/
unordered_map<string, string> sample;
// inserts key and elements
sample.insert({ "Tom", "MNNIT" });
sample.insert({ "Kate", "MNNIT" });
unordered_map<string, string>::hasher foo
= sample.hash_function();
cout << foo("Tom") << endl;
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能有更多的控制权并创建我自己的散列函数版本?因此,例如,对于键“Tom”,我希望哈希值为 100。
我试图std::hash<T>通过提供专业化来扩展const char,以便我可以const char*用作键类型std::unordered_map.
这是我试过的:
#include <unordered_map>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
namespace std
{
template<>
struct hash<const char*>
{
size_t operator()(const char* const& s) const
{
size_t h = 0;
const char* tmp = s;
while (*tmp)
h = (h << 5) + h + (unsigned char)toupper(*tmp++);
printf("hash of '%s' is (%ld)\n", s, h);
return h;
}
};
}
int main(int argc, char* argv[])
{
const char* name1= "Mark …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用指向自定义类的指针作为键和整数作为值来实现unordered_map.
我认为指针只是一个地址,所以我不必为unordered_map创建比较模板,因为map会在地址之间进行比较.但我得到编译错误.
我的代码如下进行简单测试.任何人都可以帮我解决我做错了什么吗?
#include <cstdlib>
#include <unordered_map>
#include <iostream>
using namespace std;
class MyClass{
public:
MyClass(int id){m_id = id;};
void PrintThis(){cout << " This is test " << endl;};
int m_id;
};
class Test{
public:
unordered_map<MyClass* mc, int test> mapTest;
};
int main(){
MyClass* mc1 = new MyClass(1);
MyClass* mc2 = new MyClass(2);
Test* tt1 = new Test();
tt1->mapTest.insert(make_pair<MyClass*, int>(mc1, 10));
tt1->mapTest.insert(make_pair<MyClass*, int>(mc2, 20));
auto search = tt1->find(mc1);
if(search != tt1->end()) {
search->first->PrintThis();
}else{
cout << "not Found " << …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用结构作为unordered_map. 我将“太空飞船”运算符添加到结构中,这解决了我在正常比较中遇到的错误,例如“结构 1 大于结构 2 吗?”等。但是,当我将attempting to reference a deleted function它用作地图的键时,我遇到了问题。据了解,添加太空船运算符应该允许我使用该结构作为地图键。怎么了?
struct test
{
uint32_t a;
uint32_t b;
auto operator<=>(const test&) const = default;
};
std::unordered_map<test, uint32_t> x; // Causes error
Run Code Online (Sandbox Code Playgroud) 我有一个结构
struct Key {
double x;
double y;
double z;
bool operator==(const Key& k) const{
return (x == k.x && y == k.y && z == k.z);
}
}
Run Code Online (Sandbox Code Playgroud)
我这样做是因为我想把它作为哈希映射的关键.
然后我做
std::unordered_map<Key, int> map = {{1.01, 2.02, 3.03}, 333};
Run Code Online (Sandbox Code Playgroud)
我想使用初始化列表作为构造函数,但我得到错误 no matching constructor for initialization of 'std::unordered_map<key, int> map = {{1.01, 2.02, 3.03}, 333};'