读取一组数字并对其进行排序的C++程序

3
我正在尝试从文件中读取一组数字,并通过将它们读入数组并对数组内容进行排序来对它们进行排序。但我遇到了问题。
error:incompatible types in assignment of 'std::basic_ostream<char, std::char_traits<char> >' to 'int [1]' 

我是一名新手程序员,这是我第一次使用C++。请问有谁能告诉我如何将数字列表写入数组,以便我可以对它们进行排序?以下是我的代码:

#include <fstream>
#include <iostream>
#include <iomanip>
#define ANYSIZE_ARRAY 1
using std::cout;
using std::endl;


int main()
{
  const char* filename = "test.txt";
  std::ifstream inputFile(filename);
  int numbers[ANYSIZE_ARRAY];
  int i, key;

  // Make sure the file exists
  if(!inputFile)
  {
    cout << endl << "The File is corrupt or does not exist. " << filename;
    return 1;
  }

  long n = 0;
  while(!inputFile.eof())
  {
    inputFile >> n;
    numbers = cout << std::setw(10) << n;
  }

  for(int j=1;j<5;j++)
    {
        i=j-1;
        key=numbers[j];
        while(i>=0 && numbers[i]>key)
        {
                    numbers[i+1]=numbers[i];
                    i--;
        }
        numbers[i+1]=key;
    }

    //Display sorted array
    cout<<endl<<"Sorted Array\t";
     for(i=0;i<5;i++)
       cout<<numbers[i]<<"\t";
    cout<<endl;
}

1
你的数组 numbers 太小了,无法读取你试图读入的行数。错误消息中的 int[1] 让我们知道,在最后的 for 循环中,你试图从一个只有 1 个值的数组中输出 5 个值,这是有问题的。在对其进行排序之前,打印未排序的数组可能会很有价值。最终,你将想要使用标准的 sort 模板函数。也许可以使用 int 向量来动态分配条目。 - Jonathan Leffler
文件中有多少个数字?它们的数量是恒定的吗? - Ove
@Ove 目前文件中有20个数字,但我希望它能接受一个变量,这样就不必是固定的了。 - Jmh2013
2
@JonathanLeffler 我想我会使用sort函数和向量。在查看awoodland给出的向量答案并进行一些研究后,它们似乎更容易满足我的需求。感谢您提供的信息。我也成功地用自己的方式实现了它。我正在慢慢开始学习C++。 - Jmh2013
3个回答

6

引起错误的代码行是:

numbers = cout << std::setw(10) << n;

我不确定你在这里想要做什么,看起来你只是想打印它,在这种情况下不需要使用 numbers =
读取所有数据的循环结构也存在问题。该行:while (!inputFile.eof()) 不符合C++的惯用法,也不能达到你的期望结果。关于这个问题,请参见此处的讨论(和此处)。
为了参考你可以使用std::sort来简单地完成这个任务。
#include <iterator>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iostream>

int main() {
  std::ifstream in("test.txt");
  // Skip checking it

  std::vector<int> numbers;

  // Read all the ints from in:
  std::copy(std::istream_iterator<int>(in), std::istream_iterator<int>(),
            std::back_inserter(numbers));

  // Sort the vector:
  std::sort(numbers.begin(), numbers.end());

  // Print the vector with tab separators: 
  std::copy(numbers.begin(), numbers.end(), 
            std::ostream_iterator<int>(std::cout, "\t"));
  std::cout << std::endl;
}

这个程序也使用了std::vector而不是数组来抽象出“我的数组应该有多大?”的问题(你的例子似乎可能存在问题)。


1
哇,这太神奇了!我不得不查找一些东西来理解它的工作原理,但这似乎比我尝试做的要容易得多。 - Jmh2013

3

以下是使您的程序正常运行所需的步骤:

  • Change ANYSIZE_ARRAY to 5
  • Where you read the numbers, replace the while with this:

    long n = 0;
    i=0;
    while(!inputFile.eof())
    {
            inputFile >> n;
            cout << std::setw(10) << n;
            numbers[i]=n;
            i++;
    }
    
这样,您将创建一个可以容纳5个数字的数组,并将数字读入该数组中。我使用5是因为我看到您在排序算法中使用了相同的数字,所以我假设您只需要读取5个数字。这是不好的,因为您的程序只能处理5个数字。
如果您需要让它处理可变数量的数字,则可以使用 std::vector<long> 来存储数字。您可以创建一个向量,然后使用push_back()方法添加数字。向量将自动调整大小并保存您放入其中的所有数字。然后您可以用向量的size()方法替换您代码中的5
由于这行代码毫无意义,所以出现了您最初的错误:
numbers = cout << std::setw(10) << n;

C++将这行代码视为您尝试将数字打印到stdout流(在我们的情况下是控制台,屏幕),然后将该流分配给“numbers”数组。您不能将输出流分配给整数数组(因此错误为无法从ostream转换为int [1])。


谢谢!我认为我需要调整我的编译器,因为在按照您建议的更改进行构建和运行后,我得到了“进程以状态255终止”的错误。它完成了大约一半,所以它是有效的,只是没有完成。 - Jmh2013

3

首先,你不应该将一个outstream变量赋值给一个int类型。 其次,n是长整型而numbers是一个整数数组。这存在类型不安全性。 第三,你应该预分配数组大小。 第四,你可以直接使用STL函数中的sort()来进行排序。

尝试以下代码:

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

#define ANYSIZE_ARRAY 5

int main()
{
const char* filename = "test.txt";
std::ifstream inputFile(filename);
int numbers[ANYSIZE_ARRAY];
int i, key;

// Make sure the file exists
if(!inputFile)
{
    cout << endl << "The File is corrupt or does not exist. " << filename;
    return 1;
}

int n = 0;
i = 0;
while(!inputFile.eof())
{
    inputFile >> n;
    cout << std::setw(10) << n;
    numbers[i++] = n;
}

sort(numbers, numbers + ANYSIZE_ARRAY);


//Display sorted array
cout<<endl<<"Sorted Array\t";
for(i=0; i<ANYSIZE_ARRAY; i++)
    cout<<numbers[i]<<"\t";

cout<<endl;
}

https://dev59.com/NW035IYBdhLWcg3wJcYv - Flexo
谢谢回复!这个方法可以用,但像awoodland所示,while(!inputFile.eof())不是C++的惯用写法。所以我可能需要使用另一种方法来实现。 - Jmh2013

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接