C/C++教程

C++ queue练习

本文主要是介绍C++ queue练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、概述

  案例:c++测试queue的用法

二、代码

#include <iostream>
#include <queue>
#include <string>

using namespace std;

class Person{
public:
	Person(string name,int age){
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	string m_age;
};

void test(){
	queue<Person> q;
	Person p1("tony",30);
	Person p2("luoluoyang",3);
	Person p3("kiki",18);

	//入队列
	q.push(p1);
	q.push(p2);
	q.push(p3);

	cout <<"size:"<<q.size()<<endl;

	//遍历队列集合
	while(!q.empty()){
		Person pFront = q.front();
		Person pBack = q.back();
		cout << "begin element name:"<<pFront.m_name<< " age:"<< pFront.m_age<<endl;
		cout << "end element name: "<<pBack.m_name<< " age: "<<pBack.m_age<<endl; 

		//出队列
		q.pop();
	}
	cout <<"size: "<<q.size()<<endl;
}
/**
 * 
 * 队列queue测试。先进先出集合
 * */
int main(int argc, char const *argv[])
{
	test();
	return 0;
}

  

 

这篇关于C++ queue练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!