-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic_array.cpp
122 lines (87 loc) · 2.55 KB
/
basic_array.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
// #include <bits/stdc++.h> // includes every header from stl
// #include <string>
#include <iostream>
#include <iterator>
// #include <stdlib.h> // srand, rand
#include <array>
#include <algorithm>
// std::cout << << << "\n";
void print_array(auto &arr){
for(auto i : arr){
std::cout << i << " ";
}
std::cout << "\n";
}
bool customGreaterFunction(int i, int j) {
return i > j ;
}
int binSearch(auto &arr, int target) {
int l = 0;
int r = arr.size() - 1;
int mid = 0;
while (l <= r){
mid = l + (r - l)/ 2;
if (arr[mid] == target){
return mid;
}
else if (arr[mid] < target){
l = mid + 1;
}
else r = mid - 1;
} // while
return -1;
}
int main(int argc, char *argv[]){
// srand (1); // random seed
// array c-style
// int arr[5] = {0,1,2};
// std::cout << sizeof(arr) << "\n";
// for (int i = 0; i != (sizeof(arr)/sizeof(*arr)); i++){
// std::cout << "i = " << i << " a[i] = " << arr[i] << "\n";
// }
// strings
// std::string str1("first string");
// std::cout << str1 << "\n";
// array #include <array>
// sort #include <algorithm>
std::array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
std::cout << "arr size " << s.size() << "\n";
print_array(s);
std::sort(s.begin(), s.end());
print_array(s);
std::sort(s.begin(), s.end(), std::greater<int>());
print_array(s);
std::sort(s.begin(), s.end(), std::less<int>());
print_array(s);
struct {
bool operator()(int a, int b) const
{
return a > b;
}
} customGreater;
std::sort(s.begin(), s.end(), customGreater);
std::sort(s.begin(), s.end(), customGreaterFunction);
print_array(s);
// binary search stl
if (std::binary_search(s.begin(), s.end(), 5, customGreaterFunction)){
std::cout << "Found! " << "\n";
}
else{
std::cout << "Not Found!" << "\n";
}
std::sort(s.begin(), s.end());
print_array(s);
// binary search mine
std::cout << "Found? " << binSearch(s, 50) << "\n";
// binary_search using lower_bound
// does not always work!!!!!!!!!!
// std::array<int, 9> s = {7, 4, 2, 8, 6, 1, 9, 0, 3};
// std::cout << "lower_bound " << std::lower_bound(s.begin(), s.end(), 5) - s.begin() << "\n";
//
// uint32_t data[] = {10, 20, 30, 40};
// auto dataSize = std::size(data);
// input and output files are csv files
// std::string infile(argv[1]);
// std::string outfile = infile.substr(0, infile.find(".")) + "_out.txt";
// std::cout << "infile: " << infile << "; outfile: " << outfile << std::endl;
} // main