C + + Primer learning notes _11_ Standard Template Library _stack, queue queues container and Priority_queue priority queue container
1. Stack stacks
Stack stacks are a linear table of last-in-first-out (Out,lifo), where both insert and delete elements can only be performed at one end of the table. One end of the inserted element is called the top of the stack, and the other end is called the bottom. The insert element is called into the stack (Push), and the delete element is called out of the stack (POP). is a stack
Stacks only provide several methods, such as stack, stack, top element access and judge whether it is empty. Use the push () method to stack elements, use the Pop () method to stack, use the top () method to access the top element of the stack, use the empty () method to determine if the stack is empty, or return true if NULL, or FALSE. Alternatively, you can use size () method returns a few elements of the current stack.
#include <iostream> #include <stack> using namespace std; int main (int argc, char* argv[]) { stack<int> s; S.push (1); S.push (2); S.push (3); S.push (9); cout << "Stack top elements are:" << s.top () << Endl; cout << "Number of stack elements:" << s.size () << Endl; while (S.empty () = True) { cout << "stack top elements are:" << s.top () << Endl; S.pop (); Out Stack } if (S.empty ()) cout << "Empty" << Endl; return 0;}
Operation Result:
Stack top element is: 9
Number of stack elements: 4
Stack top element is: 9
Stack top element is: 3
Stack top element is: 2
Stack top element is: 1
Empty
2. Queue container
Queue container queues are a linear storage table of FIFO (Firstin first Out,fifo), where elements are inserted only at the end of a team, and elements are deleted only at the head of the team. Figure 2-10 is a queue container data structure.
The queue queues have a queue push (), an out-of-band pop (), read the first element of the front (), read the tail element back (), determine whether the queue is empty (), and the number of current elements in the queues size () several methods.
#include <iostream> #include <queue> using namespace std; int main (int argc, char* argv[]) { queue<int> num; Num.push (1); Num.push (2); Num.push (3); Num.push (9); cout << num.size () << Endl; cout << num.empty () << Endl; cout << Num.front () << Endl; cout << num.back () << Endl; while (!num.empty ()) { cout << num.front () << ""; Num.pop (); } cout << Endl; return 0;}
Operation Result:
4
0
1
9
1 2 3 9
3. Priority Queue Container Priority_queue
Priority Queue container priority_queue As with queues, you can only insert elements from the end of a queue and delete elements from the first team. But it has a feature that the largest element in the queue is always at the head of the team, so when the team is out, it does not follow the FIFO principle, but the largest element in the current queue. This is similar to the order in which the elements in the queue are sorted from large to small. The comparison rules for elements are sorted by the value of the element from large to small by default, and you can, of course, overload the < operator to redefine the comparison rule. You can also override the "()" to redefine the comparison rule.
The header file for the declaration is also #include<queue>.
3.1 How to use priority queues
The priority queue contains the queue push (), the Out-of-band Pop (), the reading of the first element top (), the determination of whether the queue is empty (), and the number of current elements in the queues size () several methods.
#include <iostream> #include <queue> using namespace std; int main (int argc, char* argv[]) { priority_queue<int> num; Num.push (1); Num.push (2); Num.push (3); Num.push (9); cout << num.size () << Endl; cout << num.empty () << Endl; cout << num.top () << Endl; while (!num.empty ()) { cout << num.top () << ""; Num.pop (); } cout << Endl; return 0;}
Operation Result:
4
0
9
9 3 2 1
3.2 Overloading the < operator to define the priority level
If the element type of the priority queue is a struct, you can modify the priority queue precedence by overloading the "<" operator in the struct.
#include <iostream> #include <queue> #include <string> using namespace std; struct info{ string name; float score; BOOL operator < (const Info &a) const { return A.score < score; }}; int main (int argc, char* argv[]) { priority_queue<info> student; Info info; Info.name = "Jack"; Info.score = 68.5; Student.push (info); Info.name = "Bomi"; Info.score = 18.5; Student.push (info); Info.name = "Peti"; Info.score =; Student.push (info); while (!student.empty ()) { cout << student.top (). Name << ":" <<student.top (). Score < < Endl; Student.pop (); } return 0;}
Operation Result:
bomi:18.5
jack:68.5
Peti:90
3.3 Overloaded "()" operator to define precedence
If the element of the priority queue is not a struct type, you can define the precedence by overloading the () operator. Of course, an element is a struct type, and you can define precedence by overloading the () operator, rather than overloading the "<" operator in the struct.
#include <iostream> #include <queue> #include <vector> using namespace std; struct mycomp{ booloperator () (const int &A, const int &b) { return a > B; }}; int main (int ar GC, char* argv[]) { //define priority queue, element type MyComp, explicit description of internal structural vector Priority_queue<int, vector<int>, MyComp > num; Num.push (1); Num.push (9); Num.push (2); Num.push (+); while (!num.empty ()) { cout << num.top () << ""; Num.pop (); } cout << Endl; return 0;}
Operation result;
1 2 9 30
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Primer learning notes _11_ Standard Template Library _stack, queue queues container and Priority_queue priority queue container