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容器 类似~

  • 相关阅读:
    sharepoint2013保存当前输入的列表
    网站模板的下载和使用
    sharepoint获取是否为输入域用户SharePoint PeopleEditor 控件的使用
    sharepoint指定的人可以看到列表项
    js隐藏显示div
    如何为同一IE浏览器中打开多个页面
    CDC相关知识点总结
    find 命令使用总结
    find 命令search使用
    verilog behavioral modeling --loop statement
  • 原文地址:https://www.cnblogs.com/chasemeng/p/12862606.html
Copyright © 2011-2022 走看看