-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathlist_stl.cpp
76 lines (59 loc) · 1.46 KB
/
list_stl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream> //header library
#include <list> //header library
using namespace std;
int main() { // driver function
list<int> l;
//Init
list<int> l1{1,2,3,10,8,5}; //first object of list data type
//Different Datatype
list<string> l2{"apple","guava","mango","banana"}; //second object of list data type
l2.push_back("pineapple");
//sort
l2.sort();
//reverse
l2.reverse();
// pop_front
cout<<l2.front()<<endl;
l2.pop_front();
//add to the front the list
l2.push_front("kiwi");
cout<<l2.back()<<endl;
l2.pop_back();
//Iterate over the list and print the data
for(auto it=l2.begin();it!=l2.end();it++){
cout<< (*it)<<" -> ";
}
cout<<endl;
//some more functions in the list
l2.push_back("orange");
l2.push_back("guava");
for(string s:l2){
cout<<s<<"-->";
}
cout<<endl;
//remove a fruit
string f;
//cin>>f;
//l2.remove(f);
for(string s:l2){
cout<<s<<"-->";
}
cout<<endl;
//erase one or more elements
auto it = l2.begin();
it++;
it++;
l2.erase(it);
for(string s:l2){
cout<<s<<"-->";
}
cout<<endl;
//we can insert elements in the list
it = l2.begin();
it++;
l2.insert(it,"FruitJuice");
for(string s:l2){
cout<<s<<"-->";
}
return 0;
}