-
Notifications
You must be signed in to change notification settings - Fork 2
/
68.cpp
90 lines (83 loc) · 2.58 KB
/
68.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
#include<iostream>
#include<algorithm>
#include<vector>
bool isOdd(int i)
{
if(i%2!=0)
return true;
else
return false;
}
int main()
{
//program to understand the remove(), remove_if(), remove_copy() and remove_copy_if() function
// remove() : remove elements from the specified range that are equal to the val
//initializing a vector
std::vector<int> v = {5,3,2,8,66,2,5,9,10};
std::cout<<"Vector : ";
for(int i=0;i<v.size();i++)
{
std::cout<<v[i]<<" ";
}
std::cout<<std::endl;
std::vector<int>::iterator pend,p;
//calling the remove function
pend = std::remove(v.begin(),v.end(),5);
//printing the resultant vector
std::cout<<"Updated vector : ";
for(p=v.begin();p!=pend;p++)
{
std::cout<<*p<<" ";
}
std::cout<<std::endl;
//initializing another vector
std::vector<int> v2 = {3,2,4,5,6,3,4,88,12};
std::cout<<"Vector 2 : ";
for(int i=0;i<v2.size();i++)
{
std::cout<<v2[i]<<" ";
}
std::cout<<std::endl;
//remove_if() : removes the element from the specified range for which the predicate func. is true
pend = std::remove_if(v2.begin(),v2.end(),isOdd);
//printing the resultant vector
std::cout<<"\nUpdated vector 2 : ";
for(p=v2.begin();p!=pend;p++)
{
std::cout<<*p<<" ";
}
std::cout<<std::endl;
std::vector<int> v3 = {5,6,7,3,4,2,5,4,6,3,7},v4(7);
//remove_copy(): it removes the element from the specified range and puts it into the resultant vector. It also returns the end iterator for the resultant vector
std::cout<<"\nVector 3 : ";
for(int i=0;i<v3.size();i++)
{
std::cout<<v3[i]<<" ";
}
std::cout<<std::endl;
pend = std::remove_copy(v3.begin(),v3.end(),v4.begin(),4);
//printing the resultant vector
std::cout<<"Updated vector 4 : ";
for(p=v4.begin();p!=pend;p++)
{
std::cout<<*p<<" ";
}
std::cout<<std::endl;
std::vector<int> v5 = {5,6,7,3,4,2,5,4,6,3,7},v6(7);
//remove_copy_if(): it removes the element from the specified range if predicate func returns true and puts it into the resultant vector. It also returns the end iterator for the resultant vector
std::cout<<"\nVector 5 : ";
for(int i=0;i<v5.size();i++)
{
std::cout<<v5[i]<<" ";
}
std::cout<<std::endl;
pend = std::remove_copy_if(v5.begin(),v5.end(),v6.begin(),isOdd);
//printing the resultant vector
std::cout<<"Updated vector 6 : ";
for(p=v6.begin();p!=pend;p++)
{
std::cout<<*p<<" ";
}
std::cout<<std::endl;
return 0;
}