STL之queue

queue即队列,一种先进先出的数据结构。

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

int main()
{
    //构造
    queue<int> q; //一般空参构造

    //入队
    q.push(2);
    q.push(6);
    q.push(8);
    cout << q.size() << endl; //size:3
    //取队尾
    cout << q.back() << endl; //输出:8
    //queue不能遍历,只能一个一个出队
    while (!q.empty()) { //输出2 6 8 先进先出
        cout << q.front() << ' '; //取队首,不会出队
        q.pop(); //出队,无返回值
    }
    cout << endl << q.size() << endl; //size:0

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/love-ziji/p/12651651.html
今日推荐