C ++ unordered_map哈希函数无法找到对象

时间:2021-11-09 18:52:11

I have a class Scheduler that contains an unordered_map of two objects.

我有一个类Scheduler,它包含两个对象的unordered_map。

class Scheduler
{
public:
...

private:
    unordered_map<Time, Activity> schedule;

}

I get an error: 'list iterator not dereferenceable' - implying that the object was not found in here:

我收到一个错误:'list iterator not dereferenceable' - 暗示在这里找不到该对象:

void appoint(Time &time, const string activity) 
{
    Time hashTime(time + incrementor);
    schedule.find(hashTime)->second.active = 1; // <-- here
}

Here are my hash tools:

这是我的哈希工具:

namespace std {
    // does get here
    template <>
    struct hash<Time>
    {
        std::size_t operator()(const Time& k) const
        {
            return ((hash<short>()(k.hr)
                ^ (hash<short>()(k.min) << 1)) >> 1)
                ^ (hash<bool>()(k.morning) << 1);
        }
    };
}


struct Time {
    short hr, min;
    bool morning;

    // doesnt get here
    bool operator==(const Time &other) const
    {
        return (hr == other.hr && min == other.min
                && morning == other.morning);
    }
}

Here is evidence for its existence in the unordered_map.

这是在unordered_map中存在的证据。

C ++ unordered_map哈希函数无法找到对象

I've confirmed that it == schedule.end() is true.

我已经确认它== schedule.end()是真的。

        auto it = schedule.find(hashTime);
        if (it == schedule.end()) {
            cout << "ok";
        }


        // this does manage to get found
        schedule.insert(make_pair(Time(9, 30, 1), Activity(0,"")));
        auto it2 = schedule.find(hashTime);
        if (it2 == schedule.end()) {
            cout << "ok";
        }

Somehow my initial construction of the map is incorrect.

不知何故,我对地图的初步构造是不正确的。

Scheduler() 
{
    Time start(12,0,1);             // 12:00am
    Time incrementor;               // 00:00
    Time static_incrementor(0, 15); // 00:15

    // creates an empty schedule
    // there are 96, 15 min intervals from 12:00am to 11:45pm
    for (int i = 0; i < 48; i++) {
        Time newTime(start + incrementor);
        newTime.morning = true;
        cout << newTime.hr << ":" << newTime.min << newTime.morning << endl;

        schedule.insert(make_pair(newTime, Activity(0, "")));
        incrementor += static_incrementor;
    }

    Time start2(12, 0, 0); // 12:00pm
    Time incrementor2;     // 00:00
    for (int i = 0; i < 48; i++) {
        Time newTime(start2 + incrementor2);
        newTime.morning = false;
        cout << newTime.hr << ":" << newTime.min << newTime.morning << endl;

        schedule.insert(make_pair(newTime, Activity(0, "")));
        incrementor2 += static_incrementor;
    }
};

1 个解决方案

#1


0  

I've figured out the issue - but do not completely understand it. Is this because each insertion now ensures a unique construction? Perhaps someone can comment.

我已经找到了问题 - 但不完全理解它。这是因为每个插入现在确保了独特的结构吗?也许有人可以评论。

schedule.insert(make_pair(Time(newTime.hr, newTime.min, newTime.morning), 
                        Activity(0, "")));

#1


0  

I've figured out the issue - but do not completely understand it. Is this because each insertion now ensures a unique construction? Perhaps someone can comment.

我已经找到了问题 - 但不完全理解它。这是因为每个插入现在确保了独特的结构吗?也许有人可以评论。

schedule.insert(make_pair(Time(newTime.hr, newTime.min, newTime.morning), 
                        Activity(0, "")));