-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist2.cpp
More file actions
44 lines (35 loc) · 1.12 KB
/
linkedlist2.cpp
File metadata and controls
44 lines (35 loc) · 1.12 KB
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
#include<iostream>
#include<list>
#include<iterator>
using namespace std;
// int main(){
// // create a list
// list<int>l={1,2,3,4,5};
// // create an iterator to point to the first element of the list
// list<int>::iterator itr=l.begin();
// // increment itr to point to the 2nd element
// ++itr;
// // display the 2nd element
// cout<<"Second element: "<<*itr<<endl;
// // increment itr to point 4th ele
// ++itr;
// ++itr;
// // display 4th ele
// cout<<"Fourth element: "<<*itr;
// return 0;
// }
int main() {
// create a list
list<int> l = {1, 2, 3, 4, 5};
// create an iterator to point to the first element of the list
list<int>::iterator itr = l.begin();
// use advance to move the iterator to point to the 2nd element
advance(itr, 2);
// display the 2nd element
cout << "Second element: " << *itr << endl;
// use advance to move the iterator to point to the 4th element
advance(itr, 2);
// display the 4th element
cout << "Fourth element: " << *itr << endl;
return 0;
}