Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include <iterator>
- int main()
- {
- std::ifstream f("f.txt"); //открываем файл f.txt на чтение
- if(f)
- {
- std::ifstream g("g.txt");//открываем файл f.txt на чтение
- if(g)
- {
- //читаем файл f.txt в вектор fVec
- std::vector<int> fVec = {std::istream_iterator<int>(f), std::istream_iterator<int>()};
- //читаем файл g.txt в вектор gVec
- std::vector<int> gVec = {std::istream_iterator<int>(g), std::istream_iterator<int>()};
- //создаем вектор hVec размером равным сумме размеров двух считанных векторов (для того чтобы в него сложить результат)
- std::vector<int> hVec(fVec.size() + gVec.size());
- //сливаем вектора fVec и gVec в результирующий вектор hVec, с учетом упорядоченности
- //описание функции merge здесь : http://www.cplusplus.com/reference/algorithm/merge/
- std::merge (
- fVec.begin(),fVec.end(),
- gVec.begin(),gVec.end(),
- hVec.begin());
- std::ofstream h("h.txt"); //открываем файл h.txt на запись
- //записываем результат в файл
- for(const auto& item : hVec)
- {
- h << item << ' ';
- }
- h << std::endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement