【问题标题】:Sort data in one way but retrieve in another way以一种方式对数据进行排序,但以另一种方式检索
【发布时间】:2020-04-22 11:41:29
【问题描述】:

我有一个问题,我需要以排序方式存储浮点数。但是在检索时我想使用不同的机制。通过重载< operator 将当前存储在std::set 中

bool operator < (const SpanStruct_Y &b) const
{
    CommonTypes::Point p1 = mobIdentifier.startPoint, p2 = mobIdentifier.startPoint;
    CommonTypes::Point p3 = b.mobIdentifier.startPoint, p4 = b.mobIdentifier.startPoint;
    editPoint(p1, span, 270.0f + spanOrientation);
    editPoint(p2, span, 90.0f + spanOrientation);
    editPoint(p3, b.span, 270.0f + b.spanOrientation);
    editPoint(p4, b.span, 90.0f + b.spanOrientation);
    return cmpPoint_Y(cmpPointR_Y(p1,p2),cmpPointR_Y(p3,p4));
}

但是在检索数据时,我想在标识符中使用不同的值。 [mobileId and 2 index values]

struct uniqMobIdentifier
{
    CommonTypes::Point startPoint;
    uint16_t mobileId;
    uint16_t indexXInSpan;
    uint16_t indexYInSpan;

    uniqMobIdentifier()
    {
        startPoint = CommonTypes::Point();
        mobileId = DUMMY_ID;
        indexXInSpan = DUMMY_ID;
        indexYInSpan = DUMMY_ID;
    }

    uniqMobIdentifier(CommonTypes::Point p, uint16_t mobId, uint16_t x, uint16_t y)
    {
        startPoint = p;
        mobileId = mobId;
        indexXInSpan = x;
        indexYInSpan = y;
    }

    uniqMobIdentifier& operator =(const uniqMobIdentifier& a)
    {
        startPoint = a.startPoint;
        mobileId = a.mobileId;
        indexXInSpan = a.indexXInSpan;
        indexYInSpan = a.indexYInSpan;
        return *this;
    }

    bool operator==(const uniqMobIdentifier& a) const
    {
        return (((std::abs(startPoint.m_xCoordinate - a.startPoint.m_xCoordinate) < POINT_DISTANCE_TOLERANCE)
                || (std::abs(startPoint.m_yCoordinate - a.startPoint.m_yCoordinate) < POINT_DISTANCE_TOLERANCE))
                && mobileId == a.mobileId && indexXInSpan == a.indexXInSpan && indexYInSpan == indexYInSpan);
    }


    bool operator!=(const uniqMobIdentifier& a) const
    {
        return (/*((std::abs(startPoint.m_xCoordinate - a.startPoint.m_xCoordinate) > POINT_DISTANCE_TOLERANCE)
                || (std::abs(startPoint.m_yCoordinate - a.startPoint.m_yCoordinate) > POINT_DISTANCE_TOLERANCE))
                && */
                !(mobileId == a.mobileId) && !(indexXInSpan == a.indexXInSpan) && !(indexYInSpan == indexYInSpan));
    }


};

有可能吗?有更好的方法吗?

【问题讨论】:

  • 我没有完全掌握你的模型,但你可能对Boost.MultiIndex感兴趣。
  • @Quentin:我正在根据 startPoint 值对集合中的数据进行排序,但想根据索引和唯一 ID 检索它。 [ mobIdentifier 是包含所有 4 个值的 uniqMobIdentifier 类型] 如果我们没有 Boost 选项,STL 是否不可能?
  • 一切皆有可能,但没有现成的标准容器可以做到这一点,你必须自己编写代码。

标签: algorithm c++11 data-structures


【解决方案1】:

您要么必须对检索进行线性搜索,要么拥有一个以多种方式存储数据的容器,然后允许您通过其中任何一种方式进行检索。 (这可能会默认以特定顺序检索它们。)

如果您的数据操作会随着时间的推移变得越来越复杂,您可能需要考虑嵌入 SQLite。

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2021-03-16
    • 1970-01-01
    相关资源
    最近更新 更多