C++ List 库 - reverse() 函数
描述
C++ 函数 std::list::reverse() 颠倒列表中元素的顺序。
声明
以下是 std::list::reverse() 函数形式 std::list 头的声明。
C++98
void reverse();
C++11
void reverse() noexcept;
参数
None
返回值
None
异常
此成员函数从不抛出异常。
时间复杂度
线性,即 O(n)
示例
以下示例显示了 std::list::reverse() 函数的用法。
#include <iostream>
#include <list>
using namespace std;
int main(void) {
list<int> l = {1, 2, 3, 4, 5};
l.reverse();
cout << "List contains following elements after reverse operation" << endl;
for (auto it = l.begin(); it != l.end(); ++it)
cout << *it << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
List contains following elements after reverse operation 5 4 3 2 1

