C++//queue 队列 容器 先进先出 只有队头 队尾能被外界访问 因此不允许有遍历行为
1 //queue 队列 容器 先进先出 只有队头 队尾能被外界访问 因此不允许有遍历行为 2 3 4 #include<iostream> 5 #include<queue> 6 #include<string> 7 8 using namespace std; 9 10 class Person 11 { 12 public: 13 Person(string name, int age) 14 { 15 this->m_Name = name; 16 this->m_Age = age; 17 } 18 19 string m_Name; 20 int m_Age; 21 }; 22 23 void test01() 24 { 25 26 //创建队列 27 queue<Person>q; 28 29 30 //准备数据 31 Person p1("张三",30); 32 Person p2("李四",20); 33 Person p3("王五",60); 34 Person p4("张流", 50); 35 36 37 //入队 38 q.push(p1); 39 q.push(p2); 40 q.push(p3); 41 q.push(p4); 42 cout << "队列大小:" << q.size() << endl;; 43 44 //判断只要队列不为空,查看对头,查看队尾,出队 45 while (!q.empty()) 46 { 47 //查看对头 48 cout << "队头元素-----姓名: " << q.front().m_Name << " 年龄:" << q.front().m_Age << endl; 49 50 //查看队尾 51 cout << "队尾元素---姓名:" << q.back().m_Name << " 年龄:" << q.back().m_Age<< endl; 52 53 54 //出队 55 q.pop(); 56 } 57 cout << "队列大小:" << q.size(); 58 } 59 60 61 62 int main() 63 { 64 test01(); 65 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15143196.html