我有一个 C++ 程序,我运行它来获取多个参数值。我想做的是:
假设我有两个参数:
int main(){
double a;
double b;
//some more lines of codes
}
现在,在我编译之后,我想将其运行为
./output.out 2.2 5.4
So that a
取值 2.2 且b
取值为 5.4。
当然一种方法是使用cin>>
但我不能这样做,因为我在集群上运行该程序。
你需要使用命令行参数 http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html在你的main
:
int main(int argc, char* argv[]) {
if (argc != 3) return -1;
double a = atof(argv[1]);
double b = atof(argv[2]);
...
return 0;
}
此代码使用解析参数atof http://www.cplusplus.com/reference/clibrary/cstdlib/atof/;你可以用stringstream http://www.cplusplus.com/reference/iostream/stringstream/反而。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)