我正在尝试从文件中读取数字列表,并通过将它们读入数组然后对数组内容进行排序来对它们进行排序。但是我得到了
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;
}
答案 0 :(得分:6)
导致错误的行是:
numbers = cout << std::setw(10) << n;
我不太确定你在这里尝试做什么,看起来你只想打印它,在这种情况下不需要numbers =
。
读取所有数据的循环结构也存在问题。行:while (!inputFile.eof())
不是惯用的C ++,而不会做你所希望的。 See here for a discussion on that issue(和here)。
作为参考,您可以使用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 :(得分:3)
以下是使程序运行所需的操作:
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
方法替换代码中的size()
。
您收到原始错误,因为此行没有意义:
numbers = cout << std::setw(10) << n;
C ++视图就像你试图将数字打印到stdout流(在我们的例子中是控制台,屏幕),然后将该流分配给“数字”数组。您不能将输出流分配给整数数组(因此错误无法从ostream转换为int [1])。
答案 2 :(得分:3)
首先,不应将outstream变量分配给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 Array\t";
for(i=0; i<ANYSIZE_ARRAY; i++)
cout<<numbers[i]<<"\t";
cout<<endl;
}