我正在编写一个函数,用于从文件中获取数据集并将它们放入向量中。然后将数据集用于计算。在该文件中,用户将每个数据集写入标题(如“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?
发布评论
评论(3)
如果每个向量的类型相同,您可以返回
std::vector >
这看起来像:
If each vector is of the same type you can return a
std::vector<std::vector<datatype> >
This would look like:
正如已经提到的,您可以简单地使用向量的向量。
此外,您可能想在它周围添加一个智能指针,只是为了确保您没有复制向量的内容(但这已经是一个改进。首先瞄准有效的东西)。
至于向量数量的信息,您可以通过将全局向量调整为适当的值来使用它。
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.
您的问题本质上是“如何从函数返回一堆东西?”碰巧你的东西是
向量
,但这并不重要。重要的是你有一堆大小未知的东西。您可以通过将一个问题改写为两个问题来完善您的思维:
关于第一个问题,这正是容器的作用。您肯定知道,容器可以容纳任意数量的相似对象,因为您已经在使用容器了。示例包括
std::vector
和std::list
等。您选择使用哪个容器取决于您没有提到的情况 - 例如,要复制的项目有多昂贵,您是否需要从堆中间删除项目等等。在您的具体情况下,知道我们所知甚少,看来您应该使用 std::vector。如您所知,模板参数是您要存储的内容的类型。在您的情况下,恰好(巧合)是一个
std::vector
。 (容器及其包含的对象碰巧是相似类型的事实并不重要。如果您需要一堆 Blob 或 Widget,您可以说std::vector
或std::vector
因为您需要一堆vector
,所以您说vector。 >
。)因此,您可以这样声明它:(注意
>
和>
之间的空格。在以前的 C++ 标准中需要该空格。 )您可以像构建
vector
一样构建该向量 - 使用通用算法,或调用push_back
或其他方式。因此,您的代码将如下所示:通过这种方式,您将所有传入数据收集到一个结构中,
myPile
。至于第二个问题,如何返回数据。嗯,这很简单——使用
return
语句。当然,您还可以通过对向量的传入引用返回信息:
或通过对向量的传入指针:
在这两种情况下,调用者必须创建双精度向量的向量在调用你的函数之前。首选第一种(
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:
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>
andstd::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), anstd::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 saystd::vector<Blob>
orstd::vector<Widget>
. Since you need a pile ofvector<double>
s, you sayvector<vector<double> >
.) So you would declare it thus:(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 invokingpush_back
, or some other way. So, your code would look like this: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.Of course, you could also return the information via a passed-in reference to your vector:
Or via a passed-in pointer to your vector:
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.