提升无序集不起作用

发布于 2024-11-09 06:27:21 字数 3038 浏览 0 评论 0原文

我是新手,要加强。我正在尝试实现 boost::unorder_set。这是代码:

struct point {
int x;
int y;};
bool operator==(point const& p1, point const& p2) {
return p1.x == p2.x && p1.y == p2.y; 
}
struct point_hash {{
size_t operator()(point const& p) const
{
    size_t seed = 0;
    hash_combine(seed, p.x);
    hash_combine(seed, p.y);
    return seed;
}};
int main() {
point pt;
unordered_multiset<point,point_hash> points(pt);
}

我收到以下错误:

In instantiation of ‘boost::intrusive::do_pack<boost::intrusive::uset_defaults<point>, point_hash>’:
instantiated from ‘boost::intrusive::pack_options<boost::intrusive::uset_defaults<point>, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>’
instantiated from ‘boost::intrusive::make_hashtable_opt<point, false, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>’
instantiated from ‘boost::intrusive::make_unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>’
instantiated from ‘boost::intrusive::unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>’
boost_example.cpp:29:   instantiated from here
error: no class template named ‘pack’ in ‘struct point_hash’
boost_example.cpp: In function ‘int main()’:
boost_example.cpp:29: error: no matching function for call to ‘boost::intrusive::unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>::unordered_multiset(point&)’
note: candidates are: boost::intrusive::unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>::unordered_multiset(const boost::intrusive::unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>&)

我的实现有什么问题?提前致谢。

I am new to boost. I am trying to implement boost::unorder_set. Here is the code:

struct point {
int x;
int y;};
bool operator==(point const& p1, point const& p2) {
return p1.x == p2.x && p1.y == p2.y; 
}
struct point_hash {{
size_t operator()(point const& p) const
{
    size_t seed = 0;
    hash_combine(seed, p.x);
    hash_combine(seed, p.y);
    return seed;
}};
int main() {
point pt;
unordered_multiset<point,point_hash> points(pt);
}

I get following error:

In instantiation of ‘boost::intrusive::do_pack<boost::intrusive::uset_defaults<point>, point_hash>’:
instantiated from ‘boost::intrusive::pack_options<boost::intrusive::uset_defaults<point>, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>’
instantiated from ‘boost::intrusive::make_hashtable_opt<point, false, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>’
instantiated from ‘boost::intrusive::make_unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>’
instantiated from ‘boost::intrusive::unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>’
boost_example.cpp:29:   instantiated from here
error: no class template named ‘pack’ in ‘struct point_hash’
boost_example.cpp: In function ‘int main()’:
boost_example.cpp:29: error: no matching function for call to ‘boost::intrusive::unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>::unordered_multiset(point&)’
note: candidates are: boost::intrusive::unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>::unordered_multiset(const boost::intrusive::unordered_multiset<point, point_hash, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none, boost::intrusive::none>&)

What is wrong with my implementation? Thanx in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

债姬 2024-11-16 06:27:21

unordered_multiset 无法从单个元素构造(它没有这样的构造函数)。试试这个:

point pt;
boost::unordered_multiset<point,point_hash> points;
points.insert( pt );

您还需要正确包含标题:

#include <boost/unordered_set.hpp>

unordered_multiset cannot be constructed from a single element (it has no such a constructor). Try this:

point pt;
boost::unordered_multiset<point,point_hash> points;
points.insert( pt );

Also you need properly included header:

#include <boost/unordered_set.hpp>
时光瘦了 2024-11-16 06:27:21

您只需将一个参数传递给 unordered_multiset<>,boost::hash 就会完成其余的工作,因为它知道如何对您的自定义数据类型进行哈希处理。

#include "boost/unordered_set.hpp"

struct point {
  int x;
  int y;
  // The following ctor isn't required, but I'm biased against garbage in tests
  point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}
};

bool operator==(point const& p1, point const& p2) {
  return p1.x == p2.x && p1.y == p2.y;
}

std::size_t hash_value(point const& p){
  std::size_t seed =0;
  boost::hash_combine(seed, p.x);
  boost::hash_combine(seed, p.y);
  return seed;
}

int main() {
  boost::unordered_multiset<point> points;
  struct point p;
  points.insert(p);
  points.insert(p);
  return 0;
}

You only need to pass one parameter to unordered_multiset<>, boost::hash does the rest since it knows how to hash your custom data types.

#include "boost/unordered_set.hpp"

struct point {
  int x;
  int y;
  // The following ctor isn't required, but I'm biased against garbage in tests
  point(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}
};

bool operator==(point const& p1, point const& p2) {
  return p1.x == p2.x && p1.y == p2.y;
}

std::size_t hash_value(point const& p){
  std::size_t seed =0;
  boost::hash_combine(seed, p.x);
  boost::hash_combine(seed, p.y);
  return seed;
}

int main() {
  boost::unordered_multiset<point> points;
  struct point p;
  points.insert(p);
  points.insert(p);
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文