Pop函数的bug,没有招到

baitiane7100 baitiane7100 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include "stdafx.h"
#include <iostream>

using namespace std;

#define MAXSIZE 100


class CircleQueue
{

int arr[MAXSIZE ];
int front,rear;

public:

CircleQueue()
{
front=0;
rear=0;

};

bool IsEmpty() const
{
return front==rear;

}

bool IsFull() const
{

return front==(rear+1)%MAXSIZE ;

}


void Push(int val)
{
if(! IsFull())
{
rear=(rear+1)%MAXSIZE;
arr[rear-1]=val;
}

}


int Pop() throw(out_of_range)
{
if( ! IsEmpty() )
{
{
int tmp=front;
front=(front+1)%MAXSIZE;
return arr[tmp];
}
}
else
throw out_of_range("队列空");
}

int size() const
{
return (rear+MAXSIZE-front)%MAXSIZE;
}
};


int main()
{

CircleQueue cq;

int i;

int const count=10;

for(i=0; i<count; i++)
{
cq.Push(i);
}
cout<<"循环队列的元素为:"<<cq.size()<<endl;


int size=cq.size();


for(i=0; i<cq.size(); i++)
{
try
{
cout<<cq.Pop()<<endl;
}
catch(out_of_range e)
{
cout<<e.what()<<endl;
}
}

return 0;
}




弹出第5个元素的时候,程序就会终止,跟踪很多次,都没有发现问题

...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
baitiane7100
C++ 语言 65,214 成员 250,514 内容