c++ - 使用数据文件对动态数组进行排序

标签 c++

我有一个作业,其中我必须从一个名为(“random.txt”)的文件中读取输出总数并将该文件动态复制到一个数组中。然后对文件中的值进行排序。

直到第 20 行,我的程序运行良好并输出我的总值以及文件中的所有数字。

第 21 行以后也运行了,但是当我运行它时它没有输出我在第 20 行中的总数,它也没有按顺序显示值。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
  ifstream fin;
  int n=0;
  double temp;
  fin.open("data.txt");
  fin>>temp;
  while(fin)
  {
      n++;
      fin>>temp;
      cout<<temp<<endl;
  }
  cout<<"Total:"<<n<<endl;
  fin.close(); //Program run fine up to here.
  fin.open("data.txt");
  double *A;
  A=new double[n];
  for (int i=0;i<n-1;i++)
    for (int j=i;j<n;j++)
    if (A[i]>A[j])
    {
      int temp=A[i];
      A[i]=A[j];
      A[j]=temp;
    }
  for (int i=0;i<n;i++)
    {
      while(fin)
      {
          n++;
          fin>>A[i];
          cout<<"Array:"<<A[i]<<endl;//Program runs up to here as well but 
                                     //but now doesn't print out the total I          
                                     //had in my program above and just 
                                     //prints A[i] and its not even sorted. 

      }

  }
  fin.close();
}

我知道我有很多错误,我是 c++ 的新手,所以我仍在努力学习。老实说,我不知道从第 32 行开始我在做什么。我知道我从 24-31 对我的数组进行了排序,但我不知道如何将我的文件读入我的数组或如何格式化它.

最佳答案

您的数组加载位置错误。它应该在排序之前,但在分配之后:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream fin("data.txt");
    double temp;
    size_t n = 0;

    while(fin >> temp)
    {
        std::cout << temp << ' ';
        ++n;
    }
    std::cout << "Total: "<< n << endl;

    fin.close();

    if (n > 0)
    {
        fin.open("data.txt");

        double *A = new double[n];
        for (int i=0; i<n && fin >> A[i]; ++i);

        fin.close();

        for (int i=0;i<n-1;i++)
        {
            for (int j=i;j<n;j++)
            {
                if (A[i]>A[j])
                {
                    int temp=A[i];
                    A[i]=A[j];
                    A[j]=temp;
                }
            }
        }

        for (int i=0; i<n; i++)
            cout << "Array[" << i << "]: " << A[i] << endl;

        delete [] A;
    }
}

老实说,有很多更好的方法来做到这一点。使用标准库容器,这整个动物园减少到...

#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>

int main()
{
    std::ifstream fin("data.txt");

    std::vector<double> A{
        std::istream_iterator<double>(fin),
        std::istream_iterator<double>() };

    std::sort(std::begin(A), std::end(A));

    for (auto x : A)
        std::cout << x << '\n';
}

关于c++ - 使用数据文件对动态数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29871773/

相关文章:

c++ - 我在识别浮点异常时遇到问题

android - 错误 :(289, 23)C++/JNI/NDK - 错误:无法使用 'char *' 类型的左值(又名 'jstring')初始化 '_jstring *' 类型的参数

c++ - C++ regex_replace 中的后缀 ($´) 不起作用

c++ - conio.h getch() 的 Linux 等价物

c++ - 从 std::string 到 unsigned char[] 的转换

c++ - QTableView - 如何防止选择更改

c++ - 使用 qSort 对字符串进行排序

具有不可复制不可移动元素类型的 C++ 容器

c++ - 当这些对象位于单独的共享库中时,如何向工厂自动注册 C++ 对象?

c++ - 使用 boost::asio 打洞