Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

C++ > Code Snippets Code Examples

Stack: size, pop and push

/* Stack: size, pop and push */ #include <iostream> #include <stack> using namespace std; int main() { int thedata[] = {45, 34, 56, 27, 71, 50, 62}; stack<int> s; cout << "The stack size is now " << s.size() << endl; cout << "Pushing 4 elements " << endl; for (int i = 0; i < 4; ++i) s.push(thedata[i]); cout << "The stack size is now " << s.size() << endl; cout << "Popping 3 elements " << endl; for (int i = 0; i < 3; ++i) { cout << s.top() << endl; s.pop(); } cout << "The stack size is now " << s.size() << endl; return 0; } /* The stack size is now 0 Pushing 4 elements The stack size is now 4 Popping 3 elements 27 56 34 The stack size is now 1 */

Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

LIFO stack. Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container. stacks are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack.

Insert element. Inserts a new element at the top of the stack, above its current top element. The content of this new element is initialized to a copy of val. This member function effectively calls the member function push_back of the underlying container object. C++ Stack push () function is used for adding new elements at the top of the stack. If we have an array of type stack and by using the push() function we can insert new elements in the stack. The elements are inserted at the top of the stack. The element which is inserted most initially is deleted at the end and vice versa as stacks follow LIFO principle.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

Return size. Returns the number of elements in the stack. stack::size() function is an inbuilt function in C++ STL, which is defined in <stack>header file. size() is used to check the associated container's size and return the result in an integer value, which is the number of elements in the container. If the container is empty the size() returns 0. This member function effectively calls member size of the underlying container object. No parameter is required.

Access next element. Returns a reference to the top element in the stack. stack::top() function is an inbuilt function in C++ STL, which is defined in <stack> header file. top() is used to access the element at the top of the stack container. In a stack, the top element is the element that is inserted at the last or most recently inserted element. Since stacks are last-in first-out containers, the top element is the last element inserted into the stack. This member function effectively calls member back of the underlying container object. No parameter is required.

Remove top element. Removes the element on top of the stack, effectively reducing its size by one. The C++ function std::stack::pop() removes top element from the stack and reduces size of stack by one. This function calls destructor on removed element. The element removed is the latest element inserted into the stack, whose value can be retrieved by calling member stack::top. This calls the removed element's destructor. This member function effectively calls the member function pop_back of the underlying container object.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.


In C++ programming, you have to ask to the user to enter the desired decimal number to convert it into hexadecimal number to print the equivalent value in hexadecimal format




As you can see that the output is missing the value 3, however the for loop 'iterate' though the num value 0 to 6. This is because we have set a condition inside loop in such a way, that


To find 'cube root of any number' we need to find 0.3 power of any number. if you need to find cube root of 27 then calculate 0.3 power of 27, result is 3. So another method for this