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

前端 未结 5 856
时光说笑
时光说笑 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 09:54

    A hash_set would be implemented by a hash table, which has mostly O(1) operations, whereas a set is implemented by a tree of some sort (AVL, red black, etc.) which have O(log n) operations, but are in sorted order.

    Edit: I had written that trees are O(n). That's completely wrong.

提交回复
热议问题