C/C++ STL/Queue and Stack

2018/07/29 C/C++

C/C++程序语言设计 STL/Queue and Stack。

MAC

队列应用

#include <iostream>
#include <queue>
#include <string>
using namespace std;

void test_empty(){
    queue<int> myqueue;
    int sum = 0;
    for(int i=1;i<=10;i++){
        myqueue.push(i);
    }
    while(!myqueue.empty()){
        sum += myqueue.front();
        myqueue.pop();
    }
    cout<<"total: "<<sum<<endl;
}
//total: 55

void test_pop(){
    queue<int> myqueue;
    int myint;
    cout<<"Please enter some integers (enter 0 to end):\n";
    do{
        cin>>myint;
        myqueue.push(myint);
    }while(myint);
    cout<<"myqueue contains: ";
    while(!myqueue.empty()){
        cout<<" "<<myqueue.front();
        myqueue.pop();
    }
}
/********
Please enter some integers (enter 0 to end):
512
605
420
517
532
0
myqueue contains:  512 605 420 517 532 0
********/

void test_size(){
    queue<int> myints;
    cout<<"0. size: "<<myints.size()<<endl;
    for(int i=0;i<5;i++){
        myints.push(i);
    }
    cout<<"1. size: "<<myints.size()<<endl;
    myints.pop();
    cout<<"2. size: "<<myints.size()<<endl;
}
/****
0. size: 0
1. size: 5
2. size: 4
****/

int main(){
    test_empty();
    cout<<"\n***********************************************\n";
    test_size();
    cout<<"\n***********************************************\n";
    test_pop();
    cout<<"\n***********************************************\n";

    queue<string> q;
    // insert three elements into the queue
    q.push("These ");
    q.push("are ");
    q.push("more than ");
    //cout << "number of elements in the queue: " << q.size()<< endl;

    // read and print two elements from the queue
    cout << q.front();
    q.pop();
    cout << q.front();
    q.pop();
    //cout << "number of elements in the queue: " << q.size()<< endl;

    // insert two new elements
    q.push("four ");
    q.push("words!");
    //cout << "\nnumber of elements in the queue: " << q.size()<< endl;
    // skip one element
    q.pop();

    // read and print two elements
    cout << q.front();
    q.pop();
    cout << q.front() << endl;
    q.pop();

    // print number of elements in the queue
    cout << "number of elements in the queue: " << q.size()<< endl;
}
/*******
*运行结果:
total: 55
***********************************************
0. size: 0
1. size: 5
2. size: 4
***********************************************
Please enter some integers (enter 0 to end):
512
605
420
517
532
0
myqueue contains:  512 605 420 517 532 0
***********************************************
These are four words!
number of elements in the queue: 0
Process returned 0 (0x0)   execution time : 33.512 s
Press any key to continue.
*******/

优先队列应用

#include <iostream>
#include <queue>
#include <string>
using namespace std;

void test_empty(){
    priority_queue<int> mypq;
    int sum = 0;
    for(int i=1;i<=100;i++){
        mypq.push(i);
    }
    while(!mypq.empty()){
        sum += mypq.top();
        mypq.pop();
    }
    cout << "total: " << sum << endl;
}
 
void test_pop(){
    priority_queue<int> mypq;
    mypq.push(30);
    mypq.push(100);
    mypq.push(25);
    mypq.push(40);
    cout << "Popping out elements...";
    while(!mypq.empty()){
        cout << " " << mypq.top();
        mypq.pop();
    }
    cout << endl;
}

void test_top(){
    priority_queue<string> mypq;
    mypq.push("how");
    mypq.push("are");
    mypq.push("you");
    cout << "mypq.top() is now:--->>>   " << mypq.top() << endl;
}

int main(){
    test_empty();
    cout<<"\n***********************************************\n";
    test_pop();
    cout<<"\n***********************************************\n";
    test_top();
    cout<<"\n***********************************************\n";
    priority_queue<float> q;

    // insert three elements into the priority queue
    q.push(66.6);
    q.push(22.2);
    q.push(44.4);

    // read and print two elements
    cout << q.top() << ' ';
    q.pop();
    cout << q.top() << endl;
    q.pop();

    // insert three more elements
    q.push(11.1);
    q.push(55.5);
    q.push(33.3);

    // skip one element
    q.pop();

    // pop and print remaining elements
    while (!q.empty()){
        cout << q.top() << ' ';
        q.pop();
    }
    cout << endl;
}
/******************
运行结果:
total: 5050
***********************************************
Popping out elements... 100 40 30 25
***********************************************
mypq.top() is now:--->>>   you
***********************************************
66.6 44.4
33.3 22.2 11.1
Process returned 0 (0x0)   execution time : 0.055 s
Press any key to continue.
********************/

最小优先队列

#include <iostream>
#include <queue>
#include <vector>
#include <functional>
using namespace std;
 
int main(){
    priority_queue<int, vector<int>, greater<int>> pque;
    pque.push(3);
    pque.push(5);
    pque.push(1);
    pque.push(4);
    while(!pque.empty()){
        cout<<pque.top()<<" ";
        pque.pop();
    }
    cout<<endl;
    return 0;
}
/******************
运行结果:
1 3 4 5
******************/

结构体最小优先队列

#include <iostream>
#include <queue>
#include <string>
using namespace std;

struct package{
    int id;
    string data;
};

bool operator<(package a, package b) {
    return a.id > b.id;
}

int main() {
    priority_queue<package> tmp;
    tmp.push({3,"a"});
    tmp.push({2,"b"});
    int size = tmp.size();
    while(size--) {
        cout << tmp.top().id << " " << tmp.top().data <<endl;
        tmp.pop();
    }
}

/******************
运行结果:
2 b
3 a
******************/

栈式结构应用

#include <iostream>
#include <stack>
#include <vector>
#include <string>
using namespace std;

int main() {
    stack<int,vector<int>> obi;
    for(int i=0;i<10;i++){
        obi.push(i);
    }
    while(obi.size()>0){
        int x = obi.top();
        cout << x << " ";
        obi.pop();
    }
    cout << endl;
    stack<string,vector<string>>obs;
    for(char c='A';c<='Z';c++){
        string s(1, c);
        obs.push(s);
    }
    while(obs.size()){
        string str = obs.top();
        cout << str << " ";
        obs.pop();
    }
    cout<<endl;
    return 0;
}

/******************
运行结果:
9 8 7 6 5 4 3 2 1 0
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
******************/

Search

    Table of Contents