Advertisement
Guest User

Untitled

a guest
May 13th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iterator>
  6.  
  7. int main()
  8. {
  9.     std::ifstream f("f.txt"); //открываем файл f.txt на чтение
  10.     if(f)
  11.     {
  12.         std::ifstream g("g.txt");//открываем файл f.txt на чтение
  13.         if(g)
  14.         {
  15.             //читаем файл f.txt в вектор fVec
  16.             std::vector<int> fVec = {std::istream_iterator<int>(f), std::istream_iterator<int>()};
  17.  
  18.             //читаем файл g.txt в вектор gVec
  19.             std::vector<int> gVec = {std::istream_iterator<int>(g), std::istream_iterator<int>()};
  20.  
  21.             //создаем вектор hVec размером равным сумме размеров двух считанных векторов (для того чтобы в него сложить результат)
  22.             std::vector<int> hVec(fVec.size() + gVec.size());
  23.            
  24.             //сливаем вектора fVec и gVec в результирующий вектор hVec, с учетом упорядоченности
  25.             //описание функции merge здесь : http://www.cplusplus.com/reference/algorithm/merge/
  26.             std::merge (
  27.                 fVec.begin(),fVec.end(),
  28.                 gVec.begin(),gVec.end(),
  29.                 hVec.begin());
  30.            
  31.             std::ofstream h("h.txt"); //открываем файл h.txt на запись
  32.            
  33.             //записываем результат в файл
  34.             for(const auto& item : hVec)
  35.             {
  36.                 h << item << ' ';
  37.             }
  38.             h << std::endl;
  39.         }        
  40.     }
  41.        
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement