C++ 和 boost 库的哈希问题
hash problems with c++ and boost library
我正在编写图挖掘的代码。以下是完整的源代码:http://pastebin.com/BpjZPcEi
我正在尝试使用 std unordered_set但我在这部分遇到了问题:
bool edgeexist(Graph const& g, int const& fromid, int const& toid, unsigned const& elabel) {
int bn = 0;
if (num_edges(g) != 0) {
edge_pair ep;
for (ep = edges(g); ep.first != ep.second; ++ep.first) // ep edge number
{
vertex_t from = source(*ep.first, g);
vertex_t to = target(*ep.first, g);
edge_t edg = edge(from, to, g);
if ((g[from].id == fromid) && (g[to].id == toid) && (g[edg.first].label == elabel)) {
return true;
}
}
}
return false;
}
std::unordered_set<std::array<int, 3>> edgesdiff(Graph const& g1,Graph const& g2){
std::unordered_set<edge_iter> v1,v2,diff;
std::array<int, 3> t;
std::unordered_set<std::array<int, 3>> res;
for(auto x:edges(g1)){
vertex_t from = source(*x, g1);
t[0]=g1[from].id;
vertex_t to = target(*x, g1);
t[1]=g1[to].id;
edge_t edg = edge(from, to, g1);
t[2]=g1[edg.first].label;
if(!edgeexist(g2,t[0],t[1],t[2])){res.insert(t);}
}
return res;
}
当我在代码块上运行程序时,我收到以下消息:
/usr/include/c++/4.9/bits/hashtable_policy.h|85|error: no match for call to ‘(const hashedge) (const boost::detail::undirected_edge_iter<std::_List_iterator<boost::list_edge<unsigned int, EdgeProperties> >, boost::detail::edge_desc_impl<boost::undirected_tag, unsigned int>, int>&)’|
这是什么意思,我该如何解决这个问题?
读取代码后,无序集合的哈希函数不需要两个参数(更不用说按值☹ Graph
了)。
你至少需要这样的东西(让它可读!!)
struct hashedge {
hashedge(Graph const &g) : _g(g) {}
size_t operator()(const edge_iter e) const {
using namespace boost;
size_t seed = 42;
hash_combine(seed, _g[source(*e, _g)]);
hash_combine(seed, _g[*e].label);
hash_combine(seed, _g[target(*e, _g)]);
return seed;
}
private:
Graph const &_g;
};
这使用除法征服来散列捆绑的属性。因此,您可以单独定义它们:
namespace boost {
template <> struct hash<VertexProperties> {
size_t operator()(VertexProperties const &v) const {
using namespace boost;
auto seed = hash_value(v.id);
hash_combine(seed, v.label);
return seed;
}
};
template <> struct hash<EdgeProperties> {
size_t operator()(EdgeProperties const &e) const {
using namespace boost;
auto seed = hash_value(e.id);
hash_combine(seed, e.label);
return seed;
}
};
}
编译并工作。
现在您在unordered_set<Graph>
方面遇到了类似的问题。我很确定你从错误的一端看待这个问题。你能比较传递冷凝吗?您可以使用过滤后的图表吗?
这是一个部分演示,使用 edge_descriptor
而不是 edge_iterator
(因为,为什么要迭代器?
住在科里鲁
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
#include <unordered_set>
struct VertexProperties {
int id;
int label;
VertexProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
};
struct EdgeProperties {
unsigned id;
unsigned label;
EdgeProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
};
namespace boost {
template <> struct hash<VertexProperties> {
size_t operator()(VertexProperties const &v) const {
auto seed = hash_value(v.id);
hash_combine(seed, v.label);
return seed;
}
};
template <> struct hash<EdgeProperties> {
size_t operator()(EdgeProperties const &e) const {
auto seed = hash_value(e.id);
hash_combine(seed, e.label);
return seed;
}
};
}
struct GraphProperties {
unsigned id;
unsigned label;
GraphProperties(unsigned i = 0, unsigned l = 0) : id(i), label(l) {}
};
// adjacency_list
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties, GraphProperties> Graph;
typedef boost::graph_traits<Graph>::edge_descriptor edge_descriptor;
struct hashedge {
hashedge(Graph const &g) : _g(g) {}
size_t operator()(const edge_descriptor e) const {
using namespace boost;
size_t seed = 42;
hash_combine(seed, _g[source(e, _g)]);
hash_combine(seed, _g[e].label);
hash_combine(seed, _g[target(e, _g)]);
return seed;
}
private:
Graph const &_g;
};
int main() {
std::vector<Graph> dataG;
for (auto& g : dataG) {
auto es = edges(g);
std::unordered_set<edge_descriptor, hashedge> edgehash(es.first, es.second, 0ul, hashedge(g));
}
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 使用Qt C++计算类似Git的SHA1哈希
- 使用rehash的C++哈希表问题
- "Error reading character of string" 为哈希动态增长数组的问题 (C++)
- 使用哈希在3sum问题中重复
- C 无序的设置问题与结构哈希
- 生活游戏哈希表 .h 文件模板问题
- C++ 和 boost 库的哈希问题
- 使用输入数较少的哈希表有什么问题
- 创建自定义哈希函数unordered_map时出现问题
- 在简单的哈希表中解决内存泄漏和语法问题
- C++关于哈希表的问题
- 一些哈希表/unordered_map问题
- 我的哈希函数有问题吗?
- c++:stl哈希编译问题
- 哈希表添加和打印功能问题
- C关于哈希表的问题
- 长哈希填充问题
- bison中哈希查找的问题