c++ - 将函数中的排序数字写入文本文件?

标签 c++

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

input file 1: 1 3 5 7 9 11...
input file 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;
}

还有我的函数...

#include <fstream>
#include <sstream>
#include <vector>
#include "iosort.h"

void ioSort(std::ifstream& in1, std::ifstream& in2, std::ofstream& out)
{

    int a, b;
    std::vector<int> f1, f2, f3; //create one vector for each input stream

    while (in1 >> a)
    {
        f1.push_back(a);
    }

    while (in2 >> b)
    {
        f2.push_back(b);
    }

    //now f1 and f2 are vectors that have the numbers from the input files
    //we know that in these input files numbers are sorted from low to high

    if (f1.size() > f2.size()) //input stream 1 was larger
    {
        for (int i = 0; i < f2.size(); i++)
        {
            if (f1[i] > f2[i]) //number at input vector 2 less that respective pos
            {                   //in input vector 1
                f3.push_back(f2[i]);
            }
            else if(f1[i] == f2[i]) //numbers are equal
            {
                f3.push_back(f1[i]);
                f3.push_back(f2[i]);
            }
            else //number in 1 is less than that in vector 2
            {
                f3.push_back(f1[i]);
            }
        }

        for (int i = f2.size(); i < f1.size(); i++)
        {
            f3.push_back(f1[i]); //push remaining numbers from stream 1 into vector
        }
    }
    else //input stream 2 was larger
    {
        for (int i = 0; i < f1.size(); i++)
        {
            if (f1[i] > f2[i]) //number at input vector 2 less that respective pos
            {                   //in input vector 1
                f3.push_back(f2[i]);
            }
            else if(f1[i] == f2[i]) //numbers are equal
            {
                f3.push_back(f1[i]);
                f3.push_back(f2[i]);
            }
            else //number in 1 is less than that in vector 2
            {
                f3.push_back(f1[i]);
            }
        }

        for (int i = f1.size(); i < f2.size(); i++)
        {
            f3.push_back(f1[i]); //push remaining numbers from stream 2 into vector
        }

    }

    //send vector contents to output file
    for (int i = 0; i < f3.size(); i++)
    {
        out << f3[i] << " ";
    }


}

每次编译和运行时,都会创建文件 output.txt,但它是空的。谁能指出我做错了什么。如果,主要是,我做类似的事情:

out << 8 << " " << 9 << std::endl;

然后它将显示在输出文件中。

最佳答案

啊哈!发现你的错误。您正在打开文件,然后将其直接读取到标准输出(列出文件内容的位置),然后将相同的流传递到您的函数中。你不可以做这个。每当您从文件中读取时,流都会在文件中移动得更远。当您进入排序功能时,您已到达文件末尾,因此没有读取任何数字!

你需要删除线

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;

相反,在将它们存储在 vector 中后将它们打印出来。

我会把剩下的回复留在下面,因为您或后代可能需要它。


我不确定您的输出文件问题是怎么回事。遍历整个链,看看哪里出了问题:

  1. 读入文件后,用 cout 打印出 f1 和 f2。他们在那里吗?你期待什么?如果是,我们可以继续前进。
  2. 在您的算法运行后,您的 f3 是否在那里,您期望什么?如果是这样,请继续!
  3. 这让您可以诊断出代码失败的确切行(即没有按照您的预期执行),并且您知道您可以控制您检查出的所有内容。
  4. 当然,除了使用 cout 之外,您还可以在调试环境下启动它并逐步查看会发生什么,但是如果您不知道如何操作,第一次诊断您的问题将花费更长的时间.

不过你还有其他问题,你的合并函数有错误。您最终会跳过某些元素,因为您只对两个数组使用一个索引。想一想:您只将一个数字插入 f1[i] > f2[i] 中的输出数组。或 f1[i] < f2[i] , 但你通过递增 i 来丢弃两者.

您可以采用合并循环并大大简化它,同时还可以修复您的错误:)。

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++);

现在 f3 包含您的排序数组,在​​ O(m+n) 时间内排序。如果您这样做是为了学习,我会先尝试使用您的方式来纠正您的错误,然后再切换到此方式。

如果你想写更少的代码并且速度不是问题,你可以使用<algorithm>也这样做,但这是一个可怕的 O((n+m)lg(n+m))。

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());

关于c++ - 将函数中的排序数字写入文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28101295/

相关文章:

c++ - 不同 gcc 版本中的结构成员

c++ - 在初始化列表中使用 `this` 时出错

c++ - SSL_accept() 抛出 "Invalid argument"错误

c++ - QTableView 和 ItemDelegate 用于显示进度条

c++ - 小部件未正确显示

C++ 识别 X 按钮和滚轮方向

python - 使用 f2py 的 C++ 源时 undefined symbol

c++ - std::nullptr_t 类型的纯右值

c++ - 如何在结构上使用 std::unique_ptr?

c++ - 头文件 C++ 的当前工作目录