What is the difference between std::set and std::vector?

前端 未结 5 1752
南笙
南笙 2020-12-02 05:04

I am learning STL now. I read about set container. I have question when you want to use set? After reading description of set it looks like it is u

5条回答
  •  盖世英雄少女心
    2020-12-02 05:43

    it is faster to search an item against a set than a vector (O(log(n)) vs O(n)). To search an item against a vector, you need to iterate all items in the vector, but the set use red-black tree to optimize the search, only a few item will be looked to find a match.

    The set is ordered, it means you can only iterate it from smallest one to biggest one by order, or the reversed order.

    But the vector is unordered, you can travel it by the insert order.

提交回复
热议问题