list.reverse();
//反转链表,比如list包含1, 2, 3, 4, 5五个元素,运行此方法后,list就包含5, 4, 3, 2, 1元素。
1 #include <iostream> 2 #include <list> 3 4 using namespace std; 5 6 int main() 7 { 8 int num[] = { 1,2,3,4,5 }; 9 list<int> listInt(num, num + size(num)); 10 cout << "反序前遍历 listInt:"; 11 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) 12 { 13 cout << *it << " "; 14 } 15 cout << endl; 16 17 listInt.reverse(); 18 19 cout << "反序后遍历 listInt:"; 20 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) 21 { 22 cout << *it << " "; 23 } 24 25 return 0; 26 }
打印结果:
====================================================================================================================
原文:https://www.cnblogs.com/CooCoChoco/p/12831533.html