ACE 容器之四 ACE_Bounded_Stack ,ACE_Fixed_Stack,ACE_Unbounded_Stack的使用

ACE提供几个栈容器的使用。有边界限定的,有边界不限定,选择一个合适的自己用用。

// ACEstack.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 runBoundedStack (void)
{
	ACE_TRACE ("StackExample::runBoundedStack");
	ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using a bounded stack\n")));

	//构造一个100个元素的数据栈
	ACE_Bounded_Stack<int> bstack1(100);

	//数组赋值,并且将数组中的元素入栈
	int elem[10];
	for (int i = 0; i < 10; i++)
	{
		elem[i] = i;
		bstack1.push (elem[i]);
	}

	//把一个数据栈完全赋值给另外一个栈。也就是栈的整体拷贝
	//同时验证出栈操作
	ACE_Bounded_Stack<int> bstack2(10);
	bstack2 = bstack1;
	for (int j = 0; j < 10; j++)
	{
		int elem;
		bstack2.pop (elem);
		ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d "), elem));
	}
	printf("\n-------------------------------------\n");
	return 0;
}


//固定大小的栈,并且内部元素为指针
int runFixedStack(void)
{
	ACE_TRACE ("StackExample::runFixedStack");
	ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using a fixed stack\n")));

	ACE_Fixed_Stack<int*, 10> fstack;

	for (int k = 0; k < 10; k++)
	{
		// Push the element on the stack.
		int * elem  = new int;
		*elem = k;
		fstack.push (elem);    
	}

	for (int l = 0; l < 10; l++)
	{
		int* elem = 0;
		fstack.pop (elem);
		ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d "), *elem));
		delete elem;
	}
	printf("\n-------------------------------------\n");
	return 0;
}


//没有边界限制的栈,也就是说数组元素的个数没有任何限制
int runUnboundedStack (void)
{
	ACE_TRACE ("StackExample::runUnboundedStack");
	ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using an unbounded stack\n")));

	ACE_Unbounded_Stack<int*> ustack;
	ACE_Bounded_Stack<int*> privateStack_(100);
	for (int m = 0; m < 10; m++)
	{
		int  *elem = new int;
		*elem = m;
		// Push the element on both stacks.
		ustack.push (elem);
		privateStack_.push(elem);
	}

	// Oddly enough, you can actually iterate through an
	// unbounded stack! This is because underneath the covers
	// the unbounded stack is a linked list.
	// This will cause the elements in the private stack to
	// also disappear!

	ACE_Unbounded_Stack_Iterator<int*> iter(ustack);

	for (iter.first (); !iter.done (); iter.advance ())
	{
		int ** elem = 0;
		iter.next (elem);
		ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d "),**elem));

		delete (*elem);
	}
	printf("\n");
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	runBoundedStack();
	runFixedStack();
	runUnboundedStack();
	getchar();
	return 0;
}


 

你可能感兴趣的:(ACE 容器之四 ACE_Bounded_Stack ,ACE_Fixed_Stack,ACE_Unbounded_Stack的使用)