← Back to STL Containers
Question 546
Fixed-Size Ring Buffer
Fixed-Size Ring Buffer
Build a classRingBufferthat stores its values in anarray<int, 4>(capacity 4) and never allocates anything else. ARingBufferis created with no constructor arguments (the test writesRingBuffer rb;) and starts out empty. Implement: 1.void add(int value): storesvalue. If fewer than 4 values are stored it is simply appended; if the buffer is full it replaces the oldest value. 2.int count() const: returns how many values are currently stored (0 to 4). 3.void print() const: prints the stored values from oldest to newest, separated by single spaces, followed by a newline. When the buffer is empty it prints just a newline (an empty line). The test prints the result ofcount()on its own line and then callsprint(), for example (see Expected Output). One test also callscount()andprint()on a brand-new empty buffer first, which must produce the line0followed by an empty line. Do NOT write a main function.
Expected Output:
3 1 2 3 0 4 3 4 5 6
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL Containersstd::array
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.