Queue in Standard Template Library (STL)
Queues are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.
The functions supported by queue are :
- empty() – Returns whether the queue is empty.
- size() – Returns the size of the queue.
- queue::swap() in C++ STL: Exchange the contents of two queues but the queues must be of same type, although sizes may differ.
- queue::emplace() in C++ STL: Insert a new element into the queue container, the new element is added to the end of the queue.
- queue::front() and queue::back() in C++ STL– front() function returns a reference to the first element of the queue. back() function returns a reference to the last element of the queue.
- push(g) and pop() – push() function adds the element ‘g’ at the end of the queue. pop() function deletes the first element of the queue.
// CPP code to illustrate // Queue in Standard Template Library (STL) #include <iostream> #include <queue> using namespace std; void showq(queue <int> gq) { queue <int> g = gq; while (!g.empty()) { cout << '\t' << g.front(); g.pop(); } cout << '\n'; } 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; } |
chevron_right
filter_none
Output:
The queue gquiz is : 10 20 30 gquiz.size() : 3 gquiz.front() : 10 gquiz.back() : 30 gquiz.pop() : 20 30
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Priority Queue in C++ Standard Template Library (STL)
- The C++ Standard Template Library (STL)
- Set in C++ Standard Template Library (STL)
- Map in C++ Standard Template Library (STL)
- Pair in C++ Standard Template Library (STL)
- List in C++ Standard Template Library (STL)
- Deque in C++ Standard Template Library (STL)
- Multiset in C++ Standard Template Library (STL)
- Multimap in C++ Standard Template Library (STL)
- Sort in C++ Standard Template Library (STL)
- Binary Search in C++ Standard Template Library (STL)
- queue::front() and queue::back() in C++ STL
- queue::empty() and queue::size() in C++ STL
- queue::push() and queue::pop() in C++ STL
- is_unsigned template in C++
Improved By : Rohit kumar 37



