ring buffer 环形队列 C++实现

news/2024/7/6 6:42:36

RingBuff.h实现:

#include <iostream>

using namespace std;

template <class T>
class RingBuff
{
private:
    unsigned int m_size;
    int m_in;
    int m_out;
    T* m_data;

public:
    RingBuff (unsigned int size)
            : m_size(size)
            , m_in(0)
            , m_out(0)
    {
        m_data = new T[m_size];
    }

    ~RingBuff() {
        delete[] m_data;
    }

    bool isEmpty() {
        return m_in == m_out;
    }

    bool isFull() {
        return m_out == (m_in + 1) % m_size;
    }

    void push(T ele) {
        if (isFull()) {
            return;
        }

        m_data[m_in] = ele;
        m_in = (m_in + 1) % m_size;
    }

    T pop() {
        if (isEmpty()) {
            return NULL;
        }

        T tmp = m_data[m_out];
        m_out = (m_out + 1) % m_size;
        return tmp;
    }
};

main函数实现:

#include "RingBuff.h"

using namespace std;

int main()
{
    cout << "Hello CMake。" << endl;

    RingBuff<char> ringBuff(11);
    char a = '0';
    for (int i = 0; i < 1024; i++) {
        //arr[i] = a++;
        ringBuff.push(a++);
    }

    while (!ringBuff.isEmpty()) {
        printf("%c ", ringBuff.pop());
    }

    RingBuff<int> ringBuffInt(1024);
    for (int i = 0; i < 1024; i++) {
        //arr[i] = a++;
        ringBuffInt.push(i);
    }

    while (!ringBuffInt.isEmpty()) {
        printf("%d ", ringBuffInt.pop());
    }
    int b = ringBuffInt.pop();
    printf("%d\n", b);

    getchar();
    return 0;
}

http://www.niftyadmin.cn/n/3656212.html

相关文章

Microsoft Updater Application Block 1.2.1 核心设计(core design) [翻译]

Microsoft Updater Application Block 核心设计(core design) 译者&#xff1a;Tony Qu Updater Application Block由一个核心应用程序升级器&#xff08;集合[assembly]名称为ApplicationUpdater&#xff09;和许多额外的assembly接口和工具组成。 Updater Application Bl…

pcm格式封装wav

PCM PCM编码是直接存储声波采样被量化后所产生的非压缩数据,故被视为单纯的无损耗编码格式,其优点是可获得高质量的音频信号。 PCM是模拟音频信号经模数转换&#xff08;A/D变换&#xff09;直接形成的二进制序列&#xff0c;该文件没有附加的文件头和文件结束标志。 要将这样…

Microsoft Updater Application Block 1.2.2 ApplicationUpdater assembly设计 [翻译]

Microsoft Updater Application Block ApplicationUpdater assembly设计 译者&#xff1a;Tony Qu ApplicationUpdater集合提供了Updater Application Block的核心功能。 ApplicationUpdater集合类 ApplicationUpdater集合包含许多类&#xff0c;可以分为以下几类&#xff1…

音视频 协议 RTP/RTCP、TCP、UDP、RTMP、RTSP

https://www.cnblogs.com/xiaohexiansheng/articles/8288924.html

控件制作之design-time attribute

为了使自定义属性在属性编辑器中能够像默认属性一样显示出来&#xff0c;就必须使用设计时特性(design-time attribute)。 BindableAttribute 该特性可用于绑定数据&#xff0c;但必须配合代码一起使用。 BrowsableAttribute 该特性表示是否应该在属性编辑器中显示特定…

Microsoft Updater Application Block v1.0 介绍 [翻译]

Microsoft Updater Application Block v1.0 介绍 译者&#xff1a;Tony Qu 在过去&#xff0c;由于必须注册组件和安装应用程序文件&#xff0c;致使管理桌面应用程序的部署和升级变得十分困难。.Net框架支持通过简单地拷背文件到适当的目录完成安装&#xff0c;这使得在整…

av_buffersrc_add_frame分析

ffmpeg——av_buffersrc_add_frame分析一、函数功能二、使用场景三、源码分析一、函数功能 向滤镜源中添加一个frame&#xff0c;源码摘录如下&#xff1a; /*** Add a frame to the buffer source.** param ctx an instance of the buffersrc filter* param frame frame t…

[翻译]RSS 2.0 规范

RSS 2.0 规范 译者&#xff1a;Tony Qu (来自BluePrint翻译团队)原文地址&#xff1a;http://blogs.law.harvard.edu/tech/rss什么是RSS&#xff1f; RSS是一种网页内容联合格式&#xff08;web content sydication format&#xff09;。 它的名字是Really Simple Syndication的…