What does .size () do for a vector in C ++?

In other words, does .size () counts every element of the vector object whenever it calls and returns this value, or the vector object has a member (size_t?) That contains the number of elements currently in the vector, and the value of this element returned using .size ()?

+4
source share
2 answers

What exactly the call std::vector::sizedepends on the specific implementation of the standard library. However, the standard sets several limitations on what it can and cannot do. In particular, calling in sizerequires a constant time, which means that it cannot count elements (which would be linear in the size of the container, but not constant).

, , : ( ) ( ). , beginning. : , end - beginning.

.

+10

std::vector::size , O(1).

+4

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


All Articles