相关疑难解决方法(0)

什么_can_我用作std :: map键?

延伸.

我有:

struct Coord
{
  int row, col ;

  bool operator<( const Coord& other ) const
  {
    return row < other.row && col < other.col ;
  }
} ;

我想创建一个map<Coord, Node*>,在那里你可以查找Node*Coord.

问题是,它有错误.对the map<Coord, Node*>的查找Coord返回错误的.

我很难搞清楚这是否合适.

维基百科说,map [keys]需要严格的弱序.我做错了吗?有没有办法使它工作,或者地图的键是否可以"严格排序"的简单值?

基本上问题是自定义struct作为我的std :: map的关键字需要什么?

c++ stl key map

4
推荐指数
2
解决办法
877
查看次数

如何通过对地图严格的弱排序对数学向量进行排序?

我尝试编写一个std :: map <Vector3D,double>,其中colinear(并行或反并行)向量应该共享相同的密钥.

作为比较函数,我使用以下函数(在isEqualEnough()中具有1e-9容差),这是我在std :: map使用(数学)向量创建的

struct Vector3DComparator 
{ 
    bool operator() (const Vector3D& lhsIn, const Vector3D& rhsIn) const
    {
        Vector3D lhs = lhsIn.absolute(); // make all members positive
        Vector3D rhs = rhsIn.absolute(); 

        if ((lhs.z < rhs.z)) 
            return true;

        if ((isEqualEnough(lhs.z, rhs.z)) 
            && (lhs.y < rhs.y)) 
            return true;

        if ((isEqualEnough(lhs.z, rhs.z)) 
            && (isEqualEnough(lhs.y, rhs.y))
            && (lhs.x < rhs.x))
            return true;

        return false;
    }
};
Run Code Online (Sandbox Code Playgroud)

当我将一个立方体的法线插入我的地图时,我应该得到3个不同的值(因为我不关心方向)但我得到4:

  • x = 1 y = 0 z = 0
  • x = …

c++ stdmap strict-weak-ordering

3
推荐指数
1
解决办法
82
查看次数

标签 统计

c++ ×2

key ×1

map ×1

stdmap ×1

stl ×1

strict-weak-ordering ×1