C ++ fastest insertion

Is there a way to insert data at any position in an array / vector pretty quickly, given that the array / vector is very large?

If I use the vector :: insert, the vector will move all the elements after my element, and it will take a lot of time, if, for example, the vector received 1b elements, and this runs in the middle of the vector, the vector will move 500 m.

Is there an efficient way to do this with a C-style / C ++ array or Vector?

+4
source share
2 answers

( , Big O notation) , O (1) , O (n) - , /.

. - . .

, , o , :

+---------------+
| CPU registers |   ^
+---------------+   |
|   L1 cache    |   |
|      ...      |   |   Less apacity
|   LN cache    |   |   Faster access
+---------------+   |   More expensive hardware
|      RAM      |   |
+---------------+   
|      HDD      |
+---------------+

, . , , .
, - frecuently , , ( , , ).

, , :

  • , , . . , , .
  • , , . . , .

, , , , , .

, ?

, (std::list ) woks:
, :

+---+     +---+             +---+
| 1 | --> | 2 | --> ... --> | N |
+---+     +---+             +---+

, (std::vector ) :

+---+---+-----+---+
| 1 | 2 | ... | N |
+---+---+-----+---+

, , / O (1), " ". , , . , ?. , ( ), , .
, , O (n), - .

, : , / O (n), , , .
, , , , , ( ) (sapce-locality), .

, , , .

std::vector , std::list:

  • -. , std::list , "-" . /, .
  • /. malloc()/free() (.. / ) . , de/allocations O (logn).

: std::list, , / . std::vector , , /, , , .

.

+6

++ - , , - , . , , , . .

+2

All Articles