C++ Programming Code Examples
C++ > Visual C++ 5.0 Standard C++ Library Code Examples
Queue pop - Removes an element from the top of the queue
Queue pop - Removes an element from the top of the queue
pop
Header
<queue>
void pop()
Removes an element from the top of the queue. If you call pop on an empty queue the results are undefined.
Sample
//Compiler options: /GX
#include <queue>
#include <iostream>
int main()
{
std::queue<int> qi ; //Constructs an empty queue, uses deque as default container.
int i ;
std::queue<int>::allocator_type a1 = qi.get_allocator() ;
std::cout << "call qi.empty()" << std::endl ;
if (qi.empty())
{
std::cout << "queue is empty" << std::endl ;
}
else
{
std::cout << "queue contains some elements" << std::endl ;
}
std::cout << "qi.size() = " << qi.size() << std::endl ;
std::cout << "Push Values on qi = " ;
for(i = 0; i < 10; i++)
{
std::cout << i << ", " ;
qi.push(i) ;
}
std::cout << std::endl ;
std::cout << "qi.size() = " << qi.size() << std::endl ;
std::cout << "Pop Values from qi = " ;
while (!qi.empty())
{
std::cout << qi.front() << ", " ;
qi.pop() ;
}
std::cout << std::endl ;
return 0 ;
}
Program Output
call qi.empty()
queue is empty
qi.size() = 0
Push Values on qi = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
qi.size() = 10
Pop Values from qi = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
Return size. Returns the number of elements in the queue. This member function effectively calls member size of the underlying container object. The number of elements in the queue is an actual representation of the size, and the size value is given by this function. size() function is used to return the size of the list container or the number of elements in the list container.
Test whether container is empty. Returns whether the queue is empty: i.e. whether its size is zero. This member function effectively calls member empty of the underlying container object. Sometimes before actually starting the work with the individual elements of the containers, it is more feasible to look up if the container is empty, so this function finds its usage in such cases. queue::empty() is an inbuilt function in C++ STL which is declared in header file. queue::empty() is used to check whether the associated queue container is empty or not. This function returns either true or false, if the queue is empty (size is 0) then the function returns true, else if the queue is having some value then it will return false.
FIFO queue. queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other. queues are implemented as containers 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 into the "back" of the specific container and popped from its "front". The underlying container may be one of the standard container class template or some other specifically designed container class. This underlying container shall support at least the following operations:
Remove next element. Removes the next element in the queue, effectively reducing its size by one. The element removed is the "oldest" element in the queue whose value can be retrieved by calling member queue::front. This calls the removed element's destructor. This member function effectively calls the member function pop_front of the underlying container object. C++ Queue pop() function is used for removing the topmost element of the queue. The function is implied only for deletion of elements.
#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.
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
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.
Access next element. Returns a reference to the next element in the queue. The next element is the "oldest" element in the queue and the same element that is popped out from the queue when queue::pop is called. This member function effectively calls member front of the underlying container object. In C++ STL, Queue is a type of container that follows FIFO (First-in-First-Out) elements arrangement i.e. the elements which insert first will be removed first. In queue, elements are inserted at one end known as "back" and are deleted from another end known as "front". The function front() returns the reference to the first element in the queue i.e. the oldest element in the queue, so it is used to get the first element from the front of the list of a queue.
Inserts a new element at the end of the queue, after its current last element. The content of this new element is initialized to val. This member function effectively calls the member function push_back of the underlying container object. In C++ STL, Queue is a type of container that follows FIFO (First-in-First-Out) elements arrangement i.e. the elements which insert first will be removed first. In queue, elements are inserted at one end known as "back" and are deleted from another end known as "front". In the Data Structure, "push" is an operation to insert an element in any container, "pop" is an operation to remove an element from the container.
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
Linearly traverse the data array. Keep track of the "Smallest Number". Simultaneously keep updating the second smallest number also. A function to calculate second. If array element
In this example, if...else statement is used to check whether a number entered by the user is "even or odd". Integers which are perfectly "divisible by 2" are called even numbers. And
Program calculates the standard deviation of 10 data using arrays. This program calculates the 'standard deviation' of a individual series using arrays. calculate "Standard Deviation",
Program should display every prime number between 'range' and at the end total number of "prime numbers" found in range. Separate function which receives "two parameters" for