Блог пользователя Hacker-M

Автор Hacker-M, 3 года назад, По-английски

Hi, Which one of the following ways is better to store the non-duplicate numbers?

1- Using sort and unique in std :: vector in C++ like this :

sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());

2- Using std :: set in C++

I know set's Order is around $$$O(nlogn)$$$ with a little bad coefficients But I don't know what's the order of using vector and unique, So If it is possible tell me which one is faster and better, Thanks :).

  • Проголосовать: нравится
  • -13
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I wrote three programs that sort an array and remove equal elements.

std::sort Time: 4.80 s

std::stable_sort Time: 5.20 s

std::set Time: 15.43 s

P.S. Each of these programs uses a random number generator with seed 238. I tried running programs with other seeds and the results were almost the same.