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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。