C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ queueIn computer science we go for working on a large variety of programs. Each of them has their own domain and utility. Based on the purpose and environment of the program creation, we have a large number of data structures available to choose from. One of them is 'queues. Before discussing about this data type let us take a look at its syntax. Syntaxtemplate<class T, class Container = deque<T> > class queue; This data structure works on the FIFO technique, where FIFO stands for First In First Out. The element which was first inserted will be extracted at the first and so on. There is an element called as 'front' which is the element at the front most position or say the first position, also there is an element called as 'rear' which is the element at the last position. In normal queues insertion of elements take at the rear end and the deletion is done from the front. Queues in the application areas are implied as the container adaptors. The containers should have a support for the following list of operations:
Template ParametersT: The argument specifies the type of the element which the container adaptor will be holding. Container: The argument specifies an internal object of container where the elements of the queues are held. Member TypesGiven below is a list of the queue member types with a short description of the same.
FunctionsWith 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:
Example: A simple program to show the use of basic queue functions.#include <iostream> #include <queue> using namespace std; void showsg(queue <int> sg) { queue <int> ss = sg; while (!ss.empty()) { cout << '\t' << ss.front(); ss.pop(); } cout << '\n'; } int main() { queue <int> fquiz; fquiz.push(10); fquiz.push(20); fquiz.push(30); cout << "The queue fquiz is : "; showsg(fquiz); cout << "\nfquiz.size() : " << fquiz.size(); cout << "\nfquiz.front() : " << fquiz.front(); cout << "\nfquiz.back() : " << fquiz.back(); cout << "\nfquiz.pop() : "; fquiz.pop(); showsg(fquiz); return 0; } Output: The queue fquiz is : 10 20 30 fquiz.size() : 3 fquiz.front() : 10 fquiz.back() : 30 fquiz.pop() : 20 30
Next TopicC++ Queue
|