STL中的转发列表实现单链列表。从C++ 11引入的前向列表在插入,删除和移动操作(如排序)方面比其他容器有用,并且允许时间常数插入和删除元素。它与列表的不同之处在于前向列表会跟踪对象的位置只有list的下一个元素同时跟踪下一个和上一个元素。
forward_list :: push_front
push_front()函数用于将元素从前面推入“转发”列表中。新值将在当前第一个元素和容器大小增加1之前的开始处插入到“转发”列表中。
句法 :
forwardlistname.push_front(value)
Parameters :
The value to be added in the front is
passed as the parameter
Result :
Adds the value mentioned as the parameter to the
front of the forward list named as forwardlistname
例子:
Input : forward_list forwardlist{1, 2, 3, 4, 5};
forwardlist.push_front(6);
Output : 6, 1, 2, 3, 4, 5
Input : forward_list forwardlist{5, 4, 3, 2, 1};
forwardlist.push_front(6);
Output :6, 5, 4, 3, 2, 1
错误和异常
1.强大的异常保证–如果引发异常,则容器中没有任何更改。
2.如果转发列表不支持作为参数传递的值,则它将显示未定义的行为。
// CPP program to illustrate
// push_front() function
#include
#include
using namespace std;
int main()
{
forward_list myforwardlist{ 1, 2, 3, 4, 5 };
myforwardlist.push_front(6);
// Forward list becomes 6, 1, 2, 3, 4, 5
for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)
cout << ' ' << *it;
}
输出:
6 1 2 3 4 5
应用程序:使用push_front()函数输入具有以下编号和顺序的空的转发列表,并对给定的转发列表进行排序。
Input : 7, 89, 45, 6, 24, 58, 43
Output : 6, 7, 24, 43, 45, 58, 89
// CPP program to illustrate
// application Of push_front() function
#include
#include
using namespace std;
int main()
{
forward_list myforwardlist{};
myforwardlist.push_front(43);
myforwardlist.push_front(58);
myforwardlist.push_front(24);
myforwardlist.push_front(6);
myforwardlist.push_front(45);
myforwardlist.push_front(89);
myforwardlist.push_front(7);
// Forward list becomes 7, 89, 45, 6, 24, 58, 43
// Sorting function
myforwardlist.sort();
for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)
cout << ' ' << *it;
}
输出
6 7 24 43 45 58 89
forward_list :: pop_front
pop_front()函数用于从前面弹出或删除向前列表中的元素。该值从一开始就从列表中删除,并且容器大小减小了1。
句法 :
forwardlistname.pop_front()
Parameters :
No parameter is passed as the parameter.
Result :
Removes the value present at the front
of the given forward list named as forwardlistname
例子:
Input : forward_list forwardlist{1, 2, 3, 4, 5};
forwardlist.pop_front();
Output :2, 3, 4, 5
Input : forward_list forwardlist{5, 4, 3, 2, 1};
forwardlist.pop_front();
Output :4, 3, 2, 1
错误和异常
1. No-Throw-Guarantee –如果引发异常,则容器中没有任何更改。
2.如果列表为空,则显示未定义的行为。
// CPP program to illustrate
// pop_front() function
#include
#include
using namespace std;
int main()
{
forward_list myforwardlist{ 1, 2, 3, 4, 5 };
myforwardlist.pop_front();
// forward list becomes 2, 3, 4, 5
for (auto it = myforwardlist.begin(); it != myforwardlist.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
#include
using namespace std;
int main()
{
forward_list myforwardlist{}, newforwardlist{};
myforwardlist.push_front(8);
myforwardlist.push_front(7);
myforwardlist.push_front(6);
myforwardlist.push_front(5);
myforwardlist.push_front(4);
myforwardlist.push_front(3);
myforwardlist.push_front(2);
myforwardlist.push_front(1);
// Forward list becomes 1, 2, 3, 4, 5, 6, 7, 8
while (!myforwardlist.empty()) {
newforwardlist.push_front(myforwardlist.front());
myforwardlist.pop_front();
}
for (auto it = newforwardlist.begin(); it != newforwardlist.end(); ++it)
cout << ' ' << *it;
}
输出
8 7 6 5 4 3 2 1
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。