std::forward_list::reverse()是CPP STL中的内置函数,可反转forward_list中存在的元素的顺序。
用法:
forwardlist_name.reverse()
参数:该函数不接受任何参数。
返回值:该函数没有返回值。它将反向转发列表。
下面的程序演示了以上函数:
程序1:
// C++ program to illustrate the
// reverse() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialising forward list
forward_list<int> forward = { 10, 20, 40, 30, 70 };
cout << "List elements before performing reverse operation:";
for (auto it = forward.begin(); it != forward.end(); ++it)
cout << *it << " ";
// Function that performs reverse operation
forward.reverse();
// printing elements of list
cout << "\nList elements after performing reverse operation:";
for (auto it = forward.begin(); it != forward.end(); ++it)
cout << *it << " ";
return 0;
}
输出:
List elements before performing reverse operation:10 20 40 30 70 List elements after performing reverse operation:70 30 40 20 10
相关用法
注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 forward_list::reverse() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。