将浮点数读入动态分配的数组,同时读取文件时增加数组的大小

发布于 2024-12-27 11:14:01 字数 292 浏览 1 评论 0原文

我正在尝试将浮点数从文件读取到动态分配的二维数组中。我正在使用c++。

在我的 .txt 文件中,行的元素由制表符空格分隔,并且每行都从新行开始。

我的问题是 -

我可以根据编号增加数组的大小吗?文件中存在的元素(行和列)的数量?如果是这样,请建议一种方法。

我想到了写不。文本文件第一行的行和列,并在开始时读取它们以设置循环的限制,这将为我的数组分配空间。有没有更好的方法来做到这一点,也许基于文本文件的格式?这样,我认为它会更接近流数据类型的场景。

提前致谢。

阿克希尔

I am trying to read floating point numbers from a file into a dynamically allocated 2D array. I am using c++.

In my .txt file the elements of a row are separated by a tab space each and each row starts on a new line.

My question is -

Can I increase the size of my array based on the no. of elements (rows and columns) present in the file? If so, please suggest a way to do it.

I thought of writing the no. of rows and columns on the first line of the text file and reading them in the beginning to set the limits of the loop which will allocate the space for my array. Is there a better way to do it, perhaps based on the formatting of the text file? This way, I think it will be closer to a streaming data kind of a scenario.

Thanks in advance.

Akhil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2025-01-03 11:14:01

考虑使用自动调整大小的 std::vector,而不是使用数组。在 C++ 中,这比使用原始数组更受欢迎,因为它更安全并且隐藏了资源管理。

事实上,将 std::vector 与流库结合使用,可以非常轻松地读取制表符分隔的浮点值文件:

ifstream input("my-file.txt");
vector<float> myValues;

for (float f; input >> f; )
    myValues.push_back(f);

或者,或者:

ifstream input("my-file.txt");
vector<float> myValues;

myValues.insert(myValues.begin(),
                istream_iterator<float>(input),
                istream_iterator<float>());

但是,上面的代码将读取浮点数的一维数组,而不是您想要的二维数组。要读取二维数组,一种选择是一次读取文件的一行,然后使用上述代码的修改将该行分解为单独的浮点数。例如:

ifstream input("my-file.txt");
vector< vector<float> > myValues;

for (string line; getline(input, line); ) {
    stringstream lineStream(line);

    vector<float> thisLine;

    thisLine.insert(thisLine.begin(),
                    istream_iterator<float>(lineStream),
                    istream_iterator<float>());
    myValues.push_back(thisLine);
}

希望这有帮助!

Rather than using an array, consider using a std::vector, which automatically resizes. In C++, this is strongly preferred to using a raw array, since it's safer and hides the resource management.

In fact, using a std::vector in conjunction with the stream libraries, it's very easy to read a file of tab-separated floating-point values:

ifstream input("my-file.txt");
vector<float> myValues;

for (float f; input >> f; )
    myValues.push_back(f);

Or, alternatively:

ifstream input("my-file.txt");
vector<float> myValues;

myValues.insert(myValues.begin(),
                istream_iterator<float>(input),
                istream_iterator<float>());

However, the above code will read a 1D array of floats, rather than the 2D array you wanted. To read a 2D array, one option is to read one line of the file at a time, then use a modification of the above code to break that line into individual floats. For example:

ifstream input("my-file.txt");
vector< vector<float> > myValues;

for (string line; getline(input, line); ) {
    stringstream lineStream(line);

    vector<float> thisLine;

    thisLine.insert(thisLine.begin(),
                    istream_iterator<float>(lineStream),
                    istream_iterator<float>());
    myValues.push_back(thisLine);
}

Hope this helps!

初见终念 2025-01-03 11:14:01

您无法增加动态分配数组的大小。事实上,堆上它后面的内存可能已经被分配给其他东西了。

不要使用数组,而是使用自动为您处理大小调整的数据结构,例如嵌套 std ::向量std::list取决于您的需要。

You cannot increase the size of a dynamically allocated array. In fact, the memory that follows it on the heap may have already been allocated to something else.

Instead of using an array use a data structure which automatically handles resizing for you, e.g. nested std::vector or std::list depending on your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文