列表是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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。