C ++创建从ifstream的一个变量大小的数组 [英] C++ Creating a variable sized array from ifstream

查看:91
本文介绍了C ++创建从ifstream的一个变量大小的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是抬起头:我的C ++编程技巧和术语是中间的最好的。所以,请温柔。)

Just a heads up: My c++ programming skills and terminology is intermediate at best. So please be gentle ;).

我工作的一个大专班多排序算法。本来,我建程序采取在20整数数组,因为这是大如.txt文件当中。最终的实验室现在要求采取有10,100,1000,10000,100000到1000000不同数量的文件。我原来使用的ifstream的一个循环内的整数阅读。现在,我需要从文件中读取整数数量可变的,我遇到了这个code的问题。我已经广泛地搜索这个网站和谷歌找到一个回答这个问题。我已经尝试了几十种不同code片断,都无济于事。这里是code我目前正在运行的20整数工作。

I am working on a multi-sort algorithm for a college class. Originally, I built the program to take in an array of 20 integers, since that was as big as the .txt files were. The final lab is now asking to take in files that have 10, 100, 1000, 10000, 100000 and 1000000 different numbers. I originally used an ifstream inside a for loop to read in the ints. Now that I need to read a variable amount of ints from a file, I have run into issues with this code. I have extensively searched this site and Google to find an answer to this problem. I have tried dozens of different code snippets, to no avail. Here is the code I am currently running that works for 20 ints.

int i;
int A[20];
int length;
char unsortedFilename[200];
ifstream unsorted;

cout << "Please type the full name of the file you would like sorted.\n* ";
cin >> unsortedFilename;
unsorted.open(unsortedFilename);

length = (sizeof(A) / sizeof(*A));

for( i = 0; i < length; i++ )
{
    unsorted >> A[i];
    cout << A[i] << "\n";
}

insertionSort();

我有其他的code混在里面,但它的错误检查,重复的号码去除选择等我想它使code这样将运行的我的时候,其中number 我实际上是在文件中整数数量。此外,正如我前面提到的,我将需要输入在它共有1,000,000次编号的文件。我不相信一个int数组将能够容纳许多数字。难道要像换了我所有的整数以多头容易?

I do have other code mixed in there, but it's error checking, selection of duplicate number removal, etc. I would like it so that code like this would run "i" number of times, where "i" is actually the number of ints in the file. Also, as I mentioned earlier, I will need to input a file that has 1,000,000 numbers in it. I don't believe that an int array will be able to hold that many numbers. Is it going to be as easy as swapping all my ints over to longs?

感谢所有帮助您可以提供。

Thanks for any help you could provide.

推荐答案

你想要的是一个矢量。

试试这个,

int i;
vector<int> A;
int length;
string unsortedFilename;
ifstream unsorted;

cout << "Please type the full name of the file you would like sorted.\n* ";
cin >> unsortedFilename;
unsorted.open(unsortedFilename);

int temp;
for( i = 0; unsorted >> temp; i++ )
{
    A.push_back(temp);
    cout << A[i] << "\n";
}

insertionSort();

一个载体基本上是一个动态数组。因为需要更多的空间,它会自动增长。这样一来,如果你有10个,100个,甚至100000物品没关系,它会自动增长你。

A vector is basically a dynamic array. It automatically grows as more space is needed. That way it doesn't matter if you have 10, 100, or even 100000 items, it'll automatically grow for you.

另外使用一个字符串为您的文件名,部分文件名是超过200个字符。

Also use a string for your file name, some file names are longer than 200 characters.

祝您好运!

这篇关于C ++创建从ifstream的一个变量大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆