How does C++ STL unordered_map resolve collisions?

后端 未结 2 809
野性不改
野性不改 2020-11-27 15:02

How does C++ STL unordered_map resolve collisions?

Looking at the http://www.cplusplus.com/reference/unordered_map/unordered_map/, it says \"Unique keys No two eleme

2条回答
  •  -上瘾入骨i
    2020-11-27 15:35

    I found this answer looking for how to detect when my types are colliding, so I will post this in case that is the intent of the question.:

    I believe there's some misconception about "Unique keys No two elements in the container can have equivalent keys."

    look at the code below

    //pseudocode
    std::unordered_map hashmap;
    hashmap[5] = 'a';
    hashmap[5] = 'b'; //replace 'a' with 'b', there is no collision being handled.
    

    I think the Jerry's answer is referring to the internal system that it uses to shrink keys to appropriate array indices.

    If you want collisions to be handled for your types (with buckets), you need std::unordered_multimap and will have to iterate over

    Hopefully this code can be read without the context I generated it with. it basically checks to see if any element in the bucket associated with the hash is the element I'm looking for.

    //sp is std::shared_ptr
    //memo is std::unordered_multimap< int, sp >
    
    //there's probably multiple issues with this code in terms of good design (like using int keys rather than unsigned)
    
    bool AStar_Incremental::hasNodeBeenVisited(sp node)
    {
        using UMIter = std::unordered_multimap >::iterator;
    
        bool bAlreadyVisited = false;
    
        //get all values for key in O(1*)
        int hash = WorldGrid::hashGrid(node->location);
        std::pair start_end = memo.equal_range(hash); //bucket range
        UMIter start = start_end.first;
        UMIter end = start_end.second;
    
        //hopefully this is implemented to be O(m) where m is the bucket size.
        for(UMIter bucketIter = start; bucketIter != end; ++bucketIter)
        {
            sp previousNode = bucketIter->second;
            sf::Vector2i& previousVisit = previousNode->location;
            if (previousVisit == node->location)
            {
                bAlreadyVisited = true;
                break;
            }
        }
    
        return bAlreadyVisited;
    }
    

提交回复
热议问题