C ++中向量的.size()实际上做了什么?

时间:2021-06-05 03:11:38

In other words, does .size() count each element of a vector object whenever it's called and return this value, or does the vector object have a (size_t?) member that holds the number of elements currently in the vector, and the value of this member gets returned by .size()?

换句话说,.size()会在调用向量对象的每个元素时对其进行计数并返回此值,或者向量对象是否具有(size_t?)成员,该成员包含当前在向量中的元素数量,以及值这个成员是由.size()返回的?

2 个解决方案

#1


10  

What exactly a call to std::vector::size does depends on the particular implementation of the standard library. However, the standard places several constraints on what it can and cannot do. In particular, it requires a call to size to execute in constant time, which means it cannot count the elements (that would be linear in the container size, not constant).

究竟对std :: vector :: size的调用取决于标准库的特定实现。但是,该标准对它能做什么和不能做什么都有一些限制。特别是,它需要调用大小以在恒定时间内执行,这意味着它不能计算元素(在容器大小中是线性的,而不是常量)。

A vector's implementation needs one pointer to point to the beginning of the vector, and then other two pieces of information: how large the vector's data currently is (how many elements in the vector), and how large the vector's capacity currently is (how large is the allocated data buffer). The latter two can be implemented either as pointers, or as offsets from the beginning pointer. Either representation can be used to obtain the size of data in constant time: either return the offset directly, or compute it as end - beginning.

向量的实现需要一个指针指向向量的开头,然后是其他两个信息:当前向量数据的大小(向量中有多少元素),以及当前向量的容量有多大(多大)是分配的数据缓冲区)。后两者既可以作为指针实现,也可以作为开始指针的偏移来实现。可以使用任一表示来获取恒定时间内的数据大小:直接返回偏移量,或者将其计算为结束 - 开始。

You can verify the complexity requirement in the standard itself, or in suitable reference documentation.

您可以在标准本身或适当的参考文档中验证复杂性要求。

#2


4  

The implementation of std::vector::size is implementation dependant even if it's required to have a complexity in O(1).

std :: vector :: size的实现依赖于实现,即使它需要在O(1)中具有复杂性。

#1


10  

What exactly a call to std::vector::size does depends on the particular implementation of the standard library. However, the standard places several constraints on what it can and cannot do. In particular, it requires a call to size to execute in constant time, which means it cannot count the elements (that would be linear in the container size, not constant).

究竟对std :: vector :: size的调用取决于标准库的特定实现。但是,该标准对它能做什么和不能做什么都有一些限制。特别是,它需要调用大小以在恒定时间内执行,这意味着它不能计算元素(在容器大小中是线性的,而不是常量)。

A vector's implementation needs one pointer to point to the beginning of the vector, and then other two pieces of information: how large the vector's data currently is (how many elements in the vector), and how large the vector's capacity currently is (how large is the allocated data buffer). The latter two can be implemented either as pointers, or as offsets from the beginning pointer. Either representation can be used to obtain the size of data in constant time: either return the offset directly, or compute it as end - beginning.

向量的实现需要一个指针指向向量的开头,然后是其他两个信息:当前向量数据的大小(向量中有多少元素),以及当前向量的容量有多大(多大)是分配的数据缓冲区)。后两者既可以作为指针实现,也可以作为开始指针的偏移来实现。可以使用任一表示来获取恒定时间内的数据大小:直接返回偏移量,或者将其计算为结束 - 开始。

You can verify the complexity requirement in the standard itself, or in suitable reference documentation.

您可以在标准本身或适当的参考文档中验证复杂性要求。

#2


4  

The implementation of std::vector::size is implementation dependant even if it's required to have a complexity in O(1).

std :: vector :: size的实现依赖于实现,即使它需要在O(1)中具有复杂性。