What data structure discards the oldest item when adding a new one?

I tried to remember this, and it drove me crazy.

Basically, it looks like a small array, let’s say, the size is five, and as you add elements, it begins to fill up. When it is full, and you add a new element, the oldest (first added) will be deleted.

You can access the values ​​through variable [0], variable [1], etc., where variable [0] is the oldest value and variable 4 is the newest.

Any ideas on what this is called? Is this a standard C ++ type, or did I just see it somewhere as a custom class?

+6
source share
3 answers

Circular buffer increase

http://www.boost.org/doc/libs/1_46_1/libs/circular_buffer/doc/circular_buffer.html

In the Boost docs:

// Create a circular buffer with a capacity for 3 integers. boost::circular_buffer<int> cb(3); // Insert some elements into the buffer. cb.push_back(1); cb.push_back(2); cb.push_back(3); int a = cb[0]; // a == 1 int b = cb[1]; // b == 2 int c = cb[2]; // c == 3 // The buffer is full now, pushing subsequent // elements will overwrite the front-most elements. cb.push_back(4); // Overwrite 1 with 4. cb.push_back(5); // Overwrite 2 with 5. // The buffer now contains 3, 4 and 5. a = cb[0]; // a == 3 b = cb[1]; // b == 4 c = cb[2]; // c == 5 
+13
source

You can easily implement this with a queue , although it does not specify the exact behavior of the maximum number of elements that you describe here.

0
source

He called FIFO buffer . Often.

0
source

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


All Articles