我在我的 C++ 程序中使用 IBX cplex 求解器。默认情况下,它将求解输出打印到控制台。但是,我希望这个求解器输出为一个字符串,以后可以用于记录或 cout。为了实现这一点,我使用了下面的代码片段(使用 IBM Cplex 提供的 setOut 函数),但它什么也没打印。请告诉我实现这一目标的正确方法是什么?
//Other code stuffs to initialise cplex solver and model
std::ostringstream ss("");
_cplex.setOut(ss); //_cplex is an instance of IloCplex
std::cerr << ss.str() << std::endl; // Tried ss.rdbuf() also, didn't help
但是,它可以使用以下代码将输出内容写入文件,
//Other code stuffs to initialise cplex solver and model
std::ofstream of("dummy.txt"); //_cplex is an instance of IloCplex
_cplex.setOut(of); //Write successfully to the file dummy.txt