随机参数使用System()
Randomized Parameters Using System()?
我正在尝试使用system()
调用程序时为miniat尝试一些随机参数。我以前从来没有做过这样的事情,我不得不承认我很迷路。
例如,我可以这样做:
system("minisat -luby -rinc=1.5 <dataset here>")
如何将其随机化为-luby
或-no-luby
,并将-rinc
的1.5
值随机化?
system只是一个接受c风格字符串作为参数的普通函数。你可以自己构造字符串
bool luby = true;
double rinc = 1.5;
system((std::string("minisat -")+(luby?"luby":"no-luby")+" -rinc="+std::to_string(rinc)).c_str());
您需要使用变量动态构造命令。
bool luby = true; // if you want -no-luby, set it to be false
double rinc = 1.5; // set it to be other values
char command[1024];
std::string luby_str = (luby ? "luby" : "no-luby");
std::snprintf(command, sizeof(command), "minisat -%s -rinc=%f", luby_str.c_str(), rinc);
system(command);
就像@RemyLebeau指出的,c++风格应该更好。
std::string command;
std::ostringstream os;
os << "minisat -" << luby_str << " -rinc=" << rinc;
system(command.c_str());
在这里,您可以尝试使用像这样的随机字符串命令生成器来创建一个随机命令:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
#include <string>
std::string getCommand()
{
std::string result = "minisat ";
srand(time(0));
int lubyflag = rand() % 2; //Not the best way to generate random nums
//better to use something from <random>
if (lubyflag == 1)
{
result += "-luby ";
} else
{
result += "-no-luby ";
}
double lower_bound = 0; //Now were using <random>
double upper_bound = 2; //Or whatever range
std::uniform_real_distribution<double> unif(lower_bound,upper_bound);
std::default_random_engine re;
double rinc_double = unif(re);
result += "-rinc=" + rinc_double;
return result;
}
int main()
{
std::string command = getCommand();
system(command.c_str());
}
如果你想要所有的控制权,这样做:
bool flaga = false;
double valueb = 1.5;
system(std::string("ministat " + ((flaga) ? "-luby " : "-no-luby ") +
"rinc= " + std::to_string(valueb)).c_str());
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 使用单独的参数调用"boost::process::system()"时,获取"execv
- 您如何使用System()调用带有引号的参数的批处理文件
- c++中通过system()传递参数
- 随机参数使用System()
- System::对C++char*参数的字符串引用
- 如何将连接的字符串参数传递给system()函数
- 将项目从Visual Studio 2005迁移到2012 - System.参数nullexception,多个错误
- linux c++、Qt中的system()参数