1#include <algorithm>
2#include <iostream>
3#include <list>
4
5int main()
6{
7 // Create a list containing integers
8 std::list<int> l = { 7, 5, 16, 8 };
9
10 // Add an integer to the front of the list
11 l.push_front(25);
12 // Add an integer to the back of the list
13 l.push_back(13);
14
15 // Insert an integer before 16 by searching
16 auto it = std::find(l.begin(), l.end(), 16);
17 if (it != l.end()) {
18 l.insert(it, 42);
19 }
20
21 // Print out the list
22 std::cout << "l = { ";
23 for (int n : l) {
24 std::cout << n << ", ";
25 }
26 std::cout << "};\n";
27}
1#include <iostream>
2#include<list>
3#include<algorithm>
4
5using namespace std;
6
7int main()
8{
9 list<int>l{10,20,30};
10 for(list<int>::iterator it1=l.begin();it1!=l.end();it1++)
11 {
12 cout<<*it1<<" ";
13 }
14 cout<<endl;
15 list<int>::iterator it=l.begin();
16 l.insert(it,50);
17 for(list<int>::iterator it1=l.begin();it1!=l.end();it1++)
18 {
19 cout<<*it1<<" ";
20 }
21 cout<<endl;
22 l.push_back(60);
23 l.push_front(100);
24 for(list<int>::iterator it1=l.begin();it1!=l.end();it1++)
25 {
26 cout<<*it1<<" ";
27 }
28 cout<<endl;
29 l.pop_back();
30 l.pop_front();
31 for(list<int>::iterator it1=l.begin();it1!=l.end();it1++)
32 {
33 cout<<*it1<<" ";
34 }
35 cout<<endl;
36 cout<<l.empty()<<endl;
37 l.reverse();
38 for(list<int>::iterator it1=l.begin();it1!=l.end();it1++)
39 {
40 cout<<*it1<<" ";
41 }
42 cout<<endl;
43 list<int>l2{100,200,300,400};
44 l.swap(l2);
45 for(list<int>::iterator it1=l.begin();it1!=l.end();it1++)
46 {
47 cout<<*it1<<" ";
48 }
49 cout<<endl;
50 for(list<int>::iterator it1=l2.begin();it1!=l2.end();it1++)
51 {
52 cout<<*it1<<" ";
53 }
54 cout<<endl;
55 l2.sort();
56 for(list<int>::iterator it1=l2.begin();it1!=l2.end();it1++)
57 {
58 cout<<*it1<<" ";
59 }
60 cout<<endl;
61 l2.sort(greater<int>());
62 for(list<int>::iterator it1=l2.begin();it1!=l2.end();it1++)
63 {
64 cout<<*it1<<" ";
65 }
66 cout<<endl;
67 cout<<"--------"<<endl;
68 l.sort();
69 l2.sort();
70 l.merge(l2);
71 for(list<int>::iterator it1=l.begin();it1!=l.end();it1++)
72 {
73 cout<<*it1<<" ";
74 }
75 cout<<endl;
76 cout<<endl;
77 return 0;
78}
79