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;
}


 

發佈了78 篇原創文章 · 獲贊 8 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章