Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

I've been trying to remember this and it's driving me crazy.

Basically, it's like a small array, of let's say size five, and as you add items it starts to fill up. When it's full, and you add a new item, the oldest one (first added) is removed.

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

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

like image 994
Chaos Avatar asked Jun 21 '11 15:06

Chaos


People also ask

Which data structure is defined as a collection of similar data elements?

Arrays are collections of data items that are of the same type, stored together in adjoining memory locations. Each data item is known as an “element.” Arrays are the most basic, fundamental data structure.

What is a data structure that has a fixed size?

Arrays. An array is a structure of fixed-size, which can hold items of the same data type.

Which function plays an element on the stack?

1) push() which adds an element to the top of stack. 2) pop() which removes an element from top of stack.


1 Answers

Boost Circular Buffer

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

From 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
like image 135
Brian O'Kennedy Avatar answered Oct 06 '22 01:10

Brian O'Kennedy