簡體   English   中英

嘗試使用結構在 C++ 中創建一個循環緩沖區

[英]trying to create a circular buffer in C++ using struct

我想使用 C++ 中的結構創建一個循環緩沖區。 基本實現。 我在將元素插入緩沖區時遇到問題。 這是我寫的代碼。

這是我的結構定義:

#define LENGTH_BUFFER 100
typedef struct {
  int buffer[LENGTH_BUFFER];
  int head;
  int tail;
} ringbuffer;


ringbuffer test;

將指針設置為 0 的初始化:

void RingBuffer_Init(ringbuffer temp)
{
  temp.head = 0;
  temp.tail = 0;
}

推送數據的功能:

void RingBuffer_Push(int data,ringbuffer temp)
{
  temp.buffer[temp.head] = data;
  temp.head++;
}

這是我調用函數來初始化和推送數據的地方

void main()
{
  RingBuffer_Init(test);
  RingBuffer_Push(25,test);

  cout << test.buffer[0]<<endl;
}

我似乎無法將數據推送到我的緩沖區。 請幫幫我。 它返回全零。

假設head是您放置新值的索引,當您推送值temp.head++; 必須替換為temp.head = (temp.head + 1) % BUFFER_LENGTH; 管理緩沖區的長度。 當緩沖區已滿時,您還必須移動尾部

我不明白你為什么推 1 個值然后寫 5 個值

你說你是用 C++ 寫的,那你為什么用 C 寫? 為什么在 ringbuffer 上沒有構造函數和 push/pop/top 操作?

{編輯添加}

正如您要求在 C++ 中使用一種非常簡單的方法

#include <iostream>
using namespace std;

#define LENGTH_BUFFER 4

// in c++ we use a struct when all members are public (by default all is public in a struct),
// but this is dangerous because all can be modified from outside without any protection,
// so I prefer to use a class with public/private parts

class RingBuffer {
  public:
    // The constructor, it replaces RingBuffer_Init,
    // The big advantage is you cannot miss to call the constructor
    RingBuffer();

    // it is not needed to have a destructor, the implicit destructor is ok

    // because it is an operation you do not have to give the ringbuffer in parameter
    void push(int data);

    // I had the print operation, it print following the order of insertion
    // to print doesn't change the instance, so I can say the operaiotn is 'const'
    void print() const;

  private:
    int buffer[LENGTH_BUFFER];
    int head; // index of the future value
    int tail; // index of the older inserted value, except if empty / head == index
};

// The buffer is initialized empty, head == tail
// I can initialize the indexes by any other valid
// value, this is not relevant
RingBuffer::RingBuffer() : head(0), tail(0) {
}

// push a new value, 
void RingBuffer::push(int data)
{
  buffer[head] = data;
  head = (head + 1) % LENGTH_BUFFER;

  if (head == tail)
    tail = (tail + 1) % LENGTH_BUFFER;
}

// print the content and indexes
void RingBuffer::print() const {
  for (int p = tail; p != head; p = (p + 1)  % LENGTH_BUFFER)
    cout << buffer[p] << ' ';
  cout << " (head=" << head << ", tail = " << tail << ')'  << endl;
}

int main()
{
  RingBuffer test;

  test.print();

  test.push(1);
  test.print();

  test.push(2);
  test.print();

  test.push(3);
  test.print();

  test.push(4);
  test.print();

  test.push(5);
  test.push(6);
  test.print();

  return 0;
}

我減少了緩沖區的大小以使用很少的值進行旋轉。

執行產生:

 (head=0, tail = 0)
1  (head=1, tail = 0)
1 2  (head=2, tail = 0)
1 2 3  (head=3, tail = 0)
2 3 4  (head=0, tail = 1)
4 5 6  (head=2, tail = 3)

正如您在該實現中看到的那樣,丟失了一個條目,它的大小為 4,但僅管理 3 個值。 我讓你可以改變它,添加 top()、back()、pop() 等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM