Queue container (c + +)

Source: Internet
Author: User
Tags array length

A. The definition of the queue template class is in the <queue> header file.

The queue is very similar to the stack template, and the queue template also needs to define two template parameters, one element type, one container type, the element type is necessary, the container type is optional, and the default is the Dqueue type.

The sample code that defines the queue object is as follows:

queue<int>q1;

queue<double>q2;

The basic operations of the queue are:

1. Queue: As Q.push (x): The x element is connected to the end of the line;

2. Out of line: The first element of the popup queue, such as Q.pop (), does not return the value of the element;

3, access to the first element of the team: such as Q.front ()

4, access to the tail elements, such as q.back ();

5, the number of elements in the access team, such as Q.size ();

Two. Priority queue

In the <queue> header file, a very useful template class Priority_queue (priority queue) is also defined, and the difference between the priority queue and the queue is that the priority queue is not out of line in the queue, but rather in the order of priority of the elements in the queues (the default is the big priority, You can also specify your own precedence by specifying operators).

The Priority_queue template class has three template parameters, element types, container types, and comparison operators. The latter two can be omitted, the default container is a vector, the default operator is less, that is, a small forward row, large back row (out of the queue when the tail of the elements out of the team).

The sample code that defines the Priority_queue object is as follows:

Priority_queue<int >q1;

Priority_queue<pair<int,int> >q2;

Priority_queue<int,vector<int>,greater<int> >q3;//Define a small first-out team

Priority_queue basic operations are the same as the queue

/*(Repair Ranch) Enter a set of data to get the minimum right and*/#include<iostream>#include<cstdio>#include<queue>//Queue Templatesusing namespacestd;//priority_queue<int>q; Priority queue default large first-out teampriority_queue<int, vector<int, greater<int> > Q;//priority Queue definition Small first-out teamintMain () {intN, M; cout<<"input array length n:"; CIN>>N; cout<<"input data elements:";  for(inti =0; I < n; i++) {cin>>m;    Q.push (m); }    intsum =0;  while(Q.size () >1 ) {        intFirst =Q.top ();        Q.pop (); intSecond =Q.top ();        Q.pop (); Sum+ = First +second; Q.push ( First+second); } cout<<"Minimum Cost:"<<sum<<Endl; return 0;}

Queue container (c + +)

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.