What is the difference between set and hashset in C++ STL?

前端 未结 5 734
时光说笑
时光说笑 2021-02-02 09:31

When should I choose one over the other? Are there any pointers that you would recommend for using the right STL containers?

5条回答
  •  攒了一身酷
    2021-02-02 10:07

    stl::set is implemented as a binary search tree. hashset is implemented as a hash table.

    The main issue here is that many people use stl::set thinking it is a hash table with look-up of O(1), which it isn't, and doesn't have. It really has O(log(n)) for look-ups. Other than that, read about binary trees vs hash tables to get a better idea of the data structures.

提交回复
热议问题