阅读数字列表并C++排序

Reading a list of numbers and sorting C++

本文关键字:C++ 排序 列表 数字      更新时间:2023-10-16

我正在尝试从文件中读取数字列表,并通过将它们读取到数组中然后对数组的内容进行排序来对它们进行排序。但我得到了

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 Arrayt";
     for(i=0;i<5;i++)
       cout<<numbers[i]<<"t";
    cout<<endl;
}

导致错误的行是:

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而不是数组来抽象"我的数组应该有多大?"问题(您的示例看起来可能存在问题(。

以下是使程序正常工作所需的操作:

  • ANYSIZE_ARRAY更改为 5
  • 在您阅读数字的地方,将while替换为以下内容:

    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() 向其添加数字。矢量将自动调整大小,并容纳您输入的任意数量的数字。然后,您可以将代码中的 5 s 替换为向量的 size() 方法。

您收到原始错误是因为此行没有意义:

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

C++查看该行,就好像您正在尝试将数字打印到 stdout 流(在我们的例子中是控制台、屏幕(,然后将该流分配给"数字"数组。您无法将输出流分配给整数数组(因此错误无法从 ostream 转换为 int[1](。

首先,您不应该将外流变量分配给 int。其次,n 是长型,数字是整数数组。存在类型不安全。第三,应预先分配阵列大小。第四,你可以直接在 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 Arrayt";
for(i=0; i<ANYSIZE_ARRAY; i++)
    cout<<numbers[i]<<"t";
cout<<endl;
}