简体   繁体   English

将char向量和string向量传递给函数

[英]Passing char vector and string vector to a function

How do I do: 我该怎么办:

void writeFiledata(vector<char> tttstatus, vector<string> iflines)
{

}

int main()
{
writeFiledata(vector<char> tttstatus, vector<string> iflines);
return 0;
}

I can pass two ints to a function, and I can pass a 3D vector to a function, but I can't pass two 1D vectors? 我可以将两个int传递给一个函数,我可以将3D矢量传递给一个函数,但是我不能传递两个1D矢量?

You can either pass your variable or pass reference. 您可以传递变量或传递引用。 While passing as varaible, your function going to create a local copy of your variables inside your function and impacts may not reflect in calling function(in you case main) after the function call, since you are not returning anything in your function writeFiledata function. 当传递为可变变量时,由于在函数writeFiledata函数中未返回任何内容,因此函数将在函数内部创建变量的本地副本,并且影响可能不会反映在函数调用之后的调用函数(在您为main的情况下)。 So in corrected code below, I opted option to pass variable as reference. 因此,在下面的更正代码中,我选择了将变量作为参考传递的选项。

void writeFiledata(vector<char> & tttstatus, vector<string>& iflines)
{

}

int main()
{
 vector<char> tttstatus;
 vector<string> iflines;
//fill vectors using std::vector::push_Back

writeFiledata( tttstatus,  iflines);
return 0;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM