System::Collections::Generic::Stack class

Stack class

Stack class forward declaration.

template<typename T>class Stack : public System::Collections::Generic::IEnumerable<T>
ParameterDescription
TElement type.

Nested classes

Methods

MethodDescription
AddRange(IEnumerablePtr)Puts elements into stack.
virtual Clear()Deletes all elements from stack.
virtual Contains(const T&) constChecks if specific item is present in container; uses operator == for comparison.
data()Internal data structure accessor.
data() constInternal data structure accessor.
virtual get_Count() constGets number of elements in stack.
GetEnumerator() overrideGets enumerator to iterate through current stack.
Peek()Gets element from stack top, but keeps it in stack.
Pop()Gets element from top of the stack.
Push(const T&)Puts element of top of the stack.
Stack()Constructs empty stack.
Stack(int)Constructs empty stack.
Stack(IEnumerablePtr)Copy constructor.
virtual ToArray()Converts stack to array.
virtualizeBeginConstIterator() const overrideGets the implementation of begin const iterator for the current container.
virtualizeBeginIterator() overrideGets the implementation of begin iterator for the current container.
virtualizeEndConstIterator() const overrideGets the implementation of end const iterator for the current container.
virtualizeEndIterator() overrideGets the implementation of end iterator for the current container.

Typedefs

TypedefDescription
IEnumerablePtrCollection containing elements of same type.
IEnumeratorPtrEnumerator type.
stack_tRTTI information.
ValueTypeValue type.

Remarks

Stack class wrapping std::list. Objects of this class should only be allocated using System::MakeObject() function. Never create instance of this type on stack or using operator new, as it will result in runtime errors and/or assertion faults. Always wrap this class into System::SmartPtr pointer and use this pointer to pass it to functions as argument.

#include <system/collections/stack.h>
#include <system/smart_ptr.h>

using namespace System;
using namespace System::Collections::Generic;

void PrintItems(const SmartPtr<IEnumerable<int>> &stack)
{
  for (const auto item: stack)
  {
    std::cout << item << ' ';
  }
  std::cout << std::endl;
}

int main()
{
  // Create the Stack-class instance.
  auto stack = MakeObject<Stack<int>>();

  // Fill the stack.
  stack->Push(1);
  stack->Push(2);
  stack->Push(3);

  // Print the last item of the stack. The Peek method doesn't remove an item from the stack.
  std::cout << stack->Peek() << std::endl;
  PrintItems(stack);

  // Print the last item of the stack. The Pop method removes an item from the stack.
  std::cout << stack->Pop() << std::endl;
  PrintItems(stack);

  return 0;
}
/*
This code example produces the following output:
3
3 2 1
3
2 1
*/

See Also