在C ++中创建数组而无需提前知道长度 [英] Creating arrays in C++ without knowing the length ahead of time

查看:377
本文介绍了在C ++中创建数组而无需提前知道长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小程序,以帮助加快我的实验室工作的某些数据分析.应该从文本文件中读取数据,创建一堆包含该数据的数组,然后进行一些数学运算.

I'm working on a small program to help speed up some data analysis for my lab work. It's supposed to read in data from a text file, create a bunch of arrays containing this data and then do some maths.

我一直遇到的问题是我不知道原始文本文件将包含多少行,因此我不知道要制作多大的数组.我是C ++的新手,现在我对动态调整大小的数组不满意,这是一些代码

The problem I keep running into is that I don't know how many lines the original text file will have, so I don't know how big to make my arrays. I'm very new to C++ and right now I don't feel comfortable with dynamically sized arrays, here's a bit of the code

// first determine the length of the file
ifstream dataFile ("xys_data.txt");
const int LENGTH = count(istreambuf_iterator<char>(dataFile), istreambuf_iterator<char>(), '\n'); // counts the number of new lines

// declare vector of type datapoint
dataPoint data[LENGTH];

当我尝试编译它时,我得到了错误

When I try and compile this i get the error

expected constant expression
cannot allocate an array of constant size 0
'data' : unknown size

但是我不是将LENGTH定义为常数吗?

But haven't I defined the LENGTH to be constant?

任何帮助将不胜感激.

编辑

遵循几乎所有人的建议,我开始使用std :: vector.我还有最后一个问题,我有点动摇.

Following the advice of almost all of you, I have started using std::vector. I have one last issue that I'm a bit shaky on.

在第一次尝试该程序时,我定义了一个数据结构:

In the first attempt at the program I defined a data structure:

struct dataPoint
{
  double x; // x values
  double y; // y values
  double s; // sigma values
};

然后,当我从文件中读取数据时,我将其发送到该结构,就像这样

Then when I read the data from the file, I sent it to this structure like so

while (!dataFile.eof()) // this loop writes out each row of data to the arrays x, y, s until it reaches the end of the file
{ 
  int j = 0;
  dataFile >> data[j].x >> data[j].y >> data[j].s;
  j++;
}

有没有一种方法可以使用矢量来做到这一点?我的第一个想法是定义向量x,y和s,并在循环中用x替换data[j].x,但这是行不通的.

Is there a way I can do this using vectors? My first thought is to define the vectors x, y and s and replace the data[j].x with x in the loop, but this doesn't work.

推荐答案

首先,C ++中内置的数组必须具有编译时大小.声明LENGTH变量const是不够的.使它成为编译时常量也很重要.您的LENGTH不是编译时间常量,因此无法声明大小为LENGTH的数组.这就是编译器试图告诉您的.

First and foremost, built in arrays in C++ have to have compile-time size. It is not enough to declare your LENGTH variable const. It is also important to make it a compile-time constant. Your LENGTH is not a compile time constant, so declaring an array of size LENGTH is not possible. This is what the compiler is trying to tell you.

当您需要构建一个事先不知道其大小的数组时,通常至少有以下三种方法可供选择:

When you need to build an array, whose size is not known in advance, you typically have at least three approaches to choose from:

  1. 两次通过.对数据源进行试运行"以确定未来阵列的确切大小.分配阵列.在数据源上进行第二次传递,以用数据填充数组.

  1. Two-pass reading. Make a "dry run" over the data source to determine the exact size of the future array. Allocate the array. Make a second pass over the data source to fill the array with data.

重新分配..使用可重新分配的数组.分配一个固定大小的数组,并用数据填充它.如果证明数组太小,则将其重新分配为更大的大小并继续填充它.继续读取并重新分配,直到读取了所有数据.

Reallocation. Use a reallocatable array. Allocate an array of some fixed size and fill it with data. If the array proves to be too small, reallocate it to bigger size and continue to fill it. Continue to read and reallocate until all data is read.

转换..将数据读入便宜且易于扩展的数据结构(如链接列表),然后将其转换为数组.

Conversion. Read the data into a cheaply and easily expandable data structure (like linked list), then convert it to array.

这些方法中的每一种都有其优缺点,局限性和适用范围.

Each of these approaches has it own pros and cons, limitations and areas of applicability.

在您的情况下,您似乎正在尝试使用第一种方法.在处理文件时使用它不是一个好主意.首先,对文件进行两次传递不是很有效.其次,在一般情况下,它可能会创建竞争条件:文件可能会在两次传递之间更改.如果您仍然想这样做,只需使用std::vector而不是内置数组即可.

It looks like in your case you are trying to use the first approach. It is not a very good idea to use it when you are working with files. Firstly, making two passes over a file is not very efficient. Secondly, in general case it might create a race condition: the file might change between the passes. If you still want to go that way, just use std::vector instead of a built-in array.

但是,在您的情况下,我建议您使用第二种方法.同样,使用std::vector存储数据.逐行读取数据并将其逐项附加到向量中.引导程序将根据需要自动重新分配自身.

However, in your case I would recommend using the second approach. Again, use std::vector to store your data. Read your data line by line and append it to the vector, item by item. The vector will automatically reallocate itself as necessary.

这篇关于在C ++中创建数组而无需提前知道长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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