Home > front end >  std::unordered_multiset exception iterating bucket
std::unordered_multiset exception iterating bucket

Time:04-08

My test case is the one shown below:

std::size_t t(const int &i) { return i | 0b01010101010101010101010101010101; }

int main()
{
    std::unordered_multiset<int, decltype(&t)> um(100, t);
    um.insert(9872934);
    um.insert(9024582);
    um.insert(2589429);
    um.insert(2254009);
    um.insert(3254082);
    um.insert(3945820);
    um.insert(8347893);

    auto intf = t(9872934);
    for (auto cb = um.begin(intf), end = um.end(intf); cb != end;   cb)
    {
        std::cout << *cb;
    }
};

Debugging with Microsoft Visual Studio Community 2022 v17.1.2 an exception is thrown constructing the iterator; first I thougth that the hash function (t) could be the one to blame so I've tried this:

std::unordered_multiset<int> um; // no custom hash, just multiset of integers...
um.insert(9872934);
um.insert(9024582);
um.insert(2589429);
um.insert(2254009);
um.insert(3254082);
um.insert(3945820);
um.insert(8347893);

auto intf = t(9872934);
for (auto cb = um.begin(intf), end = um.end(intf); cb != end;   cb)
{
    std::cout << *cb;
}

But it behaves the same way, even in online compilers (check it out). What I'm missing? How should I make this work?

CodePudding user response:

The argument to the begin(bucket) function is the bucket number not the key.

You need use bucket to get the bucket number that corresponds to the key

auto intf = um.bucket(t(9872934)); <<<====
for (auto cb = um.begin(intf), end = um.end(intf); cb != end;   cb)
{
    std::cout << *cb;
}
  • Related