ACE 容器之三 ACE_Unbounded_Queue的使用

以下代码演示了如何在ACE_Unbounded_Queue这个队列容器中存储具体的数据元素和数据元素的指针。

 

// ACEqueue.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ace/OS_Memory.h"
#include "ace/Log_Msg.h"
#include "ace/Containers.h"

//演示队列元素存储具体的元素
int runStackUnboundedQueue (void)
{
	ACE_TRACE ("QueueExample::runStackUnboundedQueue");

	//在队列头部插入 0 --- 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
	ACE_Unbounded_Queue<int> queue;
	int elem1[10];
	int i;
	for (i = 0; i < 10; i++)
	{
		elem1[i] = i;
		queue.enqueue_head (elem1[i]);
	}

	//在队列尾部插入 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
	int elem2[10];
	for (i = 0; i < 10; i++)
	{
		elem2[i] = i + 10;
		queue.enqueue_tail(elem2[i]);
	}

	//队列输出
	for (ACE_Unbounded_Queue_Iterator<int> iter (queue); !iter.done (); iter.advance ())
	{
		int *elem = 0;
		iter.next (elem);
		printf("%d ", *elem);
	}
	printf("\n");

	return 0;
}


//演示队列元素存储指针
int runHeapUnboundedQueue (void)
{
	ACE_TRACE ("QueueExample::runHeapUnboundedQueue");

	ACE_Unbounded_Queue<int*> queue;
	for (int i = 0; i < 20; i++)
	{
		int *elem = new int;
		*elem = i;
		queue.enqueue_head(elem);
	}

	for (ACE_Unbounded_Queue_Iterator<int*> iter= queue.begin (); !iter.done (); iter.advance ())
	{
		int **elem = 0;
		iter.next(elem);
		printf("%d ", **elem);
		delete (*elem);
	}

return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
	runStackUnboundedQueue();
	runHeapUnboundedQueue();
	getchar();
	return 0;
}


你可能感兴趣的:(Queue)