Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将已排序的数字从函数输出写入文本文件?_C++ - Fatal编程技术网

C++ 将已排序的数字从函数输出写入文本文件?

C++ 将已排序的数字从函数输出写入文本文件?,c++,C++,我有两个文本文件,每个文件都有一个从最低到最高排序的未知整数数。。。例如: 输入文件1:1 3 5 7 9 11… 输入文件2:2 4 6 8 10 我想从两个文件中获取这些数字,从低到高进行排序,然后将两个输入文件中已排序数字的完整列表输出到单个输出文件。到目前为止我所拥有的 #include <iostream> #include <fstream> #include <string> #include <vector> #include "i

我有两个文本文件,每个文件都有一个从最低到最高排序的未知整数数。。。例如:

输入文件1:1 3 5 7 9 11…
输入文件2:2 4 6 8 10

我想从两个文件中获取这些数字,从低到高进行排序,然后将两个输入文件中已排序数字的完整列表输出到单个输出文件。到目前为止我所拥有的

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "iosort.h"

int main()
{
    const char* filename1 = "numberlist1.txt";
    const char* filename2 = "numberlist2.txt";
    std::ofstream ofs("output.txt");
    std::ifstream ifs1, ifs2;
    std::string input1, input2;

    ifs1.open(filename1);
    std::getline(ifs1, input1);
    std::cout << "Contents of file 1: " << input1 << std::endl;

    ifs2.open(filename2);
    std::getline(ifs2, input2);
    std::cout << "Contents of file 2: " << input2 << std::endl;

    ioSort(ifs1, ifs2, ofs);


    return 0;
}
#包括
#包括
#包括
#包括
#包括“iosort.h”
int main()
{
const char*filename1=“numberlist1.txt”;
const char*filename2=“numberlist2.txt”;
std::ofs流(“output.txt”);
std::ifstream ifs1、ifs2;
std::字符串input1,input2;
ifs1.open(filename1);
std::getline(ifs1,input1);
std::cout f2[i])//输入向量2处的数字减去相应的位置
{//在输入向量1中
f3.推回(f2[i]);
}
如果(f1[i]==f2[i])//数字相等,则为else
{
f3.推回(f1[i]);
f3.推回(f2[i]);
}
else//1中的数字小于向量2中的数字
{
f3.推回(f1[i]);
}
}
对于(int i=f2.size();if2[i])//输入向量2处的数字减去相应的位置
{//在输入向量1中
f3.推回(f2[i]);
}
如果(f1[i]==f2[i])//数字相等,则为else
{
f3.推回(f1[i]);
f3.推回(f2[i]);
}
else//1中的数字小于向量2中的数字
{
f3.推回(f1[i]);
}
}
对于(int i=f1.size();ioutAHA!找到了错误。您正在打开文件,然后将其直接读取到stdout(在那里列出文件内容),然后将同一个流传递到函数中。您不能这样做。每当您从文件中读取时,流将在文件中移动得更远。当您在排序函数中时,您已在文件的末尾,因此不会读取任何数字

您需要删除这些行

std::getline(ifs1, input1);
std::cout << "Contents of file 1: " << input1 << std::endl;
现在f3包含排序的数组,按O(m+n)时间排序。如果您是为了学习而这样做的,我会尝试在切换到该数组之前先用您的方法纠正您的错误

如果你想写更少的代码并且速度不是问题,你也可以用
来做,但是这是一个糟糕的O((n+m)lg(n+m))

简而言之:

#include <fstream>
#include <algorithm>
#include <iterator>

int main()
{
    std::ifstream infile1("infile1.txt");
    std::ifstream infile2("infile2.txt");
    std::ofstream outfile("outfile.txt");

    std::merge(
        std::istream_iterator<int>{infile1}, std::istream_iterator<int>{},
        std::istream_iterator<int>{infile2}, std::istream_iterator<int>{},
        std::ostream_iterator<int>{outfile, " "}
        );
}
#包括
#包括
#包括
int main()
{
std::ifstream infile1(“infile1.txt”);
std::ifstream infile2(“infile2.txt”);
std::ofstream outfile(“outfile.txt”);
合并(
std::istream_迭代器{infie1},std::istream_迭代器{},
std::istream_迭代器{infile2},std::istream_迭代器{},
std::ostream_迭代器{outfile,“}
);
}

std::merge
是一种STL算法,它将两个已排序范围合并为一个已排序范围。这种情况下,范围就是文件。使用
std::istream\u迭代器将文件视为范围。使用
std::ostream\u迭代器将输出文件作为范围进行访问,因为您使用
std::getline()
在调用
ioSort()
之前,排序函数不需要读取任何内容

您可以使用
seekg()
倒回文件的开头


请参见

调试器说在合并阶段发生了什么我将在iosort返回时关闭ofs-但这可能不是原因,并在iosort中添加一个endl将所有文件中的所有数字放在一个or中。它们将自动为您排序。@疯狂-他的合并循环保留重复项,因此一个集合不起作用(尽管这是一个好主意)我强烈建议将所有数字读入
std::vector
,然后对向量进行排序。完成。简单。
std::getline(ifs1, input1);
std::cout << "Contents of file 1: " << input1 << std::endl;
std::getline(ifs2, input2);
std::cout << "Contents of file 2: " << input2 << std::endl;
auto it = f1.cbegin();
auto jt = f2.cbegin();

while (it != f1.cend() && jt != f2.cend()) {
  if (*it < *jt) f3.push_back(*jt++); //f2 was bigger, push f2>f3 and increment f2 index
  else if (*it > *jt) f3.push_back(*it++); //f1 was bigger, push f1>f3 and increment f1 index 
  else { //implicit equals, only option left
    f3.push_back(*jt++);
    f3.push_back(*it++);
  }
}

while (it != f1.cend()) f3.push_back(*it++);
while (jt != f2.cend()) f3.push_back(*jt++);
auto it = f1.cbegin();
auto jt = f2.cbegin();

while (it != f1.cend()) f3.push_back(*it++);
while (jt != f2.cend()) f3.push_back(*jt++);

std::sort(f3.begin(), f3.end());
#include <fstream>
#include <algorithm>
#include <iterator>

int main()
{
    std::ifstream infile1("infile1.txt");
    std::ifstream infile2("infile2.txt");
    std::ofstream outfile("outfile.txt");

    std::merge(
        std::istream_iterator<int>{infile1}, std::istream_iterator<int>{},
        std::istream_iterator<int>{infile2}, std::istream_iterator<int>{},
        std::ostream_iterator<int>{outfile, " "}
        );
}
ifs1.clear();
ifs1.seekg(0, ifs1.beg);
ifs2.clear();
ifs2.seekg(0, ifs1.beg);
ioSort(ifs1, ifs2, ofs);