当前位置:   article > 正文

C++ 自定义循环数组(定长队列)_c++ 面试题 定长数组实现循环队列

c++ 面试题 定长数组实现循环队列

应用环境:假如有一个长度为10的窗口队列,数据依次增加,当不满10时,不计算,当满足10时,开始计算,如计划10个数的平均值,当11个数时,从第2个数开始直到第11个数的这10个数计算平均数,当12个数时,从第3个数到第12个数这10个数计算平均值; 

应用场景:均线MA之静态MA或动态MA

代码如下:

#ifndef FIXEDVECTOR

#define FIXEDVECTOR

#define BaseT int

typedef unsigned char uchar;

typedef unsigned int uint;

typedef unsigned long ulong;

template

class FixedVector

{

private: //disCopy!

        FixedVector() = default;

        FixedVector(const FixedVector& other) = delete; //c++11;

        FixedVector &operator=(const FixedVector& other) = delete;

public:

        FixedVector(uint size) : m_size(size){

                m_array = 0;

                if (m_size >= 2){ //支持>=2的容量尺寸的队列;

                        m_array = new T[m_size];

                        clear();

                }

        }

~FixedVector(){ //析构;

        if(m_array){

                delete[] m_array;

        }

}

bool isFull() const{ //队列是否满员;

        if (m_size >= 2) //支持>=2的容量尺寸的队列;

                return m_len == m_size;

        return false;

}

bool isEmpty() const{ //队列是否为空;

        return m_len == 0;

}

uint size() const{//队列容量;

        return m_size;

}

uint len() const{ //队列真实长度;

        return m_len;

}

double sum() const{ //总和;

        double sv = 0.0;

        for (uint i = 0; i < m_len; i++){

                sv += m_array[i];

        }

        return sv;

}

double avg() const{ //均值;

        if (isFull())

                return sum() / (double)m_len;

        return 0.0;

}

void clear(){ //清除所有数据;

        m_len = 0;

        m_front = m_tail = 0;

        T zero = (T)0;

        for (uint i = 0; i < m_size; i++){

                m_array[i] = zero;

        }

}

T front() const{//队列首值;

        return m_array[m_front];

}

T tail() const{ //队列尾值;

        return m_array[m_tail];

}

T & at(uint index){ //队列某个值;

        //if (index

        int realIndex = getRealIndex(index);

        return m_array[realIndex];

        //}

}

T & operator[](const uint index){//队列某个值;

        int realIndex = getRealIndex(index);

        return m_array[realIndex];

}

T push(T val){ //在队尾压入数值,弹出首值;

        T oldFrontVal = (T)0; //原先的值;

        if (m_size < 2){

                return oldFrontVal;

        }

        if (isFull() == false){

                m_len++;

                if (m_len == 1){

                        m_array[m_front] = val;

                        m_array[m_tail] = val;

                }

                else{

                        m_array[++m_tail] = val;

                }

        }

        else{

                oldFrontVal = m_array[m_front];

                if (m_tail == m_size-1){

                        m_array[m_tail = 0] = val;

                        m_front++;

                }

                else if (m_front == m_size-1){

                        m_array[++m_tail] = val;

                        m_front = 0;

                }

                else{

                        m_array[++m_tail] = val;

                        m_front++;

                }

        }

        return oldFrontVal;

}

private:

int getRealIndex(uint index){

        if (index >= m_size){

                return -1;

        }

        int realIndex = m_front + index;

        realIndex = realIndex % m_size;

        return realIndex;

}

private:

        T* m_array;

        const uint m_size; //队列的容量;

 private:

        uint m_front;

        uint m_tail;

        uint m_len; //队列的真实长度;

};

//显式特化模板支持的基本类型;

template class FixedVector;

template class FixedVector;

template class FixedVector;

template class FixedVector;

template class FixedVector;

template class FixedVector;

template class FixedVector;

template class FixedVector;

//-

typedef FixedVector FVector_char;

typedef FixedVector FVector_uchar;

typedef FixedVector FVector_int;

typedef FixedVector FVector_uint;

typedef FixedVector FVector_long;

typedef FixedVector FVector_ulong;

typedef FixedVector FVector_float;

typedef FixedVector FVector_double;

#endif//CIRCULARARRAY

使用如下:【已经跑过项目,无溢出,放心使用】

FixedVector ma10(10);

ma10.push(5.3);

ma10.push(5.8);

...

float popedValue = ma10.push(8.8); //压入后弹出队列首值;

if(ma10.isFull()){

        float static_ma = ma10.avg(); //静态ma;

        float active_ma = (ma10.sum()-ma10.front()+currentPrice) / (float)ma10.size(); //动态ma;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/756441
推荐阅读
相关标签
  

闽ICP备14008679号