Set and Vecotr-Can anyone tell me which is faster?

When we enter the elements in the set it automatically gets sorted.
But in a vector it takes another process to sort.Of course that depends on whether you want to sort it or not.But still i want to know generally which is faster?


Well, while a set maintains a specfic order at all times, I guess it all depends on what you want to do with the cotainers you’re using, keeping in mind that they are different.

Also, you need to keep in mind that a set container doesn’t allow duplicate elements, while a vector does.

If you plan to keep unique items somehow and you plan on performing many insert/remove operations, than maybe a set is not such bad idea, but, in general, a vector is more widely used as it also has more applications than set.

You also need to be aware that the set you’re thinking about is the set defined by the C++ specification, which states that the set is ordered, but, in Mathematics terms, a set has no order whatsoever.

You can read more about the differences between set and vector, here.



1 Like