【发布时间】:2015-05-12 08:16:39
【问题描述】:
我正在尝试创建一个 C++ 程序,该程序存储许多与数值相关联的名称,并且可以在输入名称时更改数值。我的问题是:如果我在主函数中创建一个数组,可以从另一个函数访问它吗?如果是这样,应该怎么做?
附加代码(部分)
#include <iostream>
#include <fstream> //required as input\output is from\to file
#include <string>
using namespace std;
int name_checker (string input);
int main()
{
int cases;
cin >> cases;
string names[cases]; //this is the array.
int i=0;
while (i<cases)
{
cin >> names[i];
i++;
}
}
int name_checker (string input);
{
//i want the data stored in above array to be availible here. possible?
}
【问题讨论】:
-
可以将数组作为参数传递...
-
您当然最好使用
std::vector<std::string> names(cases);。并将您的函数签名更改为int name_checker (const std::vector<std::string>& input)(请注意,函数定义没有分号!)。 -
@πάνταῥεῖ:谢谢。分号是一个错字。