How do I code a simple integer circular buffer in C/C++?

后端 未结 4 1275
长发绾君心
长发绾君心 2021-02-05 08:54

I see a lot of templates and complicated data structures for implementing a circular buffer.

How do I code a simple integer circular buffer for 5 numbers?

I\

4条回答
  •  走了就别回头了
    2021-02-05 09:38

    Have an array, buffer, of 5 integers. Have an index ind to the next element. When you add, do

    buffer[ind] = value;
    ind = (ind + 1) % 5;
    

提交回复
热议问题