-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_smallest_in_1k_large_files.cpp
More file actions
67 lines (57 loc) · 1.19 KB
/
10_smallest_in_1k_large_files.cpp
File metadata and controls
67 lines (57 loc) · 1.19 KB
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
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
#define MAX_SIZE 10
class HeapStore {
public:
HeapStore(std::string filepath) {
filepath_ = filepath;
fd_.open(filepath_, std::ifstream::in);
}
~HeapStore() {
fd_.close();
}
void UpdateHeap(int num);
int GetNumber();
private:
//std::priority_queue<int, vector<int>, greater<int>> store_;
std::priority_queue<int> store_;
std::ifstream fd_;
string filepath_;
};
int HeapStore::GetNumber() {
std::string line;
std::getline(fd_, line);
return std::stoi(line);
}
void HeapStore::UpdateHeap(int num) {
if (store_.size() < MAX_SIZE) {
store_.push(num);
} else {
if (num < store_.top()) {
//heapify case
cout << "poping:" << store_.top() << "for:" << num << std::endl;
store_.pop();
store_.push(num);
}
else {
//skipping num larger than 1st 10
cout << "skipping:" << num << std::endl;
}
}
}
int main() {
std::string filepath;
cout << "enter file path:";
//provide input path as /home/md/numbers.txt
cin >> filepath;
HeapStore obj(filepath);
int cnt = 0;
while(cnt < 20) {
auto num = obj.GetNumber();
obj.UpdateHeap(num);
cnt++;
}
return 0;
}