阅读数字列表并C++排序
Reading a list of numbers and sorting C++
我正在尝试从文件中读取数字列表,并通过将它们读取到数组中然后对数组的内容进行排序来对它们进行排序。但我得到了
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;
}
- 二叉排序树无法编译
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 下面是排序算法O(n)吗
- std::sort()函数无法对向量的一部分进行排序
- shell排序中的交换和比较
- clang格式:禁用排序包含
- 显示错误输出的简单数组排序程序
- 为什么我的排序算法会更改数组值
- 试图在c++中对数字列表进行排序
- 如何在C++中对数组进行冒泡排序