forked from anubhav100rao/codeplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms_stl.cpp
50 lines (27 loc) · 1.09 KB
/
algorithms_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
#include<bits/stdc++.h>
using namespace std;
bool comp_reverse(int a, int b) {
return a > b;
}
int main() {
// https://en.cppreference.com/w/cpp/algorithm
vector<int>arr = {7, 1, 4, 0, -1, 1};
sort(arr.begin(), arr.end());
sort(arr.begin(), arr.end(), comp_reverse);
reverse(arr.begin(), arr.end());
int mx_element = *max_element(arr.begin(), arr.end());
int mn_element = *min_element(arr.begin(), arr.end());
int sum = accumulate(arr.begin(), arr.end(), 0);
int fre_1 = count(arr.begin(), arr.end(), 1);
int first_occurence_4 = find(arr.begin(), arr.end(), 4) - arr.begin();
// these are much much important
// you will appreciate these functions when we teach you binary search on answer
sort(arr.begin(), arr.end());
auto l = lower_bound(arr.begin(), arr.end(), 1);
auto r = upper_bound(arr.begin(), arr.end(), 1);
bool exist_1 = binary_search(arr.begin(), arr.end(), -1);
// ....
arr.erase(arr.begin()); // removes -1
arr.erase(arr.begin() + 1); // removes 1
arr.push_back(-1); arr.push_back(1);
}