列表是C++中用於以非連續方式存儲數據的容器。通常,數組和向量本質上是連續的,因此,與列表中的插入和刪除選項相比,插入和刪除操作的成本更高。
清單::pop_front()
pop_front()函數用於從正麵彈出或刪除列表中的元素。該值將從一開始就從列表中刪除,並且容器大小減小1。
用法:
listname.pop_front() 參數: No argument is passed as parameter. Result: Removes the value present at the front of the given list named as listname
例子:
Input: list list{1, 2, 3, 4, 5}; list.pop_front(); Output:2, 3, 4, 5 Input: list list{5, 4, 3, 2, 1}; list.pop_front(); Output:4, 3, 2, 1
錯誤和異常
- No-Throw-Guarantee-如果引發異常,則容器中沒有任何更改。
- 如果列表為空,則顯示未定義的行為。
// CPP program to illustrate
// pop_front() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> mylist{ 1, 2, 3, 4, 5 };
mylist.pop_front();
// list becomes 2, 3, 4, 5
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
}
輸出:
2, 3, 4, 5
應用程序:使用push_front()函數輸入具有以下編號和順序的空列表,然後打印列表的反麵。
Input:1, 2, 3, 4, 5, 6, 7, 8 Output:8, 7, 6, 5, 4, 3, 2, 1
// CPP program to illustrate
// application Of pop_front() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> mylist{}, newlist{};
mylist.push_front(8);
mylist.push_front(7);
mylist.push_front(6);
mylist.push_front(5);
mylist.push_front(4);
mylist.push_front(3);
mylist.push_front(2);
mylist.push_front(1);
// list becomes 1, 2, 3, 4, 5, 6, 7, 8
while (!mylist.empty()) {
newlist.push_front(mylist.front());
mylist.pop_front();
}
for (auto it = newlist.begin(); it != newlist.end(); ++it)
cout << ' ' << *it;
}
輸出:
8, 7, 6, 5, 4, 3, 2, 1
清單::pop_back()
pop_back()函數用於從背麵彈出或刪除列表中的元素。該值將從末尾的列表中刪除,並且容器大小減小1。
用法:
listname.pop_back() 參數: No argument is passed as parameter. Result: Removes the value present at the end or back of the given list named as listname
例子:
Input: list list{1, 2, 3, 4, 5}; list.pop_back(); Output:1, 2, 3, 4 Input: list list{5, 4, 3, 2, 1}; list.pop_back(); Output:5, 4, 3, 2
錯誤和異常
- No-Throw-Guarantee-如果引發異常,則容器中沒有任何更改。
- 如果列表為空,則顯示未定義的行為。
// CPP program to illustrate
// pop_back() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> mylist{ 1, 2, 3, 4, 5 };
mylist.pop_back();
// list becomes 1, 2, 3, 4
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
}
輸出:
1, 2, 3, 4
應用程序:使用push_front()函數輸入具有以下編號和順序的空列表,然後打印列表的反麵。
Input:1, 20, 39, 43, 57, 64, 73, 82 Output:82, 73, 64, 57, 43, 39, 20, 1
// CPP program to illustrate
// application Of pop_back() function
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> mylist{}, newlist{};
mylist.push_front(82);
mylist.push_front(73);
mylist.push_front(64);
mylist.push_front(57);
mylist.push_front(43);
mylist.push_front(39);
mylist.push_front(20);
mylist.push_front(1);
// list becomes 1, 20, 39, 43, 57, 64, 73, 82
while (!mylist.empty()) {
newlist.push_back(mylist.back());
mylist.pop_back();
}
for (auto it = newlist.begin(); it != newlist.end(); ++it)
cout << ' ' << *it;
}
輸出:
82, 73, 64, 57, 43, 39, 20, 1
相關用法
注:本文由純淨天空篩選整理自AyushSaxena大神的英文原創作品 list::pop_front() and list::pop_back() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。