C++ STL 中的 queue::empty() 和 queue::size()
在本文中,我們將討論 C++ STL 中 queue::empty() 和 queue::size() 函式的工作原理、語法和示例。
什麼是 C++ STL 中的佇列?
佇列是在 C++ STL 中定義的一種簡單的序列或資料結構,它以 FIFO(先進先出)的方式進行資料的插入和刪除。佇列中的資料以連續的方式儲存。元素從佇列的末尾插入,從佇列的開頭刪除。在 C++ STL 中,已經有一個預定義的佇列模板,它以類似於佇列的方式插入和刪除資料。
什麼是 queue::empty()?
queue::empty() 是 C++ STL 中的一個內建函式,它在
語法
myqueue.empty();
此函式不接受任何引數。
返回值
如果關聯的佇列容器的大小為 0,則此函式返回真,否則返回假。
示例
Input: queue<int> myqueue = {10, 20, 30, 40};
myqueue.empty();
Output:
False
Input: queue<int> myqueue;
myqueue.empty();
Output:
True示例
#include <iostream>
#include <queue>
using namespace std;
int main(){
queue<int> Queue;
Queue.push(10);
Queue.push(20);
Queue.push(30);
Queue.push(40);
//check is queue is empty or not
if (Queue.empty()){
cout<<"Queue is empty";
}
else{
cout <<"Queue is not empty";
}
return 0;
}輸出
如果我們執行上面的程式碼,它將生成以下輸出:
Queue is not empty
什麼是 queue::size()?
queue::size() 是 C++ STL 中的一個內建函式,它在 <queue> 標頭檔案中宣告。queue::size() 用於檢查關聯的佇列容器的大小。此函式返回一個無符號整數值,即佇列容器的大小,或佇列容器中存在的元素數量。如果佇列為空或其中沒有元素,則此函式返回 0。
語法
myqueue.size();
此函式不接受任何引數。
返回值
此函式返回無符號整數,即與該函式關聯的佇列容器的大小。
示例
Input: queue<int> myqueue = {10, 20 30, 40};
myqueue.size();
Output:
4
Input: queue<int> myqueue;
myqueue.size();
Output:
0示例
#include <iostream>
#include <queue>
using namespace std;
int main(){
queue<int> Queue;
Queue.push(10);
Queue.push(20);
Queue.push(30);
Queue.push(40);
cout<<"size of Queue is : "<<Queue.size();
return 0;
}輸出
如果我們執行上面的程式碼,它將生成以下輸出:
size of Queue is : 4
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP