Is std :: vector the same as array [number]?

Possible duplicate:
Do std :: vector elements guarantee continuity?

Does std :: vector always contain data in sequential memory addresses as an array of [number]?

+3
source share
5 answers

For all types except bool, the standard requires elements to be contiguous in memory:

23.2.4 / 1 ... Elements of a vector are stored adjacent, which means that if v is a vector, where T is some type other than bool, then it obeys the identifier & v [n] == & v, v [0] + n for all 0 <= n <V.SIZE ()

Keep in mind that it std::vector<bool>has special requirements and does not match the bool array.

+6

. :

std::vector<int> arr;

,

&arr[0]

int, () API.

+5

. .

+1

, . , , .

. .

0

When using the standard allocator, you can be sure that the allocated memory is continuous when using std :: vector. In other words, foo [n + 1] is the next element in the sequence after foo [n]. But std :: vector is not an array like

int* blah = new int[100];

not an array. But

int blah[100];

created on the stack is an array. Pointers to allocated memory and arrays just use semantics. They are not equal by standard, so do not confuse them.

0
source

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


All Articles