[C++][原創(chuàng)]隊(duì)列的簡(jiǎn)單使用-創(chuàng)新互聯(lián)

首先自定義一個(gè)類:

目前創(chuàng)新互聯(lián)已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、孟村網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

TestManager.h

#pragma once
#includeusing namespace std;


class TestManager
{
public:
	TestManager();
	~TestManager();
	int Age=22;
	std::string Name="張三";
	float Weights=12.45;
private:
};

TestManager.cpp

#include "TestManager.h"

TestManager::TestManager()
{

}
TestManager::~TestManager()
{

}

然后隊(duì)列封裝一個(gè)類便于操作:

QueueManager.h

#pragma once
#include//c++標(biāo)準(zhǔn)頭文件,可以使用cout,cin等標(biāo)準(zhǔn)庫(kù)函數(shù) 
#include//使用queue時(shí)需要的頭文件 
#include "TestManager.h"

class QueueManager
{
public:
	QueueManager();
	~QueueManager();
	void Add(TestManager tm);//往隊(duì)列里面添加元素
	TestManager Get();//取出隊(duì)列首個(gè)元素
	int Size();//返回隊(duì)列當(dāng)前元素個(gè)數(shù)
	int MaxSize = 10;//隊(duì)列大保留元素個(gè)數(shù)

private:
	queuedataqueue;//隊(duì)列

};

QueueManager.cpp

#include "QueueManager.h"

QueueManager::QueueManager()
{

}
QueueManager::~QueueManager()
{

}
void QueueManager::Add(TestManager tm)
{
	if (dataqueue.size() >=MaxSize)
	{
		dataqueue.pop();
		
	}
	dataqueue.push(tm);
}

TestManager QueueManager::Get()
{
	if (dataqueue.empty())
	{
		TestManager temp;
		return temp;//返回默認(rèn)的對(duì)象
	}
	else
	{
		return dataqueue.front();
	}
}

int QueueManager::Size()
{
	return dataqueue.size();
}

使用:

main.cpp

#include//c++標(biāo)準(zhǔn)頭文件,可以使用cout,cin等標(biāo)準(zhǔn)庫(kù)函數(shù) 
#include "QueueManager.h"
using namespace std;//命名空間,防止重名給程序帶來(lái)各種隱患,使用cin,cout,stack,map,set,vector,queue時(shí)都要使用
int main() {
	QueueManager que;
	TestManager tm;
	tm.Age = 25;
	tm.Name = "李四";
	tm.Weights = 60.12;
	que.Add(tm);
	std::cout<< que.Size()<

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

文章標(biāo)題:[C++][原創(chuàng)]隊(duì)列的簡(jiǎn)單使用-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://muchs.cn/article28/ddsocp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站排名、App開(kāi)發(fā)面包屑導(dǎo)航、自適應(yīng)網(wǎng)站、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)