-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path20_list_stl_I.cpp
94 lines (73 loc) · 1.88 KB
/
20_list_stl_I.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
TOPIC: List STL - I
List => Doubly Linked List
Useful in insert & delete from front, middle & tail.
(as it takes O(1) time in front & tail insertion)
Unlike in vector, If we want to insert some element in the front & middle, we first have to shift
all the elements.
Some useful Methods in List stl:-
- push_back : insert at tail
- push_front : insert at head
- pop_back : remove at tail
- pop_front : remove from front
- insert : insert in the middle
- erase(idx) : erase some of the elements
- remove(val) : remove all occurance of some value
*/
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> l;
// Init
list<int> l1{1,2,3,10,8,5};
list<string> l2{"apple","guava","mango","banana"};
l2.push_back("pineapple");
// Iterate over list and print the data
for(auto s:l2)
// for(string s:l2)
{
cout << s << "-->";
}
cout << endl;
// sort
l2.sort(); // lexographic sorting of strings
// reverse
l2.reverse();
// pop_front
cout << l2.front() << endl; // display front element
l2.pop_front();
// print the data
for(auto s:l2)
{
cout << s << "-->";
}
cout << endl;
// add to the front of the list
l2.push_front("kiwi");
cout << l2.back() << endl; // display last element
l2.pop_back();
// print the data
for(auto s:l2)
{
cout << s << "-->";
}
cout << endl;
// Iterate over list using iterators
for(auto it = l2.begin(); it != l2.end(); it++)
{
cout << (*it) << "-->";
}
cout << endl;
return 0;
}
/*
OUTPUT:
apple-->guava-->mango-->banana-->pineapple
pineapple
mango-->guava-->banana-->apple
apple
kiwi-->mango-->guava-->banana
kiwi-->mango-->guava-->banana
*/