-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithm_stl.cpp
152 lines (121 loc) · 3.21 KB
/
algorithm_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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
void func1(vector<int> &v);
void func2(vector<int> &v);
void func3(vector<int> &v);
void func4(vector<int> &v);
void func5(vector<int> &v);
void func6(vector<int> &v);
void func7();
void func8(vector<int> &v);
void func9(vector<int> &v);
int main()
{
vector<int> v {4, 2, 1, 6, 10, 14, 12};
// all_of()
// input-> [start, end), condition
// returns true if all of them follow the condition
func1(v);
// any_of()
// input-> [start, end), condition
// returns true if any of them follow the condition
func2(v);
// none_of()
// input-> [start, end), condition
// returns true if none of them follow the condition
func3(v);
// for_each()
// input-> [start, end), function name
// apply that function to every element
func4(v);
// find()
// input-> [start, end), key
// return: iterator to key if found
// else return: end iterator (* iterator = garbage) if not found
func5(v);
// find_if()
// input-> [start, end), func name(UnaryPredicate)
// return: iterator to key if it satisfies func
// else return: end iterator (* iterator = garbage) if not found
func6(v);
//minmax({vector})
func7();
vector<int> v2 {1, 2, 2, -1, 4, 4, 3, 4, 6, 7};
cout << "Count: " << count(v2.begin(), v2.end(), 4) << endl;
// searching subsequence
vector<int> v3 {1, 2, 3, 4, 5};
vector<int> v4 {3, 4};
cout << "Search: " << *search(v3.begin(), v3.end(), v4.begin(), v4.end()) << endl;
string s1 = "Aayush";
string s2 = "yuh";
cout << "Search substr: " << *search(s1.begin(), s1.end(), s2.begin(), s2.end());
return 0;
}
void func1(vector<int> &v)
{
if (all_of(v.begin(), v.end(), [](int i) { return i % 2 == 0; })) // AND
{
cout << "All are even" << endl;
}
else
{
cout << "All are not even" << endl;
}
}
void func2(vector<int> &v)
{
if (any_of(v.begin(), v.end(), [](int i) { return i < 0; })) // OR
{
cout << "Atleast one number is negative" << endl;
}
else
{
cout << "All are positive" << endl;
}
}
void func3(vector<int> &v)
{
if (none_of(v.begin(), v.end(), [](int i){return i % 2;})) // none should be odd
{
cout << "None is odd" << endl;
}
else
{
cout << "some are odd" << endl;
}
}
void myfunc(int i)
{
cout << i * 10 << endl;
}
void func4(vector<int> &v)
{
for_each(v.begin(), v.end(), myfunc);
}
void func5(vector<int> &v)
{
vector<int>::iterator i = find(v.begin(), v.end(), 4);
cout << *i << endl;
}
bool isOdd(int i)
{
return i % 2 == 1;
}
void func6(vector<int> &v)
{
vector<int>:: iterator i = find_if(v.begin(), v.end(), isOdd);
cout << *i << endl;
}
void func7()
{
pair<int, int> result = minmax({1, 2, 4, 2, 10});
cout << result.first << endl;
cout << result.second << endl;
}
void func8(vector<int> &v){}
void func9(vector<int> &v){}
void func10(vector<int> &v){}
void func11(vector<int> &v){}