转发列表 在STL中实现了单链表。forward list是从C++11引入的,在插入、移除和移动操作(如排序)中,它比其他容器更有用,并且允许以时间常量插入和移除元素。它与list的不同之处在于,forward list只跟踪下一个元素的位置,而list同时跟踪下一个和上一个元素。
null
前进列表::向前推
push_front()函数用于将元素从前面推入前向列表。在当前第一个元素和容器大小增加1之前,新值将插入到转发列表的开头。 语法:
forwardlistname.push_front(value)Parameters :The value to be added in the front is passed as the parameterResult :Adds the value mentioned as the parameter to thefront 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, 5Input : forward_list forwardlist{5, 4, 3, 2, 1}; forwardlist.push_front(6);Output :6, 5, 4, 3, 2, 1
错误和异常 1.强异常保证——如果抛出异常,容器中不会发生任何更改。 2.如果转发列表不支持作为参数传递的值,则它将显示未定义的行为。
CPP
// CPP program to illustrate // push_front() function #include <forward_list> #include <iostream> using namespace std; int main() { forward_list< int > 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, 43Output : 6, 7, 24, 43, 45, 58, 89
CPP
// CPP program to illustrate // application Of push_front() function #include <forward_list> #include <iostream> using namespace std; int main() { forward_list< int > 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
转发列表::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, 5Input : forward_list forwardlist{5, 4, 3, 2, 1}; forwardlist.pop_front();Output :4, 3, 2, 1
错误和异常 1.无抛出保证–如果抛出异常,则容器中不会发生任何更改。 2.如果列表为空,则显示未定义的行为。
CPP
// CPP program to illustrate // pop_front() function #include <forward_list> #include <iostream> using namespace std; int main() { forward_list< int > 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, 8Output: 8, 7, 6, 5, 4, 3, 2, 1
CPP
// CPP program to illustrate // application Of pop_front() function #include <forward_list> #include <iostream> using namespace std; int main() { forward_list< int > 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
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END