c++ - 一个函数可以返回超过 1 个变量/结构/vector 吗?

标签 c++

我正在编写一个函数,我在其中传递 3 个不同(结构)数据类型的 vector ,1 个 vector 将包含一些值,在执行所有操作后,我用不同的数据集填充其他两个 vector 。所以我的问题是函数是否可能返回超过 1 个变量/结构/vector 。

例如..

func_name(vect1,&vect2,&vect3) //function definition
{
// steps of performing some operations 
// enter code here vect1 contains data, vect2 & vect3 are empty
// after performing operation vect2 & vect3 filled with different set of data

is it possible to return vect2 & vect3 simultaneously.
}

func_name(vect1,vect2,vect3) // function calling.

最佳答案

使用references

template<typename T>
void func_name(std::vector<T>& vect1,  
               std::vector<T>& vect2,
               std::vector<T>& vect3)  //..Notice & sign
{
/* Your Processing */
}

func_name(vect1,vect2,vect3);
//Now all vect1, vect2 and vect3 will be updated as per func_name processing

可以使用const std::vector<T>& vect1作为第一个参数,如果你不必更新 vect1

关于c++ - 一个函数可以返回超过 1 个变量/结构/vector 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18286085/

相关文章:

c# - 如何通过 COM 接口(interface)调用 C# 方法来传递函数指针

c++ - 3D vector 的高效量级计算

c++ - 使用队列和堆栈将中缀转换为后缀的运行时间是多少?

c++ - 事务中的sqlite3_step(stmt)失败,错误5,而没有调用我的忙处理程序

C++ Template Linked List storing Class Objects - Unhandled Exception Access Violation 读取位置

c++ - 如何使用函数更新 c++ 类成员?

C++ 迭代器错误 "does not refer to a value"

c++ - 在编译时使用模板遍历结构字段

c++ - 在 C++ 中将对象作为参数传递

c++ - 有条件的 static_assert