- 队列的定义
队列是一种容器适配器,专门设计用于在FIFO上下文(先进先出)中操作,其中元素插入容器的一端并从另一端提取。 队列被实现为容器适配器,它是使用特定容器类的封装对象作为其基础容器的类,提供一组特定的成员函数来访问其元素。 元素被推入特定容器的“背部”并从其“前”弹出。
- 成员函数
Member functions
-
(constructor)
-
Construct queue
(public member function )
-
empty
-
Test whether container is empty
(public member function )
-
size
-
Return size
(public member function )
-
front
-
Access next element
(public member function )
-
back
-
Access last element
(public member function )
-
push
-
Insert element
(public member function )
-
emplace
-
Construct and insert element
(public member function )
-
pop
-
Remove next element
(public member function )
-
swap
-
Swap contents
(public member function )
Non-member function overloads
-
relational operators
-
Relational operators for queue
(function )
-
swap (queue)
-
Exchange contents of queues
(public member function )
Non-member class specializations
-
uses_allocator<queue>
-
Uses allocator for queue
(class template )
#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);
cout << myqueue.back() << endl;
while (!myqueue.empty())
{
sum += myqueue.front();
myqueue.pop();
}
cout << "total: " << sum << '\n';
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';
queue<string> myqueue1;
myqueue1.emplace("First sentence");
myqueue1.emplace("Second sentence");
cout << "myqueue contains:\n";
while (!myqueue1.empty())
{
cout << myqueue1.front() << '\n';
myqueue1.pop();
}
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)
{
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;
}