-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.cpp
More file actions
105 lines (70 loc) · 2.56 KB
/
Copy pathiterator.cpp
File metadata and controls
105 lines (70 loc) · 2.56 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
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
95
96
97
98
99
100
101
102
103
104
105
#include<iostream>
#include<iterator> // for iterator
#include<vector> // for vector
using namespace std;
int main()
{
vector <int> v={1,2,3,4,5,6,7};
// Declaring iterator to a vector
vector<int>::iterator ptr;
/*begin() :- This function is used to return the beginning position of the container.
end() :- This function is used to return the after end position of the container.
*/
// Displaying vector elements using begin() and end()
cout << "The vector elements are : ";
for (ptr = v.begin(); ptr < v.end(); ptr++)
cout << *ptr << " ";
cout<<endl;
// Declaring iterator to a vector
vector<int>::iterator p = v.begin();
/*advance() :- This function is used to increment the iterator position till the specified number mentioned in its arguments.*/
// Using advance() to increment iterator position
// points to 4
advance(p, 3);
// Displaying iterator position
cout << "The position of iterator after advancing is : ";
cout << *p << " "<<endl;
/*
next() :- This function returns the new iterator that the iterator would point after advancing the positions mentioned in its arguments.
prev() :- This function returns the new iterator that the iterator would point after decrementing the positions mentioned in its arguments.
*/
// Declaring iterators to a vector
vector<int>::iterator a = v.begin();
vector<int>::iterator b = v.end();
// Using next() to return new iterator
// points to 4
auto it = next(a, 3);
// Using prev() to return new iterator
// points to 3
auto it1 = prev(b, 3);
// Displaying iterator position
cout << "The position of new iterator using next() is : ";
cout << *it << " ";
cout << endl;
// Displaying iterator position
cout << "The position of new iterator using prev() is : ";
cout << *it1 << " ";
cout << endl;
/*
inserter() :- This function is used to insert the elements at any position in the container. It accepts 2 arguments, the container and iterator to position where the elements have to be inserted
*/
vector <int> v1={1,2,3,4,5,6,7};
vector<int> v2 = { 4, 5, 6 };
vector<int>::iterator i1;
i1 = v2.begin() + 2;
// i1 points to next element of 5 in v2
// Using std::inserter inside std::copy
std::copy(v1.begin(), v1.end(), std::inserter(v2, i1));
// v2 now contains 4 5 1 2 3 7 8 9 6
// Displaying v1 and v2
cout << "v1 = ";
int i;
for (i = 0; i < 6; ++i) {
cout << v1[i] << " ";
}
cout << "\nv2 = ";
for (i = 0; i < 9; ++i) {
cout << v2[i] << " ";
}
return 0;
}