Haruyuki Mohri

毛利春幸のブログです。

It is described in C++Builder implementation using TStack

TStack is generics class for delphi.

But, try to implemented in C++Builder.
In the case of C++Builder there is a std::stack.
Towards its std::stack is, is recommended.

https://gist.github.com/mojeld/424e10acf586549304e5

//Implementation using TStack.
#include <System.Generics.Collections.hpp>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    std::tr1::shared_ptr<TStack__1<int> > delphi_stack(new TStack__1<int>());
    for (int i = 0; i < 10; i++)
    {
        delphi_stack->Push(i);
    }
    //If you want to executed "Peek()", its Stack is not deleted.
    Memo1->Lines->Append("Peek()= " + IntToStr(delphi_stack->Peek()) );
    Memo1->Lines->Append("Stack Delete");
    delphi_stack->Extract();

    int max_count = delphi_stack->Count;
    Memo1->Lines->Append("Count= " + IntToStr(max_count) );
    for (int i = 0; i < max_count; i++)
    {
        //If you want to executed "Pop()", its Stack is deleted.
        Memo1->Lines->Append("Pop()= " + IntToStr(delphi_stack->Pop() ));
    }
}
//---------------------------------------------------------------------------

In the case of std::stack

I will recommend those of std::stack.

#include <stack>
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    std::stack<String> std_stack;
    for (int i = 0; i < 10; i++)
    {
        std_stack.push(IntToStr(i));
    }

    int max_count = (int)std_stack.size();

    for (int i = 0; i < max_count; i++)
    {
        Memo1->Lines->Append("top()= " + std_stack.top());
        //The pop() in this case, meaning that you want to delete.
        std_stack.pop();
    }
}
//---------------------------------------------------------------------------


f:id:mojeld:20150824115053p:plain