堆栈(Stack)

堆栈(Stack)

日常总结    2009-06-06 16:27   阅读17   评论0  
字号:    

ACE支持动态栈和静态栈,静态栈的尺寸是固定的,动态栈在每次入栈的时候分配内存出栈的时候释放内存。ACE拥有三个模板容器用来实现栈,ACE_Bounded_StackACE_Fixed_Stack实现了静态栈,ACE_Unbounded_Stack实现动态栈。而ACE_Bounded_StackACE_Fixed_Stack的不同在于一个是在运行初始化的时候确定栈的大小一个是在编译的时候确定栈的大小。

示例代码

Stacks.cpp

此示例用来演示ACE提供的ACE_Bounded_StackACE_Fixed_StackACE_Unbounded_Stack几个模板容器的使用方法。
// Stacks.cpp,v 1.5 2006/03/30 12:41:26 jwillemsen Exp
 
#include "ace/OS_Memory.h"
#include "ace/Log_Msg.h"
#include "ace/Containers.h"
#include "DataElement.h"
 
class StackExample
{
public:
  StackExample (): privateStack_(100) {}
 
  // Illustrate all the differnet
  // types of stacks provided by ACE.
  int run (void);
 
private:
  // Illustrate the use of a bounded stack.
  int runBoundedStack (void);
 
  // Illustrate the use of an unbounded stack.
  int runUnboundedStack (void);
 
  // Illustrate the use of a compile time fixed stack.
  int runFixedStack (void);
 
private:
  ACE_Bounded_Stack<DataElement*> privateStack_;
};
 
int StackExample::run (void)
{
  ACE_TRACE (ACE_TEXT ("StackUser::run"));
 
  ACE_ASSERT(!this->runBoundedStack());
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("/n# of live objects %d/n"),
              DataElement::numOfActiveObjects()));
 
  ACE_ASSERT(!this->runFixedStack());
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("/n# of live objects %d/n"),
              DataElement::numOfActiveObjects()));
 
  ACE_ASSERT(!this->runUnboundedStack());
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("/n# of live objects %d/n"),
              DataElement::numOfActiveObjects()));
 
  return 0;
}
// Listing 1 code/ch05
//静态栈示例1
int StackExample::runBoundedStack (void)
{
  ACE_TRACE (ACE_TEXT ("StackExample::runBoundedStack"));
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using a bounded stack/n")));
  //通过构造函数创建一个最多容纳100个数据元素的静态栈
  ACE_Bounded_Stack<DataElement> bstack1 (100);
 
  // The element array is constrained to this scope.
  {
    DataElement elem[100];
    for (int i = 0; i < 100; i++)
      {
        elem[i].setData(i);
        // Push the element on the stack.
        bstack1.push (elem[i]);
      }
  }
 
  ACE_Bounded_Stack<DataElement> bstack2 (100);
 
  // Make a copy!
  bstack2 = bstack1;
  for (int j = 0; j < 100; j++)
    {
      DataElement elem;
      bstack2.pop (elem);
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), elem.getData ()));
    }
 
  return 0;
}
// Listing 1
// Listing 2 code/ch05
//静态栈示例2
int StackExample::runFixedStack (void)
{
  ACE_TRACE (ACE_TEXT ("StackExample::runFixedStack"));
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using a fixed stack/n")));
  //栈的大小在模板参数里指定,而不是通过构造函数指定。ACE_Bounded_Stack 的初始大小是运行期通过构造函数参数指定的。
  ACE_Fixed_Stack<DataElement*, 100> fstack;
  for (int k = 0; k < 100; k++)
    {
      //在此栈不是之间存储的数据元素而是存储的数据指针,比起对数据元素的出栈入栈会更高效
      DataElement* elem;
      ACE_NEW_RETURN(elem, DataElement (k), -1);
      fstack.push (elem);    // Push the element on the stack.
    }
 
  for (int l = 0; l < 100; l++)
    {
      DataElement* elem = 0;
      fstack.pop (elem);
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), elem->getData ()));
      delete elem;
    }
 
  return 0;
}
//动态链接栈的示例
int StackExample::runUnboundedStack (void)
{
  ACE_TRACE (ACE_TEXT ("StackExample::runUnboundedStack"));
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using an unbounded stack/n")));
 
  ACE_Unbounded_Stack<DataElement*> ustack;
  for (int m = 0; m < 100; m++)
    {
      DataElement *elem;
      ACE_NEW_RETURN(elem, DataElement (m), -1);
      // 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<DataElement*> iter (ustack);
  for (iter.first (); !iter.done (); iter.advance ())
    {
      DataElement** elem = 0;
      iter.next (elem);
      ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"),
                  (*elem)->getData ()));
      delete (*elem);
    }
 
  return 0;
}
// Listing 2
 
int ACE_TMAIN (int, ACE_TCHAR *[])
{
  StackExample se;
  return se.run ();
}
 
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Bounded_Stack<DataElement>;
template class ACE_Bounded_Stack<DataElement*>;
template class ACE_Fixed_Stack<DataElement*, 100>;
template class ACE_Node<DataElement*>;
template class ACE_Unbounded_Stack<DataElement*>;
template class ACE_Unbounded_Stack_Iterator<DataElement*>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Bounded_Stack<DataElement>
#pragma instantiate ACE_Bounded_Stack<DataElement*>
#pragma instantiate ACE_Fixed_Stack<DataElement*, 100>
#pragma instantiate ACE_Node<DataElement*>
#pragma instantiate ACE_Unbounded_Stack<DataElement*>
#pragma instantiate ACE_Unbounded_Stack_Iterator<DataElement*>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION*/

执行结果

D:/project/ACE_wrappers/examples/APG/Containers>Stacks
Using a bounded stack
99:98:97:96:95:94:93:92:91:90:89:88:87:86:85:84:83:82:81:80:79:78:77:76:75:74:73
:72:71:70:69:68:67:66:65:64:63:62:61:60:59:58:57:56:55:54:53:52:51:50:49:48:47:4
6:45:44:43:42:41:40:39:38:37:36:35:34:33:32:31:30:29:28:27:26:25:24:23:22:21:20:
19:18:17:16:15:14:13:12:11:10:9:8:7:6:5:4:3:2:1:0:
# of live objects 0
Using a fixed stack
99:98:97:96:95:94:93:92:91:90:89:88:87:86:85:84:83:82:81:80:79:78:77:76:75:74:73
:72:71:70:69:68:67:66:65:64:63:62:61:60:59:58:57:56:55:54:53:52:51:50:49:48:47:4
6:45:44:43:42:41:40:39:38:37:36:35:34:33:32:31:30:29:28:27:26:25:24:23:22:21:20:
19:18:17:16:15:14:13:12:11:10:9:8:7:6:5:4:3:2:1:0:
# of live objects 0
Using an unbounded stack
99:98:97:96:95:94:93:92:91:90:89:88:87:86:85:84:83:82:81:80:79:78:77:76:75:74:73
:72:71:70:69:68:67:66:65:64:63:62:61:60:59:58:57:56:55:54:53:52:51:50:49:48:47:4
6:45:44:43:42:41:40:39:38:37:36:35:34:33:32:31:30:29:28:27:26:25:24:23:22:21:20:
19:18:17:16:15:14:13:12:11:10:9:8:7:6:5:4:3:2:1:0:
# of live objects 0

你可能感兴趣的:(delete,存储,Class,Types,Instantiation)