C ++ STL中的queue :: push()和queue :: pop()

In C++ STL, Queue is a type of container that follows FIFO (First-in-First-Out) elements arrangement i.e. the elements which insert first will be removed first. In queue, elements are inserted at one end known as "back" and are deleted from another end known as "front".

在C ++ STL中,队列是遵循FIFO(先进先出)元素排列的一种容器,即,首先插入的元素将被首先删除。 在队列中,元素被插入称为“ back”的一端,并从称为“ front”的另一端删除。

Note: In the Data Structure, "push" is an operation to insert an element in any container, "pop" is an operation to remove an element from the container.

注意:数据结构中“ push”是在任何容器中插入元素的操作, “ pop”是从容器中删除元素的操作。

1)C ++ STL队列:: push()函数 (1) C++ STL queue::push()function)

push() inserts an element to queue at the back. After executing this function, element inserted in the queue and its size increased by 1.

push()在后面插入一个要排队的元素。 执行此功能后,插入队列中的元素及其大小增加了1。

Syntax:

句法:

    queue_name.push(element);

2)C ++ STL queue :: pop()函数 (2) C++ STL queue::pop() function)

pop() removes an element from the front of the queue. After executing this function, the oldest element removed from the queue and its size decreased by 1.

pop()从队列的开头删除一个元素。 执行此功能后,最旧的元素从队列中删除,其大小减小了1。

Syntax:

句法:

    queue_name.pop();

Program:

程序:

// cpp program for queue implementation 
// Example of push() and pop()
#include <iostream>
#include <queue>
using namespace std;

//function to print the queue 
void printQueue(queue<int> q)
{
	// printing content of queue
	while (!q.empty()) {
		cout<<" "<<q.front();
		q.pop();
	}
	cout<<endl;
}

//Main finction
int main()
{
	// declaring an empty queue
	queue<int> Q;
	
	//inserting elements 
	Q.push(10);
	Q.push(20);
	Q.push(30);
	Q.push(40);
	Q.push(50);
	cout<<"Queue elements after inserting elements:"<<endl;
	printQueue(Q);
	
	//removing elements
	Q.pop();
	Q.pop();
	cout<<"Queue elements after removing elements:"<<endl;
	printQueue(Q);
	
	return 0;
}

Output

输出量

Queue elements after inserting elements: 
 10 20 30 40 50
Queue elements after removing elements:
 30 40 50
 

翻译自: https://www.includehelp.com/stl/queue-push-and-queue-pop.aspx

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!对于你的问题,可以使用STLqueue模板来解决。假设你有一个数组arr,你需要找到一个最少个数的子数组,使得子数组的和大于等于一个给定的值target。下面是一个使用queue模板实现的示例代码: ```cpp #include <iostream> #include <queue> using namespace std; int findMinSubArray(int arr[], int n, int target) { queue<int> q; int currentSum = 0; int minSize = INT_MAX; for (int i = 0; i < n; i++) { currentSum += arr[i]; q.push(arr[i]); while (currentSum >= target) { minSize = min(minSize, (int)q.size()); currentSum -= q.front(); q.pop(); } } return minSize == INT_MAX ? 0 : minSize; } int main() { int arr[] = {1, 4, 2, 3, 5}; int n = sizeof(arr) / sizeof(arr[0]); int target = 10; int minSize = findMinSubArray(arr, n, target); cout << "The minimum number of elements in a subarray with sum >= " << target << " is: " << minSize << endl; return 0; } ``` 在这个示例代码,我们使用了一个队列q来存储当前子数组的元素。我们通过遍历数组arr,并将元素依次加入队列,同时更新当前子数组的和currentSum。当currentSum大于等于目标值target时,我们开始从队列移除元素,直到currentSum小于target。在这个过程,我们记录队列的大小,也就是子数组的元素个数,并不断更新最小值minSize。最后,我们返回minSize作为结果,如果没有符合条件的子数组,则返回0。 在上面的示例代码,给定的数组arr为{1, 4, 2, 3, 5},目标值target为10。运行结果显示最少元素个数为3,即子数组{4, 2, 3}的和大于等于10。 希望这个示例能帮到你!如果还有其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值