• 队列的定义
    队列是一种容器适配器,专门设计用于在FIFO上下文(先进先出)中操作,其中元素插入容器的一端并从另一端提取。 队列被实现为容器适配器,它是使用特定容器类的封装对象作为其基础容器的类,提供一组特定的成员函数来访问其元素。 元素被推入特定容器的“背部”并从其“前”弹出。
  • 成员函数

Member functions


Non-member function overloads


Non-member class specializations

  • 演示程序
#include<iostream>
#include<queue>
#include<string>
using namespace std;

int main()
{
	queue<int> myqueue;
	int sum=0;
	for (int i = 1; i <= 10; i++) myqueue.push(i);//i元素入队
	cout << myqueue.back() << endl;//输出队尾元素
	while (!myqueue.empty())
	{
		sum += myqueue.front();//取队头元素
		myqueue.pop();//队头元素出队
	}
	cout << "total: " << sum << '\n';
	

	//size用法
	queue<int> myints;
	cout << "0. size: " << myints.size() << '\n';
	for (int i = 0; i < 5; i++) myints.push(i);
	cout << "1. size: " << myints.size() << '\n';
	myints.pop();
	cout << "2. size: " << myints.size() << '\n';

	//emplace用法
	queue<string> myqueue1;
	myqueue1.emplace("First sentence");
	myqueue1.emplace("Second sentence");
	cout << "myqueue contains:\n";
	while (!myqueue1.empty())
	{
		cout << myqueue1.front() << '\n';
		myqueue1.pop();
	}

	//swap用于交换两个队列内的元素
	queue<int> foo, bar;
	foo.push(10); foo.push(20); foo.push(30);
	bar.push(111); bar.push(222);
	foo.swap(bar);
	cout << "size of foo: " << foo.size() << '\n';
	cout << "size of bar: " << bar.size() << '\n';

	system("pause");
	return 0;
}
  • 运行结果
10
total: 55
 - size: 0
 - size: 5
 - size: 4
myqueue contains:
First sentence
Second sentence
size of foo: 2
size of bar: 3
Press any key to continue . . .
  • 进阶用法
//结构体型队列
#include <iostream>
#include <queue>
using namespace std;
#pragma warning(disable:4996)
typedef struct//构造一个结构体用于记录位置
{
    int x, y, z;
}nodes;
int att[1287][129][61];//存三维数组的各个值
bool visit[1287][129][61];//用于判断某个位置是否被访问
int m, n, l, t;
bool judge(int x, int y, int z)//用于判断是否应该访问(x,y,z)
{
    if (x<0 || x>=m || y<0 || y>=n || z<0 || z>= l)return false;
    if (att[x][y][z] == 0 || visit[x][y][z] == 1)return false;
    return true;
}
int fx[6] = { 1,0,0,-1,0,0 };
int fy[6] = { 0,1,0,0,-1,0 };
int fz[6] = { 0,0,1,0,0,-1 };
int bfs(int x, int y, int z)
{
    int inc = 0;
    int tempx, tempy, tempz;
    nodes p;
    p.x = x; p.y = y; p.z = z;
    queue<nodes> q;
    q.push(p);
    nodes temp;
    visit[x][y][z] = 1;
    while (!q.empty())
    {
        temp = q.front();
        q.pop();
        ++inc;
        for (int i = 0; i < 6; ++i)
        {
            tempx = temp.x + fx[i];
            tempy = temp.y + fy[i];
            tempz = temp.z + fz[i];
            if (judge(tempx, tempy, tempz))
            {
                visit[tempx][tempy][tempz] = 1;
                p.x = tempx; p.y = tempy; p.z = tempz;
                q.push(p);
            }
        }
    }
    if (inc >= t)
        return inc;
    else
        return 0;
}
int main()
{
    scanf("%d %d %d %d", &m, &n, &l, &t);
    int i, j, k;
    for (k = 0; k < l; ++k)
        for (i = 0; i < m; ++i)
            for (j = 0; j < n; ++j)
                scanf("%d", &att[i][j][k]);
    int result = 0;
    for (k = 0; k < l; ++k)
        for (i = 0; i < m; ++i)
            for (j = 0; j < n; ++j)
                if(att[i][j][k]==1&&visit[i][j][k]==0)
                    result += bfs(i, j, k);
    printf("%d", result);
    system("pause");
    return 0;
}

在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐