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

前端 未结 5 1753
南笙
南笙 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:35

    form cpluplus.com set:

    Sets are containers that store unique elements following a specific order.

    so the set is ordered AND item are uniquely represented

    while vect:

    Vectors are sequence containers representing arrays that can change in size.

    so vector is in the order you fill it AND can hold multiple identical items

    prefer set:

    • if you wish to filter multiple identical values
    • if you wish to parse items in a specified order (doing this in vector requires to specifically sort vector).

    prefer vector:

    • if you want to keep identical values
    • if you wish to parse items in same order as you pushed them (assuming you don't process the vector order)

提交回复
热议问题