-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd_sort.cpp
More file actions
53 lines (37 loc) · 1.09 KB
/
Copy pathstd_sort.cpp
File metadata and controls
53 lines (37 loc) · 1.09 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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <chrono>
using namespace std;
int main() {
auto start = chrono::high_resolution_clock::now();
ifstream in("input.txt");
const int RECORD_SIZE = 100;
vector<string> records;
// read entire file into RAM
while (true) {
string record(RECORD_SIZE, '\0');
if (!in.read(&record[0], RECORD_SIZE)) {
break;
}
records.push_back(record);
}
cout << "Records loaded: " << records.size() << "\n";
// sort by key (first 10 bytes)
sort(records.begin(), records.end(), [](const string& a, const string& b) {
return a.substr(0, 10) < b.substr(0, 10);
});
// write output
ofstream out("std_output.txt");
for (const string& record : records) {
out.write(record.data(), RECORD_SIZE);
}
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = end - start;
cout << "std::sort time: "
<< elapsed.count()
<< " seconds\n";
return 0;
}