[英]C++ Command Line Arguments
我正在为学校学习 C++,我对如何将命令行 arguments 集成到我的代码中感到困惑
int n = 1;
int c = 0;
n = argv[1];
c = int(argv[2]);
findPrimes(n, c);
return 0;
}
到目前为止,这是我的主要 function,但 n = argv[1]; 是类型错误,并且 c = int(argv[2]); 是数据丢失错误。 我知道我离我还很远,所以对改善我的问题和解决我的问题的任何帮助表示赞赏。
要将char*
字符串转换为int
,您可以使用 C 库atoi()
、 sscanf()
或其他等效的 function:
#include <cstdlib>
int n = std::atoi(argv[1]);
#include <cstdlib>
int n;
std::sprintf(argv[1], "%d", &n);
或者,您可以使用 C++ std:stoi()
function:
#include <string>
int n = std::stoi(argv[1]);
或者, std::istringstream
stream class:
#include <sstream>
int n;
std::istringstream(argv[1]) >> n;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.