C++ 循环出列实现错误

C++ 循环出列实现错误,c++,data-structures,C++,Data Structures,基本上是一个动态数组,当数组已满时,它会进行圆形旋转。您可以访问每个元素,并可以更改其值,但只能从两端插入和删除(恒定时间)。大多数方法似乎都工作正常,但在某些“推送”数字下,我得到了错误的输出。 例如,第一个输入是1,2,3,然后在末尾插入4。下一个输出是:2,3,4,但是在我在末尾插入5之后,输出是2,3,5 我不知道是什么原因造成的。我在下面发布了完整的源代码(至少是与错误必须隐藏的测试相关的函数)。文件中有一些文档和一个错误示例,以防我没有解释清楚 #include <iostre

基本上是一个动态数组,当数组已满时,它会进行圆形旋转。您可以访问每个元素,并可以更改其值,但只能从两端插入和删除(恒定时间)。大多数方法似乎都工作正常,但在某些“推送”数字下,我得到了错误的输出。
例如,第一个输入是
1,2,3
,然后在末尾插入
4
。下一个输出是:
2,3,4
,但是在我在末尾插入
5
之后,输出是
2,3,5

我不知道是什么原因造成的。我在下面发布了完整的源代码(至少是与错误必须隐藏的测试相关的函数)。文件中有一些文档和一个错误示例,以防我没有解释清楚

#include <iostream>

using namespace std;

template <typename Object>
class ArrayVector {
private:
  int capacity; // capacity
  int sz; // number of elements
  Object*   a;
  int f; // start of the indexes
  int b; // end of the indexes
public:
  ArrayVector(int initCap);
  ~ArrayVector();

  int size() const { return sz; }
  bool isEmpty() const { return size() == 0; }
  Object elemAtRank(int r);
  void pushBack( const Object& e);
  void pushFront(const Object& e);
  void popBack();
  void popFront();
};

template <typename Object> // constructor
 ArrayVector<Object>::
ArrayVector(int initCap) {
  capacity = initCap;
  sz = 0;
  a  = new Object[capacity];
  f = 0;
  b = 0;
}

 template <typename Object> // gets the element at a certain rank
 Object ArrayVector<Object>:: elemAtRank(int r)
 {
     return a[(f + r) % sz]; // starting position in real array + r % number of elements
 }




template <typename Object>
 void ArrayVector<Object>:: pushBack( const Object& e)
 {
     if(sz == capacity && sz > 0) // if the array is full time to spin it
     {
         if(f == capacity){          // Handles the front.
           f = 0;             // if the front is equal to the capacity
                        // set it to zero, else increment
         }else{
           f++;
         }
         if(b == capacity){     //Handles the back
             b = 0;             //if the back is equal to the capacity
           //  cout<< "SC insert  "<< e << " at  "<< b  <<endl;
             a[b] = e;
         }else{                 // set it to zero, else increment
            a[b] = e;
           // cout<< "SC insert  "<< e << " at  "<< b  <<endl;
            b++;
         }
     }else{

        a[b] = e;
      //  cout<< "insert  "<< e << " at  "<< b  <<endl;
        b++;
         sz++;
     }

 }

 template <typename Object>
  void ArrayVector<Object>:: pushFront( const Object& e)
  {
      if(f == 0){
          f = capacity-1;
      }else{
          f--;
      }
      a[f] = e;
      if(sz< capacity)
          sz++;
  }

int main()
{
    // Fill array and print it
    cout << "Fill with numbers" << endl;
    ArrayVector<int> asd(3);
    asd.pushBack(1);
    asd.pushBack(2);
    asd.pushBack(3);
    for(int i =0; i < asd.size(); i++)
       cout << asd.elemAtRank(i) << endl;
    //Test if it spins
     cout << "BEGIN Spin TEST " << endl;
    asd.pushBack(4);
     cout << "First test is ok" << endl;
    for(int i =0; i < asd.size(); i++)
       cout << asd.elemAtRank(i) << endl;
    // here the error comes
    asd.pushBack(5);
     cout << "On the second iteration things crash and burn" << endl;
for(int i =0; i < asd.size(); i++)
       cout << asd.elemAtRank(i) << endl;
    return 0;
}
#包括
使用名称空间std;
模板
类ArrayVector{
私人:
int容量;//容量
int sz;//元素数
对象*a;
int f;//索引的开始
int b;//索引的结尾
公众:
ArrayVector(int initCap);
~ArrayVector();
int size()常量{return sz;}
bool isEmpty()常量{return size()==0;}
对象元素秩(int r);
无效回推(const Object&e);
正面无效(const Object&e);
void popBack();
void popFront();
};
模板//构造函数
ArrayVector::
ArrayVector(int initCap){
容量=初始上限;
sz=0;
a=新对象[容量];
f=0;
b=0;
}
template//获取某个级别的元素
对象ArrayVector::elemAtRank(int r)
{
返回[(f+r)%sz];//实数组中的起始位置+r%元素数
}
模板
void ArrayVector::pushBack(const Object&e)
{
if(sz==capacity&&sz>0)//如果数组有足够的时间旋转它
{
如果(f==capacity){//处理前端。
f=0;//如果前面等于容量
//将其设置为零,否则递增
}否则{
f++;
}
如果(b==容量){//处理后面的
b=0;//如果背面等于容量

//cout除了插入时间与所需要求不匹配之外,您的问题还在于:

template <typename Object>
void ArrayVector<Object>:: pushFront( const Object& e)
{
    if(f == 0)
    {
        f = capacity-1;
    }
    else
    {
        f--;
    }
    a[f] = e; // problem lies here!

    if(sz < capacity)
        sz++;
}
如果您确实必须使用连续内存块,则不会得到恒定时间插入,但它看起来是这样的:

// Assumptions:  0 is always the front, capacity-1 is always the maximum back
template <typename Object>
void ArrayVector<Object>:: pushFront( const Object& e)
{
    // assume capacity > 0, move all the elements to the right one slot
    for (int i = capacity - 1; i > 0; --i)
    {
        a[i] = a[i - 1];
    }

    a[0] = e;

    if(sz < capacity)
        sz++;
}
//假设:0总是前面,容量-1总是后面的最大值
模板
void ArrayVector::pushFront(const Object&e)
{
//假设容量>0,将所有元素向右移动一个插槽
对于(int i=容量-1;i>0;--i)
{
a[i]=a[i-1];
}
a[0]=e;
if(sz<容量)
sz++;
}

您无法将其实现为一个向量,并在末尾获得恒定的插入时间。当您在前面插入时,您必须将所有现有元素按1(这意味着您必须迭代列表并复制/移动每个元素)。与从后面推动相同。如果您想要固定时间插入,则需要使用列表样式的结构。实现方式更多地是循环缓冲区而不是向量,尽管我对导致错误的原因很感兴趣。您当前的实现不是循环缓冲区。它很大程度上是向量。这是错误的来源您当前代码中存在的逻辑问题(我在回答中对此进行了详细解释)。
// Assumptions:  0 is always the front, capacity-1 is always the maximum back
template <typename Object>
void ArrayVector<Object>:: pushFront( const Object& e)
{
    // assume capacity > 0, move all the elements to the right one slot
    for (int i = capacity - 1; i > 0; --i)
    {
        a[i] = a[i - 1];
    }

    a[0] = e;

    if(sz < capacity)
        sz++;
}