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.
Subscribe to:
Post Comments (Atom)
Measure execution time with Julia, example using sorting algorithms
# random integers between 1 and 100 inclusive, generate thousands of them x = rand ( 1 : 100 , 100000 ) @time sort (x; alg=InsertionSort, r...
-
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 RM38. Indirectly, the subscription fee is ...
-
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...
No comments:
Post a Comment