Fork me on GitHub

STL——容器(List)list 数据的存取

list.front();

//返回第一个元素

list.back();

//返回最后一个元素

 1 #include <iostream>
 2 #include <list>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int num[] = { 111,222,333,444,555 };
 9     list<int> listInt(num, num + size(num));
10 
11     int iFront = listInt.front();
12     int iBack = listInt.back();
13 
14     cout << "iFront的值为:" << iFront << endl;
15     cout << "iBack的值为:" << iBack << endl;
16 
17     cout << "修改前遍历 listInt:";
18     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
19     {
20         cout << *it << " ";
21     }
22     cout << endl;
23 
24     listInt.front() = 0;
25     listInt.back() = 666;
26 
27     cout << "修改后遍历 listInt:";
28     for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++)
29     {
30         cout << *it << " ";
31     }
32     cout << endl;
33 
34     return 0;
35 }

打印结果:

 

 

 

 

 

 

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

posted @ 2020-05-04 21:43  索智源  阅读(352)  评论(0编辑  收藏  举报