C++ 提高教程 STL queue容器(队列)
Posted 行码阁119
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 提高教程 STL queue容器(队列)相关的知识,希望对你有一定的参考价值。
# include<iostream>
# include<queue>
# include<string>
# include<ctime>
using namespace std;
class Person
{
public:
Person(int age, string name)
{
this->m_Age = age;
this->m_Name = name;
}
int m_Age;
string m_Name;
};
void setPersonInf(queue<Person> &p)
{
string name = "abcde";
int age;
for (int i = 0; i < 5; i++)
{
string a = "平民";
a += name[i];
int b = rand() % 100;
Person p1(b,a);
p.push(p1);
}
}
void test02()
{
queue<Person> q;
setPersonInf(q);
while (!q.empty())
{
cout << "队尾元素为:" << endl;
cout << "姓名:" << q.back().m_Name << "年龄:" << q.back().m_Age << endl;
cout << "队头元素为:" << endl;
cout << "姓名:" << q.front().m_Name << "年龄:" << q.front().m_Age << endl;
q.pop();
}
}
void test01()
{
queue<int>q;
q.push(10);
q.push(20);
q.push(30);
q.push(40);
while ((!q.empty()))
{
cout << "队头元素为:" << q.front() << endl;
cout << "队尾元素为:" << q.back() << endl;
//q.pop();
q.pop();
}
cout << "stack的大小" << q.size() << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
以上是关于C++ 提高教程 STL queue容器(队列)的主要内容,如果未能解决你的问题,请参考以下文章
[ C++ ] STL priority_queue(优先级队列)使用及其底层模拟实现,容器适配器,deque(双端队列)原理了解
小白学习C++ 教程二十一C++ 中的STL容器Arrays和vector