首页 > 其他 > 详细

STL——容器(List)list 的反序排列

时间:2020-05-05 17:43:19      阅读:56      评论:0      收藏:0      [点我收藏+]

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 }

打印结果:

技术分享图片

 

 

 

 

 

 

 

 

====================================================================================================================

STL——容器(List)list 的反序排列

原文:https://www.cnblogs.com/CooCoChoco/p/12831533.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!