本程序在VS2010及以上编译时可能会出现错误:
error C4996:
'std::_Copy_impl': Function call with parameters that may be unsafe
- this call relies on the caller to check that the passed values
are correct. To disable this warning, use
-D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual
C++ 'Checked Iterators'
1> d:\program files (x86)\microsoft
visual studio 12.0\vc\include\xutility(2113) : 参见"std::_Copy_impl"的声明
请按照一下步骤处理:
在主程序前面添加一段宏定义
#ifndef
_ITERATOR_DEBUG_LEVEL
#define
_ITERATOR_DEBUG_LEVEL 0
#else
#undef
_ITERATOR_DEBUG_LEVEL
#define
_ITERATOR_DEBUG_LEVEL 0
#endif
Queue.h头文件源代码:
#ifndef
_QUEUE_H
#define
_QUEUE_H
#include
template
class
Queue
{
public:
Queue(int
queueCapacity = 10);
bool
IsEmpty()
const; //判断队列是否为空
T&
Front()
const; //查看队首的数据
T&
Rear()
const; //查看队尾的数据
void
Push(const T&
item); //在队尾插入数据
void
Pop(); //删除队首的数据
private:
T*
queue; //创建动态数据
int
front; //记录队首位置
int
rear; //记录队尾位置
int
capacity;
};
template
Queue::Queue(int
queueCapacity) :capacity(queueCapacity)
{
if
(capacity<1)
throw"Queue
capacity must be >0";
queue
= new T[capacity];
front
= rear = 0;
}
template
inline bool
Queue::IsEmpty() const
{
return
front == rear;
}
template
void
Queue::Push(const T& item)
{
if
((rear + 1) % capacity ==
front) //队列满了,扩充队列
{
T*
newQueue = new T[2 * capacity];
int
start = (front + 1) % capacity;
if
(start<2) //没有回绕
copy(queue
+ start, queue + start + capacity - 1, newQueue);
else //发生了回转
{
copy(queue
+ start, queue + capacity, newQueue);
copy(queue,
queue + rear + 1, newQueue + capacity - 1);
}
front
= 2 * capacity - 1;
rear
= capacity - 2;
capacity
= 2 * capacity;
delete[]
queue;
queue
= newQueue;
}
rear
= (rear + 1) %
capacity; //很高明的写法!
queue[rear]
= item;
}
template
inline
T& Queue::Front() const
{
if
(IsEmpty())
throw
"Queue is empty.No front element";
return
queue[(front + 1) % capacity];
}
template
inline
T& Queue::Rear() const
{
if
(IsEmpty())
throw
"Queue is empty.No Rear element";
return
queue[rear];
}
template
void
Queue::Pop()
{
if
(IsEmpty())
throw"Queue
is empty.Cannot delete";
front
= (front + 1) % capacity;
queue[front].~T();
}
#endif
主程序源代码:
#ifndef
_ITERATOR_DEBUG_LEVEL
#define
_ITERATOR_DEBUG_LEVEL 0
#else
#undef
_ITERATOR_DEBUG_LEVEL
#define
_ITERATOR_DEBUG_LEVEL 0
#endif
#include
#include"Queue.h"
using
namespace std;
int
main()
{
Queue
q(8);
q.Push('A');
q.Push('B');
q.Push('C');
cout
<< q.Front() << "," << q.Rear() <<
endl;
q.Push('E');
q.Push('F');
cout
<< q.Front() << "," << q.Rear() <<
endl;
q.Pop();
cout
<< q.Front() << "," << q.Rear() <<
endl;
q.Push('D');
cout
<< q.Front() << "," << q.Rear() <<
endl;
return
0;
}
运行结果:
注:本程序无法在g++下编译通过,g++的代码如下
Queue.h头文件源代码:
#ifndef
_QUEUE_H
#define
_QUEUE_H
#include
template
class
Queue
{
public:
Queue(int
queueCapacity=10);
bool
IsEmpty()
const; //判断队列是否为空
T&
Front()
const; //查看队首的数据
T&
Rear()
const; //查看队尾的数据
void
Push(const T&
item); //在队尾插入数据
void
Pop(); //删除队首的数据
private:
T*
queue; //创建动态数据
int
front; //记录队首位置
int
rear; //记录队尾位置
int
capacity;
};
template
Queue::Queue(int
queueCapacity):capacity(queueCapacity)
{
if(capacity<1)
throw"Queue
capacity must be >0";
queue=new
T[capacity];
front=rear=0;
}
template
inline bool
Queue::IsEmpty() const
{
return
front==rear;
}
template
void
Queue::Push(const T& item)
{
if((rear+1)%pacity==front) //队列满了,扩充队列
{
T*
newQueue=new T[2*capacity];
int
start=(front+1)%pacity;
if(start<2) //没有回绕
std::copy(queue+start,queue+start+capacity-1,newQueue);
else //发生了回转
{
std::copy(queue+start,queue+capacity,newQueue);
std::copy(queue,queue+rear+1,newQueue+capacity-1);
}
front=2*capacity-1;
rear=capacity-2;
capacity=2*capacity;
delete
[] queue;
queue=newQueue;
}
rear=(rear+1)%pacity; //很高明的写法!
queue[rear]=item;
}
template
inline
T& Queue::Front() const
{
if(IsEmpty())
throw
"Queue is empty.No front element";
return
queue[(front+1)%pacity];
}
template
inline
T& Queue::Rear() const
{
if(IsEmpty())
throw
"Queue is empty.No Rear element";
return
queue[rear];
}
template
void
Queue::Pop()
{
if(IsEmpty())
throw"Queue
is empty.Cannot delete";
front=(front+1)%pacity;
queue[front].~T();
}
#endif
主程序源代码:
#include
#include"Queue.h"
using
namespace std;
int
main()
{
Queue
q(8);
q.Push('A');
q.Push('B');
q.Push('C');
cout
<< q.Front() << "," << q.Rear() <<
endl;
q.Push('E');
q.Push('F');
cout
<< q.Front() << "," << q.Rear() <<
endl;
q.Pop();
cout
<< q.Front() << "," << q.Rear() <<
endl;
q.Push('D');
cout
<< q.Front() << "," << q.Rear() <<
endl;
return
0;
}