我只是尝试使用boost :: pool来查看它是否是我正在使用的东西的更快的分配器,但我无法弄清楚如何将它与boost :: unordered_map一起使用:
这是一段代码:
unordered_map<int,int,boost::hash<int>, fast_pool_allocator<int>> theMap;
theMap[1] = 2;
Run Code Online (Sandbox Code Playgroud)
这是我得到的编译错误:
错误3错误C2064:term不评估为带有2个参数的函数C:\ Program Files(x86)\ boost\boost_1_38\boost\unordered\detail\hash_table_impl.hpp 2048
如果我注释掉地图的使用,例如"theMap [1] = 2"那么编译错误就会消失.
我为unrorderd_map定义了自己的哈希函数.但是我无法使用find函数在容器中搜索.我尝试使用散列函数中的print语句进行调试,并生成插入键/值时生成的相同散列值.如果有人可以指出错误,那就太好了.我在Windows上使用Eclipse IDE,我正在使用-std = c ++ 11进行编译
typedef struct tree node;
struct tree
{
int id;
node *left;
node *right;
};
class OwnHash
{
public:
std::size_t operator() (const node *c) const
{
cout << "Inside_OwnHash: " <<std::hash<int>()(c->id) + std::hash<node *>()(c->left) + std::hash<node *>()(c->right) << endl;
return std::hash<int>()(c->id) + std::hash<node *>()(c->left) + std::hash<node *>()(c->right);
}
};
int main()
{
std::unordered_map<node *,node *,OwnHash> ut;
node * one = new node;
one->id = -1;
one->left = nullptr;
one->right = nullptr;
ut.insert({one,one});
node * zero …Run Code Online (Sandbox Code Playgroud) 有人可以告诉我,我如何获得boost :: Variant处理无序地图?
typedef boost::variant<long, string, double> lut_value;
unordered_map<lut_value, short> table;
我认为有一个用于boost :: variant的哈希函数,对吗?
编译器说:
./src/lookup/HashMap.o:在函数`std :: __ detail :: _ Hash_code_base中,std :: allocator>,double,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_ ,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_,boost :: detail: :variant :: void_,boost :: detail :: variant :: void_,boost :: detail :: variant :: void_,boost :: detail …
在我的代码中的某些时候,我必须对unordered_map中的所有元素进行操作.为了加速这个过程,我想使用openMP,但天真的方法不起作用:
std::unordered_map<size_t, double> hastTable;
#pragma omp for
for(auto it = hastTable.begin();
it != hastTable.end();
it ++){
//do something
}
Run Code Online (Sandbox Code Playgroud)
原因是,unordered_map的迭代器不是随机访问迭代器.作为替代方案,我尝试了使用for_each的__gnu_parallel指令.但是下面的代码
#include <parallel/algorithm>
#include <omp.h>
__gnu_parallel::for_each (hashTable.begin(), hashTable.end(),[](std::pair<const size_t, double> & item)
{
//do something with item.secon
});
Run Code Online (Sandbox Code Playgroud)
编译(gcc 4.8.2)
g++ -fopenmp -march=native -std=c++11
Run Code Online (Sandbox Code Playgroud)
没有并行运行.使用向量切换unordered_map并使用相同的__gnu_parallel指令并行运行.
为什么在无序地图的情况下不能并行运行?有变通方法吗?
在下面我给你一些简单的代码,它可以重现我的问题.
#include <unordered_map>
#include <parallel/algorithm>
#include <omp.h>
int main(){
//unordered_map
std::unordered_map<size_t, double> hashTable;
double val = 1.;
for(size_t i = 0; i<100000000; i++){
hashTable.emplace(i, val);
val += 1.;
}
__gnu_parallel::for_each (hashTable.begin(), hashTable.end(),[](std::pair<const size_t, double> & …Run Code Online (Sandbox Code Playgroud) 我有一个代码片段,看起来有点像这样:
std::unordered_map<FooId, Foo> fooMap;
Foo foo1(..);
fooMap.emplace(foo1.id(), std::move(foo1));
Run Code Online (Sandbox Code Playgroud)
是安全的,即foo1.id()之前调用的C++语言标准保证std::move(foo1)吗?
std::unordered_map 保证O(1)时间搜索,但它如何管理碰撞?
无序映射是一个关联容器,包含具有唯一键的键值对.元素的搜索,插入和删除具有平均的恒定时间复杂度.
假设所有哈希码都相同的情况,内部如何处理冲突?
如果哈希码对每个键都是唯一的,那么我的假设将完全错误.在这种情况下,如何在没有冲突的情况下创建唯一的哈希码?
std::unordered_map哈希函数采用什么方法来保证O(1)搜索?
假设我有一个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)
我也看到一些人加入黄金比例: …
以下是C++代码,用于计算杂志中的单词数.我试图添加单词,如果它的值不存在,如果它存在,增加它.
unordered_map<string,int>hash;
vector<string> magazine(m);
for(int i = 0;i <m;i++)
{
cin >> magazine[i];
if(hash[magazine[i]]>0)
hash[magazine[i]]++;
else
hash.emplace(magazine[i],1);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试输出时,所有杂志键都将0作为值.知道为什么吗?
我正在尝试使用一对作为键来创建无序映射.这对我来说是新的,所以我按照一些教程编写了这个:
struct pair_hash {
template <class T1, class T2>
std::size_t operator () (const std::pair<T1, T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
return h1 ^ h2;
}
};
int wmain(int argc, wchar_t * argv[])
{
{...}
using Key = std::pair<DWORD, DWORDLONG>;
std::unordered_map<Key, USN, pair_hash> mymap;
std::pair<DWORD, DWORDLONG> mypair(dwVolSN, fileId);
mymap.insert({ mypair, usn });
std::unordered_map<Key, USN>::const_iterator got;
got = mymap.find(mypair); // HERE I GET THE ERROR
return 0
}
Run Code Online (Sandbox Code Playgroud) 我有以下数据结构问题?你能帮帮我吗?因此,当我将新数据项添加到此映射中时,我的要求是将此数据结构初始化为默认值.
我怎样才能有效地做到这一点?
对于我要添加的每个条目,我需要将a1,a2,a3设置为零.
struct a {
int a1;
int a2;
int a3;
};
struct A {
struct a A1;
struct a A2;
};
unordered_map<int, unordered_map<int, struct A>> big_map;
Run Code Online (Sandbox Code Playgroud)
尝试在代码下面运行.
unordered_map<int, struct A> inner_map;
big_map[0] = inner_map;
struct A m;
big_map[0][0] = m;
cout << "Values: " << big_map[0][0].A1.a1 << ", " << big_map[0][0].A1.a2 << ", " << big_map[0][0].A1.a3 << endl;
Run Code Online (Sandbox Code Playgroud)
输出:
g ++ -std = c ++ 11 -o exe b.cc ./exe值:0,0,1518395376 ./exe值:0,0,-210403408 ./exe值:0,0,-1537331360 ./exe值:0,0,-915603664
那么a3的默认初始化没有完成?