相关疑难解决方法(0)

std :: set用户定义的类型,如何确保没有重复

所以我有一个std :: set,它需要保持特定的顺序以及不允许用户定义(由我)类型的重复.现在我可以通过重载我的类型中的'<'运算符来使订单正常工作.但是,该集合没有适当地检测重复,并且说实话,我不完全确定它是如何在内部执行的.我已经重载'=='运算符,但不知何故我不确定这是该集实际使用的是什么?所以问题是当你添加值时,集合如何确定重复?这是相关代码:

用户定义的类型:

//! An element used in the route calculation.
struct RouteElem {
    int shortestToHere; // Shortest distance from the start.
    int heuristic;      // The heuristic estimate to the goal.
    Coordinate position;
    bool operator<( const RouteElem& other ) const
    {
        return (heuristic+shortestToHere) < (other.heuristic+other.shortestToHere);
    }
    bool operator==( const RouteElem& other ) const
    {
        return (position.x == other.position.x && position.y == other.position.y);
    }
};
Run Code Online (Sandbox Code Playgroud)

因此,当元素的位置相等时,元素是等价的,如果元素的组合函数小于另一元素,则元素小于另一元素.排序工作,但该集将接受相同位置的两个元素.

c++ set

59
推荐指数
4
解决办法
6万
查看次数

标签 统计

c++ ×1

set ×1