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

Learn C++ Language

Queue in C++ Programming Language

Queue in C++ Language
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: • empty • size • front • back • push_back • pop_front The standard container classes deque and list fulfill these requirements. By default, if no container class is specified for a particular queue class instantiation, the standard container deque is used.
Syntax for Queue in C++
template <class T, class Container = deque<T> > class queue;
Type of the elements. Aliased as member type queue::value_type.
Type of the internal underlying container object where the elements are stored. Its value_type shall be T. Aliased as member type queue::container_type.
Member Types
Given below is a list of the queue member types with a short description of the same. value_type: Element type is specified. container_type: Underlying container type is specified. size_type: It specifies the size range of the elements. reference: It is a reference type of a container. const_reference: It is a reference type of a constant container.
Member Functions
With the help of functions, an object or variable can be played with in the field of programming. Queues provide a large number of functions that can be used or embedded in the programs. A list of the same is given below: • (constructor): The function is used for the construction of a queue container. • empty: The function is used to test for the emptiness of a queue. If the queue is empty the function returns true else false. • size: The function returns the size of the queue container, which is a measure of the number of elements stored in the queue. • front: The function is used to access the front element of the queue. The element plays a very important role as all the deletion operations are performed at the front element. • back: The function is used to access the rear element of the queue. The element plays a very important role as all the insertion operations are performed at the rear element. • push: The function is used for the insertion of a new element at the rear end of the queue. • pop: The function is used for the deletion of element; the element in the queue is deleted from the front end. • emplace: The function is used for insertion of new elements in the queue above the current rear element. • swap: The function is used for interchanging the contents of two containers in reference. • relational operators: The non member function specifies the relational operators that are needed for the queues. • uses allocator<queue>: As the name suggests the non member function uses the allocator for the queues.
Non-member overloaded functions
• operator== Tests whether two queues are equal or not. • operator!= Tests whether two queues are equal or not. • operator< Tests whether first queue is less than other or not. • operator<= Tests whether first queue is less than or equal to other or not. • operator> Tests whether first queue is greater than other or not. • operator>= Tests whether first queue is greater than or equal to other or not. • swap Exchanges the contents of two queues.
/* Queue is a data structure designed to operate in FIFO (First in First out) context. In queue elements are inserted from rear end and get removed from front end. Queue class is container adapter. Container is an objects that hold data of same type. Queue can be created from different sequence containers. Container adapters do not support iterators therefore we cannot use them for data manipulation. However they support push() and pop() member functions for data insertion and deletion respectively. */ // CPP code to illustrate // Queue in Standard Template Library (STL) #include <iostream> #include <queue> using namespace std; // Print the queue void showq(queue<int> gq) { queue<int> g = gq; while (!g.empty()) { cout << '\t' << g.front(); g.pop(); } cout << '\n'; } // Driver Code int main() { queue<int> gquiz; gquiz.push(10); gquiz.push(20); gquiz.push(30); cout << "The queue gquiz is : "; showq(gquiz); cout << "\ngquiz.size() : " << gquiz.size(); cout << "\ngquiz.front() : " << gquiz.front(); cout << "\ngquiz.back() : " << gquiz.back(); cout << "\ngquiz.pop() : "; gquiz.pop(); showq(gquiz); return 0; }

A recursive function that returns true if there is an articulation point in a graph, otherwise returns false. If graph is Biconnected returns true, otherwise returns false. If this Graph is

The statements within the while loop would keep on getting executed till the "condition" being tested remains "true". When condition becomes false, the control passes to the first

This function reads n lines from a file. Returns the amount of bytes read. Try to open txt file. Try to locate the starting line. Set the starting position. Read the lines. Close the file. Return

If graph has no "Odd Degree Vertex", there is at least one "Eulerian Circuit". If graph as two vertices with odd degree, there is no Eulerian Circuit but at least one Eulerian Path. If graph