"C + + standard library" special container

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.