As a good rule of thumb, stricter guarantees are offered, the worse the performance you get.
The insert in std::set ensures that the sequence is sorted after each insert.
Insertion into std::vector and calling std::sort once after all insertions are completed ensures that the sequence is sorted as soon as all manipulations on vector are completed. This does not require the vector to be sorted during all intermediate inserts.
A std::vector also has better spatial locality and requires less memory allocations. Therefore, I would suggest that the vector approach will be faster, but if performance is important to you, then this is important enough for measurement.
If you do not want to measure what is faster in your case for your data sets with your code in then you donβt care what is faster.
source share