隊列是一種以先進先出 (FIFO) 類型的排列方式運行的容器。元素在隊列的後端插入,從隊列的前端刪除。為此,分別使用成員函數queue::push()和queue::pop()。
在本文中,我們將討論這些 queue::pop() 和 queue()::push() 函數。
queue::push() 在 C++ 中
push()隊列容器的函數用於將元素插入隊列的末尾。這是一個內置函數C++標準模板庫(STL)。該函數屬於<queue>頭文件。該元素被添加到隊列容器中,並且隊列的大小增加 1。
用法
queue_name.push(value)
參數
- 要插入的元素的值作為參數傳遞。
結果
- 添加一個與隊列末尾傳遞的參數值相同的元素。
例子
Input : myqueue myqueue.push(6); Output : 6 Input : myqueue myqueue.push(0); myqueue.push(1); Output : 0, 1
錯誤和異常
- 如果傳遞的值與隊列類型不匹配,則顯示錯誤。
- 如果參數不拋出任何異常,則顯示不拋出異常保證。
queue::push( 的示例)
CPP
// CPP program to illustrate
// Application of push() and pop() function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// Empty Queue
int c = 0;
queue<int> myqueue;
myqueue.push(5);
myqueue.push(13);
myqueue.push(0);
myqueue.push(9);
myqueue.push(4);
// queue becomes 5, 13, 0, 9, 4
// Counting number of elements in queue
while (!myqueue.empty()) {
myqueue.pop();
c++;
}
cout << c;
}
0 1 2
時間複雜度:O(1)(隊列pop()操作需要恒定的時間複雜度。)
Note: Here, output is printed on the basis of FIFO property.
queue::pop() 在 C++ 中
隊列容器的pop()函數用於從隊列的前麵刪除一個元素(隊列中最舊的元素)。這是 C++ 標準模板庫 (STL) 的內置函數。該函數屬於<queue>頭文件。該元素從隊列容器中刪除,隊列大小減 1。
用法
queue_name.pop()
參數
- 沒有傳遞參數
結果
- 刪除隊列中最舊的元素或本質上是最前麵的元素。
例子:
Input : myqueue = 1, 2, 3 myqueue.pop(); Output : 2, 3 Input : myqueue = 3, 2, 1 myqueue.pop(); Output : 2, 1
錯誤和異常
- 如果傳遞參數則顯示錯誤。
- 如果參數不拋出任何異常,則顯示不拋出異常保證。
queue::pop( 的示例)
CPP
2
時間複雜度:O(1)(隊列pop()操作需要恒定的時間複雜度。)
Note: Here, output is printed on the basis of FIFO property.
push()和pop()的應用在 C++ 中
給定多個整數,將它們添加到隊列中,並在不使用 size 函數的情況下查找隊列的大小。
Input : 5, 13, 0, 9, 4 Output: 5
算法
- 將給定的元素一一推入隊列容器。
- 繼續彈出隊列的元素,直到隊列變空,並遞增計數器變量。
- 打印計數器變量。
示例
CPP
// CPP program to illustrate
// Application of push() and pop() function
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// Empty Queue
int c = 0;
queue<int> myqueue;
myqueue.push(5);
myqueue.push(13);
myqueue.push(0);
myqueue.push(9);
myqueue.push(4);
// queue becomes 5, 13, 0, 9, 4
// Counting number of elements in queue
while (!myqueue.empty()) {
myqueue.pop();
c++;
}
cout << c;
}
5
時間複雜度:O(n) //n 是隊列的大小。
輔助空間:O(1)
C++中隊列push()和隊列pop()之間的區別
下表列出了 queue::push() 和 queue::pop() 之間的區別:
S. 編號 |
隊列push() |
queue pop() |
---|---|---|
1. |
它用於在隊列末尾插入一個新元素。 | 它用於刪除隊列中的下一個元素 |
2. |
它的語法是-: 推(value_type&& val); |
它的語法是-: pop(); |
3. |
它需要一個參數,即要插入的值。 | 它不帶任何參數。 |
4. |
它的返回類型是void。 | 它的返回類型是void。 |
5. |
它存在於<queue>頭文件。 | 它存在於<queue>頭文件。 |
相關用法
- C++ queue front()用法及代碼示例
- C++ queue::swap()用法及代碼示例
- C++ queue::empty()、queue::size()用法及代碼示例
- C++ queue::front()、queue::back()用法及代碼示例
- C++ queue::push()、queue::pop()用法及代碼示例
- C++ queue::emplace()用法及代碼示例
- C++ quick_exit()用法及代碼示例
- C++ qsort()用法及代碼示例
- C++ cos()用法及代碼示例
- C++ sin()用法及代碼示例
- C++ asin()用法及代碼示例
- C++ atan()用法及代碼示例
- C++ atan2()用法及代碼示例
- C++ acos()用法及代碼示例
- C++ tan()用法及代碼示例
- C++ sinh()用法及代碼示例
- C++ ceil()用法及代碼示例
- C++ tanh()用法及代碼示例
- C++ fmod()用法及代碼示例
- C++ acosh()用法及代碼示例
- C++ asinh()用法及代碼示例
- C++ floor()用法及代碼示例
- C++ atanh()用法及代碼示例
- C++ log()用法及代碼示例
- C++ trunc()用法及代碼示例
注:本文由純淨天空篩選整理自AyushSaxena大神的英文原創作品 queue push() and pop() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。