STL中的棧和佇列

2021-07-26 11:52:09 字數 3795 閱讀 9221

empty() 如果隊列為空返回真

pop() 刪除對頂元素

push() 加入乙個元素

size() 返回優先佇列中擁有的元素個數

top() 返回優先佇列對頂元素

在預設的優先佇列中,優先順序高的先出隊。在預設的int型中先出隊的為較大的數。

標頭檔案:

[cpp]view plain

copy

#include 

1、普通方法:

[cpp]view plain

copy

priority_queue<

int>q;  

//通過操作,按照元素從大到小的順序出隊

2、自定義優先順序:

[cpp]view plain

copy

struct

cmp  

};  

priority_queue, vector<

int>, cmp>q;

//定義方法

//其中,第二個引數為容器型別。第三個引數為比較函式。

3、結構體宣告方式:

[cpp]view plain

copy

struct

node  

};  

priority_queueq;//定義方法

//在該結構中,y為值, x為優先順序。

//通過自定義operator《操作符來比較元素中的優先順序。

//在過載」<」時,最好不要過載」>」,可能會發生編譯錯誤

push(x) 將x壓入佇列的末端

pop() 彈出佇列的第乙個元素(隊頂元素),注意此函式並不返回任何值

front() 返回第乙個元素(隊頂元素)

back() 返回最後被壓入的元素(隊尾元素)

empty() 當隊列為空時,返回true

size() 返回佇列的長度

標頭檔案:

[cpp]view plain

copy

#include 

1、普通宣告

[cpp]view plain

copy

queue<

int>q;  

2、結構體

[cpp]view plain

copy

struct

node  

;  queueq;  

push(x) 將x加入棧中,即入棧操作

pop() 出棧操作(刪除棧頂),只是出棧,沒有返回值

top() 返回第乙個元素(棧頂元素)

size() 返回棧中的元素個數

empty() 當棧為空時,返回 true

和佇列差不多,其中標頭檔案為:

[cpp]view plain

copy

#include 

[cpp]view plain

copy

stack<

int>s1;

//入棧元素為 int 型

stacks2;// 入隊元素為string型

stacks3;//入隊元素為自定義型

[cpp]view plain

copy

/**/

/**************************************

|                                   |

|       stl中優先佇列使用方法       |

|                                   |        

|       chenlie                     |

|                                   |

|       2010-3-24                   |

|                                   |

*************************************

*/#include 

#include 

#include 

using

namespace

std;  

intc[100];  

struct

cmp1  

};  

struct

cmp2  

};  

struct

node  

};  

priority_queue>q1;  

priority_queue, vector<

int>, cmp1>q2;  

priority_queue, vector<

int>, cmp2>q3;  

priority_queueq4;  

queue>qq1;  

queueqq2;  

intmain()  

cout << endl;  

while

(!q4.empty())  

//    cout << endl;

}  return

0;  

}  

STL中棧 佇列和優先佇列的使用

stl standard template library,即標準模板庫,stl可分為容器 containers 迭代器 iterators 空間配置器 allocator 配接器 adapters 演算法 algorithms 仿函式 functors 六個部分。其中,容器分為三類 序列式容器,容...

STL佇列和棧 雙向佇列

stl 中佇列的使用 queue 基本操作 push x 將x壓入佇列的末端 pop 彈出佇列的第乙個元素 隊頂元素 注意此函式並不返回任何值 front 返回第乙個元素 隊頂元素 back 返回最後被壓入的元素 隊尾元素 empty 當隊列為空時,返回true size 返回佇列的長度 使用方法 ...

STL中棧和佇列的使用方法

stl 中優先佇列的使用方法 priority queu 基本操作 empty 如果隊列為空返回真 pop 刪除對頂元素 push 加入乙個元素 size 返回優先佇列中擁有的元素個數 top 返回優先佇列對頂元素 在預設的優先佇列中,優先順序高的先出隊。在預設的int型中先出隊的為較大的數。使用方...