STL中栈和队列的使用方法



STL 中优先队列的使用方法(priority_queu)

基本操作:

empty() 如果队列为空返回真

pop() 删除对顶元素

push() 加入一个元素

size() 返回优先队列中拥有的元素个数

top() 返回优先队列对顶元素

在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。

使用方法:

头文件:

#include <queue>

声明方式:

1、普通方法:

priority_queue<int>q;

//通过操作,按照元素从大到小的顺序出队

2、自定义优先级: 

struct cmp
{
    bool operator()(int x, int y)
    {
        return x > y; // x小的优先级高
      //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高
}
};
priority_queue<int, vector<int>, cmp>q;//定义方法
//其中,第二个参数为容器类型。第三个参数为比较函数。

</span>

3、结构体声明方式:

struct node

{

int x, y;

friend bool operator < (node a, node b)

{

return a.x > b.x; //结构体中,x小的优先级高

}

};

priority_queue<node>q;//定义方法

//在该结构中,y为值, x为优先级。

//通过自定义operator<操作符来比较元素中的优先级。

//在重载”<”时,最好不要重载”>”,可能会发生编译错误

STL 中队列的使用(queue)

基本操作:

push(x) 将x压入队列的末端

pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值

front() 返回第一个元素(队顶元素)

back() 返回最后被压入的元素(队尾元素)

empty() 当队列为空时,返回true

size() 返回队列的长度

使用方法:

头文件:

#include <queue>

声明方法:

1、普通声明

queue<int>q;

2、结构体

struct node

{

int x, y;

};

queue<node>q;

STL
中栈的使用方法(stack)

基本操作:

push(x) 将x加入栈中,即入栈操作

pop() 出栈操作(删除栈顶),只是出栈,没有返回值

top() 返回第一个元素(栈顶元素)

size() 返回栈中的元素个数

empty() 当栈为空时,返回 true

使用方法:

和队列差不多,其中头文件为:

#include <stack>

定义方法为:

stack<int>s1;//入栈元素为 int 型

stack<string>s2;// 入队元素为string型

stack<node>s3;//入队元素为自定义型

/**//*

*===================================*

|                                   |

|       STL中优先队列使用方法       |

|                                   |

|       chenlie                     |

|                                   |

|       2010-3-24                   |

|                                   |

*===================================*

*/

#include <iostream>

#include <vector>

#include <queue>

using namespace std;

int c[100];

struct cmp1

{

bool operator ()(int x, int y)

{

return x > y;//小的优先级高

}

};

struct cmp2

{

bool operator ()(const int x, const int y)

{

return c[x] > c[y];

// c[x]小的优先级高,由于可以在对外改变队内的值,

//所以使用此方法达不到真正的优先。建议用结构体类型。

}

};

struct node

{

int x, y;

friend bool operator < (node a, node b)

{

return a.x > b.x;//结构体中,x小的优先级高

}

};

priority_queue<int>q1;

priority_queue<int, vector<int>, cmp1>q2;

priority_queue<int, vector<int>, cmp2>q3;

priority_queue<node>q4;

queue<int>qq1;

queue<node>qq2;

int main()

{

int i, j, k, m, n;

int x, y;

node a;

while (cin >> n)

{

for (i = 0; i < n; i++)

{

cin >> a.y >> a.x;

q4.push(a);

}

cout << endl;

while (!q4.empty())

{

cout << q4.top().y << " " << q4.top().x << endl;

q4.pop();

}

//    cout << endl;

}

return 0;

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 08:01:49

STL中栈和队列的使用方法的相关文章

C++中 栈和队列的使用方法

C++中 栈和队列已经被封装好,我们使用时只需要按照如下步骤调用即可. 1.包含相关的头文件 包含栈头文件: #include<stack> 包含队列头文件: #include<queue> 2.作相关定义 定义栈如下: stack<int> stk; 定义队列如下: queue<int> q; 3.使用相关操作 栈提供了如下的操作: s.empty() 如果栈为空返回true,否则返回falses.size() 返回栈中元素的个数s.pop() 删除栈顶元

(hdu step 8.1.1)ACboy needs your help again!(STL中栈和队列的基本使用)

题目: ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 73 Accepted Submission(s): 57   Problem Description ACboy was kidnapped!! he miss his mother very much and is very

STL中慎重选择删除元素的方法

 一.要删除容器中有特定值的所有对象 1.如果容器是vector.string或deque,则使用erase-remove习惯用法.例如: vector<int> c; c.erase(remove(c.begin(),c.end(),1963),c.end());//删除值是1963的元素 下面讲一下算法remove: template<classForwardIterator,class T> ForwardIteratorremove(ForwardIterator fi

栈和队列的基本方法

使用栈和队列, 1 #include<stack> 2 #include<queue> 定义int型的栈和队列如下 1 stack<int> s; 2 queue<int> q; 栈的基本方法: 1 s.empty() 如果栈为空返回true,否则返回false 2 s.size() 返回栈中元素的个数 3 s.pop() 删除栈顶元素但不返回其值 4 s.top() 返回栈顶的元素,但不删除该元素 5 s.push() 在栈顶压入新元素 队列的基本方法:

STL中的优先级队列priority_queue

priority_queue(queue类似)完全以底部容器为根据,再加上二叉堆(大根堆或者小根堆)的实现原理,所以其实现非常简单,缺省情况下priority_queue以vector作为底部容器.另外priority_queue缺省比较规则是less: class Compare = less<typename Sequence::value_type> less对应的是按照大根堆来实现优先级队列,当然也可以将比较规则设置为greater,这时候是按照小根堆来实现的优先级队列. priori

STL 中栈的使用方法(stack)

基本操作: push(x)  将x加入栈中,即入栈操作 pop()  出栈操作(删除栈顶),只是出栈,没有返回值 top()  返回第一个元素(栈顶元素) size()  返回栈中的元素个数 empty()  当栈为空时,返回 true 使用方法: #include <stack> using namespace std; 定义方法为: stack<int>s1;          //入栈元素为 int 型 stack<string>s2;     // 入队元素为s

c++中栈与队列的实现

栈:具有先进后出的特点,且只能在一端进行插入与删除的操作,栈的实现如下所示 struct truetype { bool get() { return true; } }; struct falsetype { bool get() { return false; } }; template<class T> struct typetraits { typedef falsetype  isnpodtype; }; template <> struct typetraits<

python中栈和队列简单学习

栈#模拟栈结构#栈有先后顺序的.后进的先取出,先进的最后取出stack=[] #压栈(向栈里存数据)stack.append("a")print(stack)stack.append("b")print(stack) #出栈(在栈里取数据)res1=stack.pop()print("res1=",res1)print(stack)res2 =stack.pop()print("res2=",res2)print(stack)

内存数据中栈与队列的区别