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
|
#include <iostream>
#include <vector>
#include "sales_data.hpp"
#include "sales_item.hpp"
/*
*
* 3.26
*
* We cannot use '+' to add 2 iterators
*/
using std::string;
using std::cout;
using std::cin;
using std::cerr;
using std::clog;
using std::endl;
using std::vector;
int main () {
vector<int> in = {1, 4, 23, 35, 36, 111, 122, 135, 239, 345, 365, 399, 401, 477, 480, 592, 728, 777, 824, 928, 983, 1002, 1023, 1209};
int sought = 0;
for(auto vt : in) {
cout << vt << " ";
}
cout << endl;
if(cin >> sought) {
} else {
return -1;
}
auto beg = in.begin(), end = in.end();
auto mid = in.begin() + (end - beg)/2; // 12
while (mid != end && *mid != sought) {
if (sought < *mid) {
end = mid;
} else {
beg = mid + 1;
}
cout << *mid << endl;
mid = beg + (end - beg)/2;
}
if(mid != end) {
cout << "found: " << *mid << endl;
} else {
cout << "could not find: " << sought << endl;
}
return 0;
}
|