zoukankan      html  css  js  c++  java
  • queue容器

    一.摘要

      队列,先进先出,后进后出~

    二.函数原型

    构造函数:

    queue<T> que; //queue采用模板类实现,queue对象的默认构造形式
    queue(const queue &que); //拷贝构造函数

    赋值操作:

    queue& operator=(const queue &que); //重载等号操作符

    数据存取:

    push(elem); //往队尾添加元素
    pop(); //从队头移除第一个元素
    back(); //返回最后一个元素
    front(); //返回第一个元素

    大小操作:

    empty(); //判断堆栈是否为空
    size(); //返回栈的大小

    三.示例代码

    代码:

    /*quequ容器*/
    #include<iostream>
    #include<queue>
    #include<string>
    using namespace std;
    class Person {
    public:
        Person(string t_name,int t_age) {
            this->name = t_name;
            this->age = t_age;
        }
        string name;
        int age;
    };
    void printQueue(queue<Person>&q) {
        while (!q.empty()) {//empty(); //判断堆栈是否为空
            cout << q.front().name<<""<<q.front().age<<""<<endl;//front(); //返回第一个元素
            q.pop();//pop(); //从队头移除第一个元素
        }
    }
    int main() {
        queue<Person>q1;//queue<T> que; //queue采用模板类实现,queue对象的默认构造形式
        Person p1("mzb", 20);
        Person p2("cxy", 19);
        Person p3("cxt", 20);
        Person p4("zyf", 19);
        Person p5("tjc", 19);
        q1.push(p1), q1.push(p2), q1.push(p3), q1.push(p4), q1.push(p5);//push(elem); //往队尾添加元素
        cout << "q1最后一个元素:" << q1.back().name << "" << q1.back().age << "" << endl;//back(); //返回最后一个元素
        queue<Person>q2(q1);//queue(const queue &que); //拷贝构造函数
        queue<Person>q3;
        q3 = q2;//queue& operator=(const queue &que); //重载等号操作符
        cout << "q1:" << endl;
        printQueue(q1);
        cout << "q2:" << endl;
        printQueue(q2);
        cout << "q3:" << endl;
        cout << "出队前q3大小:" << q3.size() << endl;
        printQueue(q3);
        cout << "出队后q3大小:" << q3.size() << endl;
        system("pause");
        return 0;
    }
    queue示例代码

    结果:

     三.总结

      很容易掌握,操作跟 stack容器 类似~

  • 相关阅读:
    算法----(1)冒泡排序
    淘宝爬虫
    爬虫_豆瓣电影top250 (正则表达式)
    爬虫_猫眼电影top100(正则表达式)
    Android 简单调用摄像头
    Android 简单天气预报
    思维模型
    This view is not constrained, it only has designtime positions, so it will jump to (0,0) unless you
    Android studio preview界面无法预览,报错render problem
    Android studio 3.1.2报错,no target device found
  • 原文地址:https://www.cnblogs.com/chasemeng/p/12862606.html
Copyright © 2011-2022 走看看