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>usingnamespaceSystem;usingnamespaceSystem::Collections::Generic;voidPrintItems(constSmartPtr<IEnumerable<int>>&stack){for(constautoitem:stack){std::cout<<item<<' ';}std::cout<<std::endl;}intmain(){// Create the Stack-class instance.
autostack=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);return0;}/*
This code example produces the following output:
3
3 2 1
3
2 1
*/