Im работает над программой для ввода входного файла, сортировки и печати вывода. Эти входные файлы будут иметь 10, 100, 1000 или 100000 строк, каждая из которых имеет номер в строке.C++ номера в файле массиву
Прямо сейчас мой код работает только для первых 10 строк.
основная часть
int array[10];
int size;
size = sizeof array/sizeof(int);
и
if(file.is_open())
{
for(int i = 0; i < size ; ++i)
{
file >> array[i];
}
}
читать строки файла в массив.
Как это изменить, чтобы принимать переменные длины размеров строк и читать их в массиве.
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<ctime>
using namespace std;
void insertion_sort(int x[],int length)
{
int key,i;
for(int j=1;j<length;j++)
{
key=x[j];
i=j-1;
while(x[i]>key && i>=0)
{
x[i+1]=x[i];
i--;
}
x[i+1]=key;
}
}
int main()
{
int array[10];
int size;
size = sizeof array/sizeof(int);
int x;
char name[256];
string sort, order, dup;
cout << "enter a file\n";
cin >> name;
ofstream ofile;
ifstream file(name);
if(file.is_open())
{
for(int i = 0; i < size ; ++i)
{
file >> array[i];
}
}
else{
cout << "file doesnt exist";
return 1;
}
size = sizeof array/sizeof(int);
cout << "enter a sort method - type insertion_sort or merge_sort or quick_sort or counting_sort\n";
cin >> sort;
cout << "Type 'dec' for non decreasing or 'inc' for increasing order\n";
cin >> order;
cout << "Type yes or no to remove duplicates\n";
cin >> dup;
if (sort == "insertion_sort")
{
insertion_sort(array,size);
cout << "\ninsertion sort";
}
if (order == "dec" )
for(int i=0;i<size/2;i++)
swap(array[i],array[size-i-1]);
if(dup == "yes")
{
int i, j;
int NewLength = 1;
for(i=1; i< size; i++){
for(j=0; j< NewLength ; j++)
{
if(array[i] == array[j])
break;
}
if (j==NewLength)
array[NewLength++] = array[i];
}
}
cout<<endl<<"sorted "<<endl;
for(x=0;x<size;x++)
{
cout<<array[x]<<endl;
}
return 0;
}
Вы, должно быть, считали 'std :: vector', правильно? –