简体   繁体   English

圆形数组的矢量实现

[英]Vector implementation with circular array

I am confused. 我很困惑。 I was reading about the implementation of vectors using arrays.While we were on the simple dynamic array implementation everything was okay. 我在读关于使用数组实现向量的文章,而在进行简单的动态数组实现时,一切都很好。

It was mentioned that you can implement the vector by using an array in a circular fashion and that that will make the addition and removal of elements (for first and last) run in constant time. 提到可以通过以循环方式使用数组来实现向量,这将使元素的添加和删除(对于第一个和最后一个)在恒定时间内运行。 Isn't that what a linked list is supposed to do? 这不是链接列表应该做什么吗?

I want to know how it works, but I couldn't really find an implementation or a proper explanation. 我想知道它是如何工作的,但是我找不到真正的实现或正确的解释。 Any information on what is the general idea and how to implement it is welcomed. 欢迎提供有关什么是总体思想以及如何实现的任何信息。

Edit: My guess is that new data should be written on the 'oldest' data and that the array has fixed size and that you must have a variable that stores the last used position. 编辑:我的猜测是新数据应该写在“最旧的”数据上,并且数组具有固定的大小,并且您必须具有存储最后使用位置的变量。

You are talking about circular buffer 您正在谈论循环缓冲区

Unfortunately you cannot implement vector using it as you will not be able to insert in the middle or delete from the middle etc. 不幸的是,您不能使用它实现vector,因为您将无法在中间插入或从中间删除等。

Since C++03 vector guarantees contiguous storage, that is to say for each 0 <= n < vec.size() : 由于C ++ 03 vector保证了连续存储,也就是说,对于每个0 <= n < vec.size()

&vec[n] == &vec[0] + n

This means that you cannot use a circular buffer to implement vector . 这意味着您不能使用循环缓冲区来实现vector At the point you "wrap around" from the end of the buffer to the start, you violate this restriction. 从缓冲区的末尾到起点“回绕”时,您违反了此限制。

In case you think that operator[] could return some proxy with an overloaded operator& that satisfies this requirement -- no, it can't, it's required to return T& . 如果您认为operator[]可以返回满足此要求的带有重载的operator&某些代理-不,不能,它需要返回T&

Aside from this requirement I don't think there's anything in the standard to prevent implementing vector using a circular buffer. 除了此要求外,我认为标准中没有任何东西可以阻止使用循环缓冲区实现vector In the case where the buffer is full you'd do the same thing vector actually does -- reallocate to a larger buffer. 在缓冲区已满的情况下,您将执行vector实际所做的相同操作-重新分配给更大的缓冲区。 The only difference in behaviour would be what happens when you erase or insert at the start. 行为上的唯一区别将是在开始时擦除或插入时会发生什么。 With a circular buffer all the other elements would stay still assuming sufficient space, whereas normally they all move. 使用圆形缓冲区时,所有其他元素将在保持足够空间的情况下保持静止,而通常它们都会移动。 But the standard doesn't explicitly require them to move, and since iterators and references to all elements are invalidated when you insert or erase at the start, a program cannot legitimately depend on them moving in any particular way. 但是该标准并未明确要求它们移动,并且由于在开始插入或擦除操作时对所有元素的迭代器和引用无效,因此程序无法合法地依赖它们以任何特定方式移动。

It's true that std::list can insert and remove at both the start and the end in constant time. 的确, std::list可以在开始和结束时以固定时间插入和删除。 So can std::deque . 所以std::deque也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM