队列(c++容器queue实现)

#include<iostream>
#include<queue>//队列容器
#include<string>
using namespace std;

class person{
public:
    person(string name,int age){
        m_name=name;
        m_age=age;
    }
    string m_name;
    int m_age;
};

int main(){
    queue<person>q;//默认构造
    
    person p1("张三",13);
    person p2("李四",14);
    person p3("王五",15);
    person p4("刘傻子",16);

    q.push(p1);//往容器中插入数据 push往队尾添加元素
    q.push(p2);
    q.push(p3);
    q.push(p4);

    q.pop();//移除队头元素

    //queue<person>q2=q;//容器给容器赋值

    cout<<"队头名字为"<<q.front().m_name<<"队头年龄为"<<q.front().m_age<<endl;//返回队头元素
    cout<<"队尾名字为"<<q.back().m_name<<"队尾年龄为"<<q.back().m_age<<endl;//返回队尾元素

    cout<<"队列是否为空"<<q.empty()<<endl;
    cout<<"队列的大小为"<<q.size()<<endl;
    return 0;
}

 

posted @ 2021-01-27 17:53  大耿2844  阅读(225)  评论(0)    收藏  举报