View Stack data structure source code from CodeBlocks and g++
Open CodeBlocks
Create a CodeBlocks project
Menu > File > New > Project > Console application > Go button
Follow on-screen instructions
Replace all the main.cpp content with the below code.
{{{{{
#include <iostream>
#include <stack>
using namespace std;
int main(int argc, char* argv[])
{
stack<int> mystack;
mystack.push(1);
mystack.push(2);
mystack.push(3);
cout << "stack size: " << mystack.size() << endl;
cout << "stack content in LIFO context: ";
while(!mystack.empty())
{
cout << mystack.top() << " ";
mystack.pop();
}
cout << endl;
return 0;
}
}}}}}
Right-click at the push() method of the mystack object on the line “mystack.push(1);”, select “Find implementation of: push”
The stack data structure source code from the g++.exe compiler is at:
“C:\program files (x86)\codeblocks\MinGW\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_stack.h”
File name: stl_stack.h
Both the stack class declaration and implementation are coded in this header file.
You can view the push() method implementation of the stack class as shown below:
{{{{{
/**
* @brief Add data to the top of the %stack. //documentation comments
* @param __x Data to be added.
*
* This is a typical %stack operation. The function creates an
* element at the top of the %stack and assigns the given data
* to it. The time complexity of the operation depends on the
* underlying sequence.
*/
void
push(const value_type& __x) //call by reference
{ c.push_back(__x); }
}}}}}
Right-click on the “deque” word on top of the “class stack” line as shown below and select “Find declaration of: deque”.
{{{{{
template<typename _Tp, typename _Sequence = deque<_Tp> >
class stack
{
. . .
}}}}}
The g++ stack class is implemented as a container adapter. It uses the standard deque data structure.
The stl_deque.h is at:
C:\program files (x86)\codeblocks\MinGW\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_deque.h
Press Ctrl+F and search for the “push_back” method.
The push_back() method implementation for the deque data structure is shown below:
{{{{{
void
push_back(const value_type& __x)
{
if (this->_M_impl._M_finish._M_cur
!= this->_M_impl._M_finish._M_last - 1) // deque capacity is not full
{ // add data to the end of the deque
this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
++this->_M_impl._M_finish._M_cur;
}
else // else add data to the end of the auxiliary deque
_M_push_back_aux(__x);
}
}}}}}
Due to the nature of the deque data structure, this push_back() operation can be done in constant time.
Reading. Practice. Writing.
Please feel free to comment. Suggestions are also welcomed here. Write something "wholesome serving purpose" here.
Wednesday
Sunday
How to debug your C++ code in CodeBlocks?
How to debug your C++ code in CodeBlocks?
You have to create a C++ project in CodeBlocks before you can start using the CodeBlocks debugger.
Use the following code.
File name: main.cpp
File content:
{{{{{
01 #include <iostream>
02 using namespace std;
03
04 int add(int a, int b);
05
06 int main(int argc, char* argv[])
07 {
08 int x=5, y=2, z;
09
10 z = add(x,y);
11
12 cout << "z value is " << z << endl;
13
14 return 0;
15 }
16
17 int add(int a, int b)
18 {
19 int c;
20
21 c = a + b;
22
23 return c;
24 }
}}}}}
Open the Watches window to watch certain variables during the execution of the program:
Menu > Debug > Debugging windows > Watches
You can start debugging at any line of your code:
-- Put the mouse cursor on line 8 which is the initialization values of variables
-- Press F4 to run to cursor
-- Press F7 to debug next line which is the add() function "z = add(x,y)"
-- While debugging, check all the variable values in the Watches window. You can see that x=5, y=2, and z is set to a random value
-- Press Shift+F7 to step into the body of the add() function
-- Press F7 to debug next line
-- In the watches window, you can know the local variable values of add() function where a=5, b=2, and c=7
-- Click on the "Toolbar > Debug > Step out" to step out from the add() function
-- Press F7 to debug next line
-- In the watches window, you can see that z=7
-- Press F7 to debug next line
-- Check out the command prompt window, you will get the program output "z value is 7"
-- Press F8 to continue debugging and your program will run to completion. Try not to select from the Debug menu "Break debugger" because you want your program to end naturally rather than aborting.
You can debug your program at multiple lines from all your marked out breakpoints:
-- Click on line 10 "z = add(x,y);" and press F5 to insert a breakpoint.
-- Click on line 21 "c = a + b;" and press F5 to insert a breakpoint.
-- Press F8 to start debugging. The debugger will pause at line 10 as the first breakpoint
-- In the watches window, you can see that x=5, y=2, and z is set to a random value
-- Press F8 to continue debugging at line 21 as the second breakpoint
-- Click on the "Toolbar > Debug > Step out" to step out from the add() function
-- Press F7 to debug next line
-- Press F7 to debug next line
-- Check out the command prompt window, you will get the program output "z value is 7"
-- Press F8 to continue debugging and your program will run to completion.
You have to create a C++ project in CodeBlocks before you can start using the CodeBlocks debugger.
Use the following code.
File name: main.cpp
File content:
{{{{{
01 #include <iostream>
02 using namespace std;
03
04 int add(int a, int b);
05
06 int main(int argc, char* argv[])
07 {
08 int x=5, y=2, z;
09
10 z = add(x,y);
11
12 cout << "z value is " << z << endl;
13
14 return 0;
15 }
16
17 int add(int a, int b)
18 {
19 int c;
20
21 c = a + b;
22
23 return c;
24 }
}}}}}
Open the Watches window to watch certain variables during the execution of the program:
Menu > Debug > Debugging windows > Watches
You can start debugging at any line of your code:
-- Put the mouse cursor on line 8 which is the initialization values of variables
-- Press F4 to run to cursor
-- Press F7 to debug next line which is the add() function "z = add(x,y)"
-- While debugging, check all the variable values in the Watches window. You can see that x=5, y=2, and z is set to a random value
-- Press Shift+F7 to step into the body of the add() function
-- Press F7 to debug next line
-- In the watches window, you can know the local variable values of add() function where a=5, b=2, and c=7
-- Click on the "Toolbar > Debug > Step out" to step out from the add() function
-- Press F7 to debug next line
-- In the watches window, you can see that z=7
-- Press F7 to debug next line
-- Check out the command prompt window, you will get the program output "z value is 7"
-- Press F8 to continue debugging and your program will run to completion. Try not to select from the Debug menu "Break debugger" because you want your program to end naturally rather than aborting.
You can debug your program at multiple lines from all your marked out breakpoints:
-- Click on line 10 "z = add(x,y);" and press F5 to insert a breakpoint.
-- Click on line 21 "c = a + b;" and press F5 to insert a breakpoint.
-- Press F8 to start debugging. The debugger will pause at line 10 as the first breakpoint
-- In the watches window, you can see that x=5, y=2, and z is set to a random value
-- Press F8 to continue debugging at line 21 as the second breakpoint
-- Click on the "Toolbar > Debug > Step out" to step out from the add() function
-- Press F7 to debug next line
-- Press F7 to debug next line
-- Check out the command prompt window, you will get the program output "z value is 7"
-- Press F8 to continue debugging and your program will run to completion.
Saturday
How to download and run LibreOffice Portable version from your flash drive
How to download and run LibreOffice Portable version from your flash drive:
Download to flash drive and double-click to install LibreOffice Portable version from
https://www.libreoffice.org/download/portable-versions/
Direct link to the executable file is at
http://download.documentfoundation.org/libreoffice/portable/5.0.2/LibreOfficePortable_5.0.2_MultilingualStandard.paf.exe
Why do you choose LibreOffice software as your office suite?
** LibreOffice is an open source software
** LibreOffice is available for a variety of computing platforms including Microsoft Windows, Mac OS X, Linux, LibreOffice Viewer for Android
** Has portable version
** Most importantly, LibreOffice includes the all familiar pull-down menus, customizable toolbars, and buttons as default
Download to flash drive and double-click to install LibreOffice Portable version from
https://www.libreoffice.org/download/portable-versions/
Direct link to the executable file is at
http://download.documentfoundation.org/libreoffice/portable/5.0.2/LibreOfficePortable_5.0.2_MultilingualStandard.paf.exe
Why do you choose LibreOffice software as your office suite?
** LibreOffice is an open source software
** LibreOffice is available for a variety of computing platforms including Microsoft Windows, Mac OS X, Linux, LibreOffice Viewer for Android
** Has portable version
** Most importantly, LibreOffice includes the all familiar pull-down menus, customizable toolbars, and buttons as default
Android Phone: How to enable Child mode by allowing only education apps to be played by your child
Android Phone: How to enable Child mode by allowing only education apps to be played by your child:
-- Home screen > Settings > Child mode
-- Home screen > Settings > Child mode
Subscribe to:
Posts (Atom)
If a hater attacked your age and not the goodness of you
Whether young or old, I've always been known what endures. I've known the very idea of people that were all created equal and deserv...
-
DiGi Super Long Life is a feature where you can extend your talktime validity to 1 year for just RM38. Indirectly, the subscription fee is ...
-
Open LibreOffice Draw program. Select the rectangle shape from shape toolbar at the bottom of drawing page. Click a point and drag-and-re...
-
DiGi Super Long Life is a feature where you can extend your talktime validity to 1 year for just RM30. Indirectly, the subscription fee is...