How the stl vector provides random access

Last night I used std :: vector for my work, and this question popped up in my head: how does a vector give random access?

I tried to learn the code, but was unsuccessful. Can someone give some pointers?

Thanks Arun

+3
source share
4 answers

A vector uses adjacent memory at the bottom, so it gives random access just like an array: it knows the starting address and size of the elements and does some math pointer.

+18
source

Of course, here are a few pointers:

int *x, *y;

, vector . ([]), .

+12

[*]... , O (1) ( ) .

( ) (. ). , , 2, n- , (2 * n) (4 * n)-th ...

, , N O (N). , , - .

[*] , . -?

+1

, 1,5 , , .

+1

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


All Articles