如何从C ++函数返回数百个值? [英] How do I return hundreds of values from a C++ function?

查看:82
本文介绍了如何从C ++函数返回数百个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,每当一个函数创建许多(数百或数千个)值时,我曾经让调用者传递一个数组,然后我的函数填充输出值:

  void computeValues(int input,std :: vector< int>& output); 

因此,函数将填充向量输出与它计算的值。但是这不是很好的C ++风格,因为我现在意识到。



以下函数签名更好,因为它不提交使用 std :: vector 使用任何容器:

  void computeValues(int input,std :: insert_iterator< int> outputInserter); 

现在,调用者可以使用一些插入器

  std :: vector< int>值; //或者可以使用deque,list,map,... 
computeValues(input,std :: back_inserter(values));再次,我们不提交使用 std :: vector

具体来说,这是很好,因为用户可能只需要一个 std :: set 等的值(我应该传递 c $ c

我的问题是: insert_iterator 正确或标准的做法吗?或者有更好的东西吗?



编辑:我编辑了这个问题,明确我不是说回两三个价值,而是数百或数千。 (假设您已返回在某个目录中找到的所有文件或图形中的所有边等)

解决方案

对编辑的响应:如果您需要返回数百和数千个值,元组当然不会是路要走。最好用迭代器选择解决方案,但最好不要使用任何特定的迭代器类型。






如果使用迭代器,应尽可能使用它们。在你的函数中,你使用了一个插入迭代器,如 insert_iterator< vector< int> > 。你失去了任何一般性。这样做:

 模板< typename OutputIterator> 
void computeValues(int input,OutputIterator output){
...
}

无论你给它什么,它都会工作。但它不会工作,如果你有不同的类型在返回集。你可以使用一个元组。在下一个C ++标准中也可以作为 std :: tuple

  boost :: tuple< int,bool,char> computeValues(int input){
....
}

value的值是可变的,并且值的类型来自固定集合,如(int,bool,char),可以查看 boost :: variant 。然而,这意味着仅在呼叫侧的改变。您可以保留上面的迭代器样式:

  std :: vector< boost :: variant< int,bool,char> >数据; 
computeValues(42,std :: back_inserter(data));


In C++, whenever a function creates many (hundreds or thousands of) values, I used to have the caller pass an array that my function then fills with the output values:

void computeValues(int input, std::vector<int>& output);

So, the function will fill the vector output with the values it computes. But this is not really good C++ style, as I'm realizing now.

The following function signature is better because it doesn't commit to using a std::vector, but could use any container:

void computeValues(int input, std::insert_iterator<int> outputInserter);

Now, the caller can call with some inserter:

std::vector<int> values; // or could use deque, list, map, ...
computeValues(input, std::back_inserter(values));

Again, we don't commit to using std::vector specifically, which is nice, because the user might just need the values in a std::set etc. (Should I pass the iterator by value or by reference?)

My question is: Is the insert_iterator the right or standard way to do it? Or is there something even better?

EDIT: I edited the question to make it clear that I'm not talking about returning two or three values, but rather hundreds or thousands. (Imagine you have return all the files you find in a certain directory, or all the edges in a graph etc.)

解决方案

Response to Edit: Well, if you need to return hundreds and thousands if values, a tuple of course would not be the way to go. Best pick the solution with the iterator then, but it's best not use any specific iterator type.


If you use iterators, you should use them as generic as possible. In your function you have used an insert iterator like insert_iterator< vector<int> >. You lost any genericity. Do it like this:

template<typename OutputIterator>
void computeValues(int input, OutputIterator output) {
    ...
}

Whatever you give it, it will work now. But it will not work if you have different types in the return set. You can use a tuple then. Also available as std::tuple in the next C++ Standard:

boost::tuple<int, bool, char> computeValues(int input) { 
    ....
}

If the amount of values is variadic and the type of the values is from a fixed set, like (int, bool, char), you can look into a container of boost::variant. This however implies changes only on the call-side. You can keep the iterator style of above:

std::vector< boost::variant<int, bool, char> > data;
computeValues(42, std::back_inserter(data));

这篇关于如何从C ++函数返回数百个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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