Guest User

sort-TXT-CPP2

a guest
Jul 8th, 2023
1,386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <chrono>
  6.  
  7. void sort_file(const std::string& input_path, const std::string& output_path) {
  8.     std::ifstream input_file(input_path);
  9.     std::ofstream output_file(output_path);
  10.     std::vector<char> contents((std::istreambuf_iterator<char>(input_file)),
  11.                                std::istreambuf_iterator<char>());
  12.  
  13.     auto partition_point = std::partition(contents.begin(), contents.end(), [](char c) { return c != ' '; });
  14.     std::sort(contents.begin(), partition_point);
  15.  
  16.     std::copy(contents.begin(), contents.end(), std::ostreambuf_iterator<char>(output_file));
  17. }
  18.  
  19. int main() {
  20.     auto start = std::chrono::high_resolution_clock::now();
  21.     sort_file("input.txt", "output.txt");
  22.     auto stop = std::chrono::high_resolution_clock::now();
  23.     std::chrono::duration<double> elapsed = stop - start;
  24.     std::cout << "RunTime: " << elapsed.count() << std::endl;
  25.     return 0;
  26. }
  27.  
Tags: C++
Advertisement
Add Comment
Please, Sign In to add comment