stl 中優先佇列的使用方法(priority_queue)
基本操作:
empty() 如果隊列為空返回真
pop() 刪除對頂元素
push() 加入乙個元素
size() 返回優先佇列中擁有的元素個數
top() 返回優先佇列對頂元素
在預設的優先佇列中,優先順序高的先出隊。在預設的int型中先出隊的為較大的數。
使用方法:
標頭檔案:
#include
宣告方式:
1、普通方法:
priority_queueq;
//通過操作,按照元素從大到小的順序出隊
2、自定義優先順序:
struct cmp
};priority_queue, cmp>q;//
定義方法
//其中,第二個引數為容器型別。第三個引數為比較函式。
3、結構體宣告方式:
struct node
};priority_queueq;//
定義方法
//在該結構中,y為值
, x為優先順序。
//通過自定義
operator<
操作符來比較元素中的優先順序。
//在過載
」<」
時,最好不要過載
」>」
,可能會發生編譯錯誤
stl 中佇列的使用(queue)
基本操作:
push(x) 將x壓入佇列的末端
pop() 彈出佇列的第乙個元素(隊頂元素),注意此函式並不返回任何值
front() 返回第乙個元素(隊頂元素)
back() 返回最後被壓入的元素(隊尾元素)
empty() 當隊列為空時,返回true
size() 返回佇列的長度
使用方法:
標頭檔案:
#include
宣告方法:
1、普通宣告
queueq;
2、結構體
struct node
;queueq;
stl 中棧的使用方法(stack)
基本操作:
push(x) 將x加入棧中,即入棧操作
pop() 出棧操作(刪除棧頂),只是出棧,沒有返回值
top() 返回第乙個元素(棧頂元素)
size() 返回棧中的元素個數
empty() 當棧為空時,返回 true
使用方法:
和佇列差不多,其中標頭檔案為:
#include
定義方法為:
stacks1;//
入棧元素為
int
型stacks2;//
入隊元素為
string
型stacks3;//
入隊元素為自定義型
/**//*
*************************************
| |
| stl
中優先佇列使用方法
|| |
| chenlie |
| |
| 2010-3-24 |
| |
*************************************
*/#include
#include
#include
using
namespace std;
int c[100];
struct cmp1
};struct cmp2
};struct node
};priority_queueq1;
priority_queue, cmp1>q2;
priority_queue, cmp2>q3;
priority_queueq4;
queueqq1;
queueqq2;
int main()
cout << endl;
while (!q4.empty())
// cout << endl;
}return 0;
}
STL 棧,佇列,優先佇列
stl 棧 include includeusing namespace std struct node int main stackroot struct node x,y x.value 3 y.value 4 root.push x root.push y while root.empty r...
棧 佇列 優先佇列 STL
棧 include 標頭檔案 stackst 定義 st.push str1 0 入棧 cur st.top 取棧頂值 st.pop 出棧 st empty 空為true 佇列 include queue char que que.push a a que.front que.pop que.emp...
STL 棧與佇列
在大多數情況下可以不需要自己寫相應函式,直接呼叫stl中棧與佇列的相關函式,簡潔方便。以下是stl中棧與佇列的標頭檔案,定義方式以及常用函式 標頭檔案 include 佇列 include 棧定義方式 stacks 棧的定義方式 queueq 佇列的定義方式棧的基本操作 s.empty 如果棧為空返...