Random element from unordered_set in O(1)

后端 未结 4 1350
旧时难觅i
旧时难觅i 2020-12-10 12:28

I\'ve seen people mention that a random element can be grabbed from an unordered_set in O(1) time. I attempted to do so with this:

std::unordered_set

        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 13:29

    I believe you have misinterpreted the meaning of "random access", as it was used in those cases you're referring to.

    "Random access" doesn't have anything to do with randomness. It means to access an element "at random", i.e. access any element anywhere in the container. Accessing an element directly, such as with std::vector::operator[] is random access, but iterating over a container is not.

    Compare this to RAM, which is short for "Random Access Memory".

提交回复
热议问题