Why is the difficulty of inserting or deleting elements at the end or beginning of std :: deque constant O (1)?

According to the C ++ standard, std :: deque is something like

std::vector<std::array<T, M> *>

If so, how is it possible that inserting or deleting elements at the end or at the beginning is a constant O (1)? If the bandwidth of the vector is exceeded, and we insert something at the end or at the beginning, there is no guarantee that the whole vector is not redistributed, so we have 0 (N / M), which is actually 0 (N), isn't it ? (N - deck size).

+4
source share
2 answers

, - , , , 0 (N/M), 0 (N), isn ' ? (N - ).

, / , .

:

[container.requirements.general]
-2- .

, N/M , O (1). , . deque , , , , deques .

+3

() .

, , 4 , 8 :

[ nil, nil, nil, N1*, N2*, N3*, nil, nil ]

N1 N3 :

N1: [ nil, nil, nil, 1 ]
N2: [ 2, 3, 4, 5 ]
N3: [ 6, 7, nil, nil ]

push_front deque, N1 , . , push_front , :

[ N1*, N2*, N3*, N4*, N5*, N6*, nil, nil ]
  |                         `---------------------------------------\
  `-----------------------------------v                             v
[ nil, nil, nil, nil, nil, nil, nil, N0*, N1*, N2*, N3*, N4*, N5*, N6*, nil, nil ]

O (1) deque::push_front, deque::push_back , vector::push_back O (1) .

+2

Source: https://habr.com/ru/post/1686797/


All Articles