如何使用std :: map将bool映射到3d点结构? [英] How to map a bool to a 3d point struct with std::map?

查看:112
本文介绍了如何使用std :: map将bool映射到3d点结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用以下结构:

struct point 
{
    int x;
    int y;
    int z;
};

作为 std :: map的键< point,bool> code>?如何定义运算符<两个点?

推荐答案

容器如 std :: map 要求您的订单是一个严格弱订单,所以你在设计时要非常小心。

Standard library containers like std::map require that your ordering is a "Strict Weak Ordering", so you have to be very careful when designing one.

一个3元组的典型方法如下:

The typical approach for a 3-ary tuple looks like this:

bool operator<(const point& other) const
{
   if (x != other.x)
       return (x < other.x);

   if (y != other.y)
       return (y < other.y);

   return (z < other.z);
}

就像一个比较器,只是 x ,但有区别,如果两个 x s是相同的,你会通过比较 y s。如果它们是相同的,那么类似地,你可以通过 z 比较。

It's like a comparator for just x, but with the difference that if the two xs are the same, you fall through to compare ys. If they are the same, then similarly you fall through to the z comparison.

这篇关于如何使用std :: map将bool映射到3d点结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆