STL-stack/queue/priority_queue函数的使用

STL-stack/queue/priority_queue函数的使用

Stack

  • 既然是使用,首先肯定是头文件#include

  • 函数使用:
    1. 定义:stack myStack;
    2. 判断栈是否为空:myStack.empty();
    3. 栈的大小:int my_size = myStack.size();
    4. 取出栈顶元素:int topVal = myStack.top();
    5. 弹出栈顶元素:myStack.pop();
    6. 压入元素:myStack.push(date);

  • 栈的性质:
    先入后出 ,后入先出
    STL-stack/queue/priority_queue函数的使用_第1张图片

  • 常见问题:

     1. 如何使用两个队列来实现栈功能
     文字描述:队列的特性:先进先出,所以为了实现栈先入后出,这里采用两个队列,一个push_queue,一个pop_queue;		
    
  • PUSH
    当有数据压入时,直接压入push_queue;

  • POP
    当有数据需要弹出时,将push_queue中的size-1个元素压入pop_queue,剩下的最后一个元素就是要弹出的元素,并再次将pop_queue中的元素压入到push_queue中;

  • TOP
    当要获取栈顶元素时,将push_queue中的size-1个元素压入pop_queue,剩下的最后一个元素就是要获取的栈顶元素,创建另一个对象后,在次将最后一个元素压入到pop_queue中,并将pop_queue中的元素压入到push_queue中。
    当然这里POP和PUSH也可以不进行最后的pop_queue元素转换到push_queue中,但需要进行判断(queue1,queue2)

```cpp
			//PUSH操作
			if(queue1.empty()&&!queue2.empty()){
				queue2.push(date);
			}else{
				queue1.push(date);
			}
			//POP操作
			if(queue1.empty()&&queue2.empty()){
				return -1;
			}else if(!queue1.empty()&&queue2.empty()){
				while(queue1.size()>1){
					queue2.push(queue1.front());
					queue1.pop();
				}
				int date = queue1.top();
				queue1.pop();
				return date;
			}else{
				while(queue2.size()>1){
					queue1.push(queue2.front());
					queue2.pop();
				}
				int date = queue2.top();
				queue2.pop();
				return date;
			}
			//TOP 操作同上
			```

STL-stack/queue/priority_queue函数的使用_第2张图片
Queue

  • 头文件:#include ;

  • 函数使用:
    1. 定义:queue myQueue;
    2. 判断栈是否为空:myQueue.empty();
    3. 栈的大小:int my_size = myQueue.size();
    4. 弹出栈顶元素:myQueue.pop();
    5. 压入元素:myQueue.push(date);
    6. 取出队首元素:int date = myQueue.front();
    7. 取出队尾元素:int date = myQueue.back();

  • 特性:先进先出,后进后出
    STL-stack/queue/priority_queue函数的使用_第3张图片

  • 常见问题

    1. 如何使用两个栈实现一个队列
      文字描述:为了实现栈先进后出,后进先出的功能,使用两个栈来实现队列的功能(pop_stack,push_stack);
  • PUSH

    直接压入push_stack中;

  • POP
    1. 当push_stack和pop_stack均为空时,出错;
    2. 当pop_stack不为空时,直接弹出栈顶的元素就可以;当pop_stack为空,而push_stack不为空时,将push_stack中元素全部压入到pop_stack中,弹出pop_stack的栈顶元素即可。

  • FRONT
    1. 当push_stack和pop_stack均为空时,出错;
    2. 当pop_stack不为空时,直接弹出栈顶的元素就可以;当pop_stack为空,而push_stack不为空时,将push_stack中元素全部压入到pop_stack中,取出pop_stack的栈顶元素即可。

  • BACK
    1. 当push_stack和pop_stack均为空时,出错;
    2. 当push_stack不为空时,直接弹出栈顶的元素就可以;当push_stack为空,而pop_stack不为空时,将pop_stack中元素全部压入到push_stack中,获取pop_stack的栈顶元素即可。

Priority_queue

  • 头文件:#include ;
  • 函数使用:
    - 定义:
    priority_queue<pair<int,int> > my_queue;   //对key-value对进行排序,默认大顶堆,从大到小进行排序
    priority_queue<int,vector<int>,less<int> >q; //也可定义为priority_queue q
    priority_queue<int,vector<int>,greater<int> >q //小顶堆进行排序,压入int数据成员
    
     - 压入数据:`my_queue.push(date)`;
     - 弹出数据:`my_queue.pop()`;
     - 堆首数据:`my_queue.top()`;
     - 堆的大小:`my_queue.size()`;
    
  • 特性:底层为堆,通常默认为大顶堆,即插入的数据被构建为大顶堆。堆排序算法在我的另一篇博客里,有需要可以查看:堆排序代码以及vector函数的使用。

以上就是我自己使用函数过程中的简单整理,有更多详细的常用方法也可以评论告诉我,让我又能学习一把。

你可能感兴趣的:(STL-stack/queue/priority_queue函数的使用)