C++新手:编写一个从文件中提取 i 个向量的函数。如何从函数中获取 i/未知数量的向量?

发布于 2024-12-06 03:16:53 字数 452 浏览 1 评论 0 原文

我正在编写一个函数,用于从文件中获取数据集并将它们放入向量中。然后将数据集用于计算。在该文件中,用户将每个数据集写入标题(如“Dataset1”)下的一行中。当函数完成执行时,结果是 i 个向量。该功能运行得很好。

问题是我不知道如何从函数中获取向量! (1) 我认为我只能从函数返回一个实体。所以我无法返回 i 个向量。另外,(2)我无法将向量/数据集编写为函数参数并通过引用返回它们,因为每次计算的向量/数据集的数量都不同。如果还有其他可能性,我不知道。

我确信这是一个愚蠢的问题,但我在这里错过了什么吗?如果有任何建议,我将非常感激。到目前为止,我还没有将向量/数据集提取代码放入函数中;我把它保存在我的主文件中,它运行得很好。我现在想通过将所有数据提取代码放入其自己的函数中来清理我的代码。

对于每次计算,我确实知道该函数将在文件中找到的向量/数据集的数量,因为我已将该信息写入文件中并且可以提取它。我可以通过某种方式使用这些信息吗?

I am writing a function for getting datasets from a file and putting them into vectors. The datasets are then used in a calculation. In the file, a user writes each dataset on a line under a heading like 'Dataset1'. The result is i vectors by the time the function finishes executing. The function works just fine.

The problem is that I don't know how to get the vectors out of the function! (1) I think I can only return one entity from a function. So I can't return i vectors. Also, (2) I can't write the vectors/datasets as function parameters and return them by reference because the number of vectors/datasets is different for each calculation. If there are other possibilities, I am unaware of them.

I'm sure this is a silly question, but am I missing something here? I would be very grateful for any suggestions. Until now, I have not put the vector/dataset extraction code into a function; I have kept it in my main file, where it has worked fine. I would now like to clean up my code by putting all data extraction code into its own function.

For each calculation, I DO know the number of vectors/datasets that the function will find in the file because I have that information written in the file and can extract it. Is there some way I could use this information?

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

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

发布评论

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

评论(3

一人独醉 2024-12-13 03:16:53

如果每个向量的类型相同,您可以返回
std::vector >

这看起来像:

std::vector<std::vector<datatype> > function(arguments) {  
    std::vector<std::vector<datatype> > return_vector;
    for(int i =0; i < rows; ++i) {
       \\ do processing
       return_vector.push_back(resulting_vector);
    }
    return return_vector;
} 

If each vector is of the same type you can return a
std::vector<std::vector<datatype> >

This would look like:

std::vector<std::vector<datatype> > function(arguments) {  
    std::vector<std::vector<datatype> > return_vector;
    for(int i =0; i < rows; ++i) {
       \\ do processing
       return_vector.push_back(resulting_vector);
    }
    return return_vector;
} 
醉态萌生 2024-12-13 03:16:53

正如已经提到的,您可以简单地使用向量的向量。

此外,您可能想在它周围添加一个智能指针,只是为了确保您没有复制向量的内容(但这已经是一个改进。首先瞄准有效的东西)。

至于向量数量的信息,您可以通过将全局向量调整为适当的值来使用它。

As has been mentionned, you may simply use a vector of vectors.

In addition, you may want to add a smart pointer around it, just to make sure you're not copying the contents of your vectors (but that's already an improvement. First aim at something that works).

As for the information on the number of vectors, you may use it by resizing the global vector to the appropriate value.

守望孤独 2024-12-13 03:16:53

您的问题本质上是“如何从函数返回一堆东西?”碰巧你的东西向量,但这并不重要。重要的是你有一堆大小未知的东西。

您可以通过将一个问题改写为两个问题来完善您的思维:

  • 我如何表示一堆事物
  • 如何从函数返回该表示形式?

关于第一个问题,这正是容器的作用。您肯定知道,容器可以容纳任意数量的相似对象,因为您已经在使用容器了。示例包括 std::vectorstd::list 等。您选择使用哪个容器取决于您没有提到的情况 - 例如,要复制的项目有多昂贵,您是否需要从堆中间删除项目等等。

在您的具体情况下,知道我们所知甚少,看来您应该使用 std::vector。如您所知,模板参数是您要存储的内容的类型。在您的情况下,恰好(巧合)是一个 std::vector。 (容器及其包含的对象碰巧是相似类型的事实并不重要。如果您需要一堆 Blob 或 Widget,您可以说 std::vector std::vector 因为您需要一堆 vector,所以您说 vector。 >。)因此,您可以这样声明它:(

std::vector<std::vector<double > > myPile;

注意 >> 之间的空格。在以前的 C++ 标准中需要该空格。 )

您可以像构建 vector 一样构建该向量 - 使用通用算法,或调用 push_back 或其他方式。因此,您的代码将如下所示:

void function( /* args */ ) {
    std::vector<std::vector<double> > myPile;
    while( /* some condition */ ) {
        std::vector<double> oneLineOfData;
        /* code to read in one vector */
        myPile.push_back(oneLineOfData);
    }
}

通过这种方式,您将所有传入数据收集到一个结构中,myPile

至于第二个问题,如何返回数据。嗯,这很简单——使用 return 语句。

std::vector<std::vector<double> > function( /* args */ ) {
    std::vector<std::vector<double> > myPile;
    /* All of the useful code goes here*/
    return myPile;
}

当然,您还可以通过对向量的传入引用返回信息:

void function( /* args */, std::vector<std::vector<double> >& myPile)
{
    /* code goes here. including: */
    myPile.push_back(oneLineOfData);
}

或通过对向量的传入指针:

void function( /* args */, std::vector<std::vector<double> >* myPile)
{
    /* code goes here. */
    myPile->push_back(oneLineOfData);
}

在这两种情况下,调用者必须创建双精度向量的向量在调用你的函数之前。首选第一种(return)方式,但如果您的程序设计规定,您可以使用其他方式。

You question is, at its essence "How do I return a pile of things from a function?" It happens that your things are vector<double>, but that's not really important. What is important is that you have a pile of them of unknown size.

You can refine your thinking by rephrasing your one question into two:

  • How do I represent a pile of things?
  • How do I return that representation from a function?

As to the first question, this is precisely what containers do. Containers, as you surely know because you are already using one, hold an arbitrary numbers of similar objects. Examples include std::vector<T> and std::list<T>, among others. Your choice of which container to use is dictated by circumstances you haven't mentioned -- for example, how expensive are the items to copy, do you need to delete an item from middle of the pile, etc.

In your specific case, knowing what little we know, it seems you should use std::vector<>. As you know, the template parameter is the type of the thing you want to store. In your case that happens to be (coincidentally), an std::vector<double>. (The fact that the container and its contained object happen to be similar types is of no consequence. If you need a pile of Blobs or Widgets, you say std::vector<Blob> or std::vector<Widget>. Since you need a pile of vector<double>s, you say vector<vector<double> >.) So you would declare it thus:

std::vector<std::vector<double > > myPile;

(Notice the space between > and >. That space is required in the previous C++ standard.)

You build up that vector just as you did your vector<double> -- either using generic algorithms, or invoking push_back, or some other way. So, your code would look like this:

void function( /* args */ ) {
    std::vector<std::vector<double> > myPile;
    while( /* some condition */ ) {
        std::vector<double> oneLineOfData;
        /* code to read in one vector */
        myPile.push_back(oneLineOfData);
    }
}

In this manner, you collect all of the incoming data into one structure, myPile.

As to the second question, how to return the data. Well, that's simple -- use a return statement.

std::vector<std::vector<double> > function( /* args */ ) {
    std::vector<std::vector<double> > myPile;
    /* All of the useful code goes here*/
    return myPile;
}

Of course, you could also return the information via a passed-in reference to your vector:

void function( /* args */, std::vector<std::vector<double> >& myPile)
{
    /* code goes here. including: */
    myPile.push_back(oneLineOfData);
}

Or via a passed-in pointer to your vector:

void function( /* args */, std::vector<std::vector<double> >* myPile)
{
    /* code goes here. */
    myPile->push_back(oneLineOfData);
}

In both of those cases, the caller must create the vector-of-vector-of-double before invoking your function. Prefer the first (return) way, but if your program design dictates, you can use the other ways.

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