Skip to content

The 25 days of C++mas - December 23 - C++ stack, Lambda expression, Win64/Win32

Two more days to go until the 25 days of C++mas are done.  There are two more days until Christmas.  It’s raining very hard here in Santa Cruz California (flash flood alerts keep interrupting the San Francisco 49ers vs Seattle Seahawks football game).  I was playing with the C++Builder XE3 64-bit compiler and the support for Lambda Expressions.  I modified an example from our Embarcadero DocWiki that uses a C++ stack and Lambda and updated it to work with both the 32-bit and 64-bit compilers.  In the 64-bit version, unicode strings are pushed onto the stack and a Lambda Expression is used to clear the stack.  In the 32-bit version, unicode strings are pushed onto the stack and a while loop is used to pop the stack until it is empty.

The FireMonkey (FMX) form contains a TButton and a TMemo.

Here is the code containing the Button Click Event Handler:

    //---------------------------------------------------------------------------
    #include <fmx.h>
    #pragma hdrstop
    #include "MainUnit.h"
    #include <stack>   // from the Dinkumware Standard C++ Library (STL)
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.fmx"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
    	: TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    	Memo1->Lines->Clear();
    	// Create the stack
    	std::stack stk;  // create stack to contain Unicode Strings
    	Memo1->Lines->Add("Stack Created");
    	Memo1->Lines->Add("The stack contains: "
    		+ IntToStr((int)stk.size())
    		+ " elements.");
    	// Add elements to the stack
    	stk.push("John");
    	Memo1->Lines->Add("Pushed John");
    	stk.push("Mary");
    	Memo1->Lines->Add("Pushed Mary");
    	stk.push("Bob");
    	Memo1->Lines->Add("Pushed Bob");
    	stk.push("Anna");
    	Memo1->Lines->Add("Pushed Anna");
    	stk.push("Erica");
    	Memo1->Lines->Add("Pushed Erica");
    	// Show the last pushed element "Erica"
    	Memo1->Lines->Add("Stack Top element is: " + stk.top());
    	Memo1->Lines->Add("The stack contains: "
    		+ IntToStr((int)stk.size())
    		+ " elements.");
    	// Pop the last pushed element
    	stk.pop();
    	Memo1->Lines->Add("Popped Stack");
    	// Show the number of elements in the stack "4"
    	Memo1->Lines->Add("The stack contains: "
    		+ IntToStr((int)stk.size())
    		+ " elements.");
    	// Show the last pushed element "Anna"
    	Memo1->Lines->Add("Top of stack element is: " + stk.top());
    	#ifdef _WIN64
    		// Lambda expression for clearing the stack
    		// will work only for 64-bit C++ applications
    		[&]
    		() {
    				std::stack empty;   // create an empty stack
    				swap(stk, empty);  // swap it with
    		}();
    		Memo1->Lines->Add("Stack Emptied using Lambda Expression");
    	#else
    		// if not Win64 then use loop to empty the stack
    		while (!stk.empty()) {
    			stk.pop();
    		}
    	#endif
    	// Show the number of elements in the stack "0"
    	Memo1->Lines->Add("The stack contains: "
    		+ IntToStr((int)stk.size())
    		+ " elements.");
    }
    //---------------------------------------------------------------------------

The output is the same on 32-bit and 64-bit windows:

Additional information about Lambda Expressions can be found at:

Watch/Download the C++Builder 64-bit Compiler Preview Video

Additional details about the 64-bit C++Builder compiler are available in the preview video.  Watch the C++ 64-bit compiler preview video on YouTube at http://www.youtube.com/watch?v=PwwMpBUoR6Y. You can also download the MP4 video file at http://cc.embarcadero.com/item/29197.  The preview video is 9 minutes long.

Try the C++Builder 64-bit compiler

The C++Builder XE3 and RAD Studio XE3 free trial downloads have been updated to include the new C++Builder 64-bit compiler.  Trial downloads are available at https://downloads.embarcadero.com/free/c_builder

Happy C++mas!

During the first 25 days of December leading up to Christmas, I will blog about the release of the C++Builder 64-bit compiler for Microsoft Windows and C++Builder XE3 support for building VCL and FireMonkey 2 applications.

Post a Comment

Your email is never published nor shared. Required fields are marked *

Bad Behavior has blocked 4 access attempts in the last 7 days.

Close