resort on a std::vector vs std::insert

Posted by Abruzzo Forte e Gentile on Stack Overflow See other posts from Stack Overflow or by Abruzzo Forte e Gentile
Published on 2012-06-02T10:36:57Z Indexed on 2012/06/02 10:41 UTC
Read the original article Hit count: 175

Filed under:
|
|
|
|

I have a sorted std::vector of relative small size ( from 5 to 20 elements ). I used std::vector since the data is continuous so I have speed because of cache. On a specific point I need to remove an element from this vector.

I have now a doubt: which is the fastest way to remove this value between the 2 options below?

  1. setting that element to 0 and call sort to reorder: this has complexity but elements are on the same cache line.
  2. call erase that will copy ( or memcpy who knows?? ) all elements after it of 1 place ( I need to investigate the behind scense of erase ).

Do you know which one is faster?

I think that the same approach could be thought about inserting a new element without hitting the max capacity of the vector.

Regards

AFG

© Stack Overflow or respective owner

Related posts about c++

Related posts about Performance