如何比较 boost::variant 以便使其成为 std::map 的键?

发布于 2024-10-05 13:22:12 字数 84 浏览 0 评论 0原文

如何比较 boost::variant 以便使其成为 std::map 的键? 似乎没有为 boost::variant 定义operator<()

How to compare boost::variant in order to make it a key of std::map ?
Seems that operator<() is not defined for boost::variant

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

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

发布评论

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

评论(2

情泪▽动烟 2024-10-12 13:22:12

已编辑以修复应用 BOOST::APPLY_VISITOR 时出现的错误

您可以为您的变体创建一个二进制访问者,然后使用 boost::apply_visitor 为您的地图创建一个比较器:

class variant_less_than
    : public boost::static_visitor<bool>
{
public:

    template <typename T, typename U>
    bool operator()( const T & lhs, const U & rhs ) const
    {
            // compare different types
    }

    template <typename T>
    bool operator()( const T & lhs, const T & rhs ) const
    {
            // compare types that are the same
    }

};

您可能需要重载 operator() 用于每个可能的类型对,而不是使用模板化的operator(const T &, const U &)。然后你需要像这样声明你的地图:

class real_less_than
{
public:
  template<typename T>
  bool operator()(const T &lhs, const T &rhs)
  {
    return boost::apply_visitor(variant_less_than(), lhs, rhs);
  }
};

std::map<boost::variant<T, U, V>, ValueType, real_less_than> myMap;

编辑:对于它的价值, operator<() 是为 boost::variant 定义的,但它定义为

bool operator<(const variant &rhs) const
{
  if(which() == rhs.which())
    // compare contents
  else
    return which() < rhs.which();
}

:我假设这不是你想要的。

EDITED TO FIX ERROR APPLYING BOOST::APPLY_VISITOR

You can create a binary visitor for your variant, and then use boost::apply_visitor to create a comparator for your map:

class variant_less_than
    : public boost::static_visitor<bool>
{
public:

    template <typename T, typename U>
    bool operator()( const T & lhs, const U & rhs ) const
    {
            // compare different types
    }

    template <typename T>
    bool operator()( const T & lhs, const T & rhs ) const
    {
            // compare types that are the same
    }

};

You'll probably need to overload operator() for each possible pair of types, as apposed to using the templated operator(const T &, const U &). Then you'd need to declare your map like this:

class real_less_than
{
public:
  template<typename T>
  bool operator()(const T &lhs, const T &rhs)
  {
    return boost::apply_visitor(variant_less_than(), lhs, rhs);
  }
};

std::map<boost::variant<T, U, V>, ValueType, real_less_than> myMap;

Edit: for what it's worth, operator<() is defined for boost::variant however it's defined as:

bool operator<(const variant &rhs) const
{
  if(which() == rhs.which())
    // compare contents
  else
    return which() < rhs.which();
}

which I'm assuming is not what you want.

遥远的她 2024-10-12 13:22:12

也许您可以将比较器传递给地图。请参阅http://www.sgi.com/tech/stl/Map.html 有关如何编写比较器的示例。

Perhaps you can pass a comparator to the map. Please see http://www.sgi.com/tech/stl/Map.html for an example on how to write a comparator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文