template <typename T1, typename T2>
void construct(T1 *p, const T2 &value)
{
new (p) T1(value);
}
// Destroy an object (but does not deallocate any memory)
template <typename T>
void destroy(T *p)
{
p->~T();
}
// Destroy an object range (but does not deallocate any memory)
template <typename FwdIter>
void destroy(FwdIter first, FwdIter last)
{
while (first != last)
{
destroy(&*first);
++first;
}
}
// Exchange the values of two objects
template <typename T>
void swap(T &a, T &b)
{
T temp(a);
a = b;
b = temp;
}
// Return first element in queue
template<typename T>
T & Queue::front()
{
// if the queue is empty, throw an exception and report to user
if (vused_ == 0)
{
throw logic_error("Error: empty queue");
}
// otherwise, return the first element in the queue
else
{
return v_[0];
}
}
// Return first element in queue
template<typename T>
const T & Queue::front() const
{
// if the queue is empty, throw an exception and report to user
if (vused_ == 0)
{
throw logic_error("Error: empty queue");
}
// otherwise, return the first element in the queue
else
{
return v_;
}
}
// Determine whether queue is empty
template<typename T>
bool Queue::empty() const
{
return (vused_ == 0);
}
// vused_ accessor
template<typename T>
size_t Queue::size() const
{
return vused_;
}
int main()
{
Queue myQueue;
cout << "Is the queue empty? Answer: " << myQueue.empty() << "\n";
for (int i = 0; i < numElems; ++i)
{
cout << "Pushing " << elems[i] << " to queue\n";
myQueue.push(elems[i]);
}
cout << "Is the queue empty? Answer: " << myQueue.empty() << "\n";
cout << "The current front element is " << myQueue.front() << " and the current size is " << myQueue.size() << "\n";
for (int i = 0; i < numElems-1; ++i)
{
cout << "Popping " << myQueue.front() << ". ";
myQueue.pop();
cout << "The current front element is " << myQueue.front() << " and the current size is " << myQueue.size() << "\n";
}
Queue myQueue2;
for (int i = 0; i < numElems; ++i)
{
cout << "Pushing " << elems[i] << " to queue 2\n";
myQueue2.push(elems[i]);
}
Queue myQueue3;
myQueue3 = myQueue2;
cout << "Queue 3 front: " << myQueue3.front() << ", queue 2 front: " << myQueue2.front() << "\n";
cout << "Queue 3 size: " << myQueue3.size() << ", queue 2 size: " << myQueue2.size() << "\n";
}
©2022 Christopher George Berger · All Rights Reserved · License · About · Linked
Do'stlaringiz bilan baham: |