Special containers, also known as container adapters (Container Adapter), transform standard STL containers to meet special requirements.
Stack stacks
When using a stack, include the header file <stack>
- Push () pushes an element inside the stack
- Pop () removes the next element from the stack, but does not return it
- Top () returns the next element within the stack, but does not remove it.
If there are no elements inside the stack, top () and pop () cause ambiguous behavior, either size () or empty () can be used to check if the container is empty.
Queue queues
The queue implements a FIFO first-out structure and is a typical data buffering construct. Need to include header files when using <queue>
- Push () moves an element into the queue
- Front () returns the first element in a queue without removing the element
- Back () returns the last element in the queue without removing the element
- Pop () removes an element from the queue, but does not return it
If there are no elements in the queue, front (), Back (), and pop () will result in ambiguous behavior, either size () or empty () to check if the container is empty.
Priority Queue Priorities
The elements within the priority queue are sorted according to their priorities, with header files <queue>
- Push () puts an element into the priority queue
- Top () returns the first element in the priority queue, but does not remove
- Pop () removes an element, but does not return
If there are no elements in the priority queue, top () and pop () cause ambiguous behavior, either size () or empty () can be used to check if the container is empty.
#include <iostream>#include<queue>using namespacestd;intMain () {priority_queue<float>Q; Q.push (66.6); Q.push (22.2); Q.push (44.4); cout<< q.top () <<Endl; Q.pop (); cout<< q.top () <<Endl; Q.pop (); Q.push (11.1); Q.push (55.5); Q.push (33.3); while(!Q.empty ()) {cout<< q.top () <<Endl; Q.pop (); } return 0;}
View Code
"C + + standard library" special container